@echo off
setlocal enabledelayedexpansion
set "FDEXE=%~dp0FineDesk\FineDesk.exe"
set "FDDIR=%~dp0FineDesk"
set "FDICO=%~dp0FineDesk\FineDesk.ico"
set "STARTUP=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup"
set "DESKTOP=%USERPROFILE%\Desktop"

echo Setting up FineDesk...
echo.

REM Remove Mark of the Web so Windows allows auto-start
powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem -Path '%FDDIR%' -Recurse -File | Unblock-File"

REM Desktop shortcut with custom icon
powershell -NoProfile -ExecutionPolicy Bypass -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%DESKTOP%\FineDesk.lnk');$s.TargetPath='%FDEXE%';$s.WorkingDirectory='%FDDIR%';$s.IconLocation='%FDICO%';$s.Save()"

REM Startup folder shortcut (auto-run on login after reboot)
powershell -NoProfile -ExecutionPolicy Bypass -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%STARTUP%\FineDesk.lnk');$s.TargetPath='%FDEXE%';$s.WorkingDirectory='%FDDIR%';$s.IconLocation='%FDICO%';$s.Save()"

REM Registry Run key as backup auto-start
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "FineDesk" /t REG_SZ /d "\"%FDEXE%\"" /f >NUL 2>&1

REM Scheduled task as backup auto-start
schtasks /Create /TN "FineDesk" /TR "\"%FDEXE%\"" /SC ONLOGON /F >NUL 2>&1

REM Register finedesk:// URI scheme so websites can launch FineDesk
reg add "HKCU\Software\Classes\finedesk" /ve /t REG_SZ /d "URL:FineDesk Protocol" /f >NUL 2>&1
reg add "HKCU\Software\Classes\finedesk" /v "URL Protocol" /t REG_SZ /d "" /f >NUL 2>&1
reg add "HKCU\Software\Classes\finedesk\shell\open\command" /ve /t REG_SZ /d "\"%FDEXE%\" \"%%1\"" /f >NUL 2>&1

echo.
echo Verifying...
if exist "%DESKTOP%\FineDesk.lnk" (echo [OK] Desktop shortcut) else (echo [FAIL] Desktop shortcut)
if exist "%STARTUP%\FineDesk.lnk" (echo [OK] Startup shortcut) else (echo [FAIL] Startup shortcut)
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v FineDesk >NUL 2>&1 && echo [OK] Registry Run key || echo [FAIL] Registry Run key
schtasks /Query /TN "FineDesk" >NUL 2>&1 && echo [OK] Scheduled task || echo [FAIL] Scheduled task
reg query "HKCU\Software\Classes\finedesk" >NUL 2>&1 && echo [OK] URI scheme (finedesk://) || echo [FAIL] URI scheme

echo.
echo Launching FineDesk...
start "" "%FDEXE%"
exit
