@echo off
setlocal EnableExtensions
title Installing YTD

echo YTD INSTALLER VERSION 2026-07-22B
echo.

set "SOURCE=%~dp0."
set "DEST=C:\YTD"
set "FFMPEG_BIN=%DEST%\FFMPEG\bin"

fltmc >nul 2>&1
if errorlevel 1 (
    echo Administrator permission is required.
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

echo Installing YTD...
echo Source:      "%SOURCE%"
echo Destination: "%DEST%"
echo.

if not exist "%SOURCE%\yt-dlp.exe" (
    echo ERROR: yt-dlp.exe was not found beside this installer.
    echo Extract the entire ZIP file before running RunMe1st.bat.
    echo.
    pause
    exit /b 1
)

if not exist "%SOURCE%\FFMPEG\bin\ffmpeg.exe" (
    echo ERROR: FFmpeg was not found at:
    echo "%SOURCE%\FFMPEG\bin\ffmpeg.exe"
    echo.
    pause
    exit /b 1
)

if not exist "%SOURCE%\YTD-Dashboard.ps1" (
    echo ERROR: YTD-Dashboard.ps1 was not found beside this installer.
    echo.
    pause
    exit /b 1
)

if not exist "%DEST%" mkdir "%DEST%"

robocopy "%SOURCE%" "%DEST%" /E /R:2 /W:1

if errorlevel 8 (
    echo.
    echo ERROR: YTD could not be copied to C:\YTD.
    echo.
    pause
    exit /b 1
)

for %%D in ("Songs" "Videos" "Logs") do (
    if not exist "%DEST%\%%~D" mkdir "%DEST%\%%~D"
)

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem -LiteralPath 'C:\YTD' -Recurse -File -ErrorAction SilentlyContinue | Unblock-File -ErrorAction SilentlyContinue"

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$newEntry='C:\YTD\FFMPEG\bin'; $machinePath=[Environment]::GetEnvironmentVariable('Path','Machine'); $entries=@($machinePath -split ';' | ForEach-Object {$_.Trim()} | Where-Object {$_}); if (-not ($entries | Where-Object {$_.TrimEnd('\') -ieq $newEntry.TrimEnd('\')})) {[Environment]::SetEnvironmentVariable('Path',(($entries + $newEntry) -join ';'),'Machine')}"

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "try { Add-MpPreference -ExclusionPath 'C:\YTD' -ErrorAction Stop } catch { exit 0 }"

echo.
echo Creating desktop shortcut...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$desktop=[Environment]::GetFolderPath('Desktop'); $link=Join-Path $desktop 'YTD Dashboard.lnk'; $shell=New-Object -ComObject WScript.Shell; $shortcut=$shell.CreateShortcut($link); $shortcut.TargetPath=$env:SystemRoot + '\System32\WindowsPowerShell\v1.0\powershell.exe'; $shortcut.Arguments='-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File ""C:\YTD\YTD-Dashboard.ps1""'; $shortcut.WorkingDirectory='C:\YTD'; $shortcut.IconLocation=$env:SystemRoot + '\System32\shell32.dll,167'; $shortcut.Description='Open the YTD Downloader dashboard'; $shortcut.Save(); if (-not (Test-Path -LiteralPath $link)) { exit 1 }"

if errorlevel 1 (
    echo WARNING: The desktop shortcut could not be created.
) else (
    echo Desktop shortcut created.
)

echo.
echo YTD was installed successfully at:
echo %DEST%
echo.
echo Launch YTD Dashboard from your desktop.
echo.
pause
exit /b 0
