@echo off
REM =========================================================================
REM install.bat — Install Norge Replay
REM
REM Installs to %LOCALAPPDATA%\Programs\NorgeReplay (no admin required)
REM Creates Start Menu and Desktop shortcuts
REM Registers in Windows Apps & Features for easy removal
REM =========================================================================

setlocal enabledelayedexpansion

set "APP_NAME=Norge Replay"
set "INSTALL_DIR=%LOCALAPPDATA%\Programs\NorgeReplay"
set "START_MENU=%APPDATA%\Microsoft\Windows\Start Menu\Programs"
set "DESKTOP=%USERPROFILE%\Desktop"
set "UNINSTALL_KEY=HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\NorgeReplay"
set "SOURCE_DIR=%~dp0"

echo.
echo ===== Norge Replay Installer =====
echo.
echo This will install %APP_NAME% to:
echo   %INSTALL_DIR%
echo.

REM --- Confirm installation ---
set /p "CONFIRM=Proceed with installation? [Y/N] "
if /i not "%CONFIRM%"=="Y" (
    echo Installation cancelled.
    exit /b 0
)

echo.
echo Installing...

REM --- Create installation directory ---
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"
if not exist "%INSTALL_DIR%\doc\help" mkdir "%INSTALL_DIR%\doc\help"

REM --- Copy application files ---
echo   Copying application files...
copy /y "%SOURCE_DIR%NorgeReplay.exe"  "%INSTALL_DIR%\" >nul
copy /y "%SOURCE_DIR%fileviewer.exe"   "%INSTALL_DIR%\" >nul
copy /y "%SOURCE_DIR%SDL3.dll"         "%INSTALL_DIR%\" >nul
copy /y "%SOURCE_DIR%NorgeLogo.png"    "%INSTALL_DIR%\" >nul
copy /y "%SOURCE_DIR%numbers.txt"      "%INSTALL_DIR%\" >nul

REM --- Copy help documentation ---
echo   Copying help files...
xcopy /y /q "%SOURCE_DIR%doc\help\*" "%INSTALL_DIR%\doc\help\" >nul

REM --- Copy uninstaller ---
copy /y "%SOURCE_DIR%uninstall.bat" "%INSTALL_DIR%\" >nul

REM --- Create Start Menu shortcut ---
echo   Creating Start Menu shortcut...
powershell -NoProfile -Command ^
    "$ws = New-Object -ComObject WScript.Shell; " ^
    "$sc = $ws.CreateShortcut('%START_MENU%\Norge Replay.lnk'); " ^
    "$sc.TargetPath = '%INSTALL_DIR%\NorgeReplay.exe'; " ^
    "$sc.WorkingDirectory = '%INSTALL_DIR%'; " ^
    "$sc.Description = 'Norge Replay - Tournament Video Viewer'; " ^
    "$sc.Save()"

REM --- Create Desktop shortcut ---
echo   Creating Desktop shortcut...
powershell -NoProfile -Command ^
    "$ws = New-Object -ComObject WScript.Shell; " ^
    "$sc = $ws.CreateShortcut('%DESKTOP%\Norge Replay.lnk'); " ^
    "$sc.TargetPath = '%INSTALL_DIR%\NorgeReplay.exe'; " ^
    "$sc.WorkingDirectory = '%INSTALL_DIR%'; " ^
    "$sc.Description = 'Norge Replay - Tournament Video Viewer'; " ^
    "$sc.Save()"

REM --- Register in Apps & Features (Add/Remove Programs) ---
echo   Registering in Apps ^& Features...
reg add "%UNINSTALL_KEY%" /v "DisplayName"     /t REG_SZ /d "%APP_NAME%" /f >nul 2>&1
reg add "%UNINSTALL_KEY%" /v "UninstallString" /t REG_SZ /d "\"%INSTALL_DIR%\uninstall.bat\"" /f >nul 2>&1
reg add "%UNINSTALL_KEY%" /v "InstallLocation" /t REG_SZ /d "%INSTALL_DIR%" /f >nul 2>&1
reg add "%UNINSTALL_KEY%" /v "Publisher"       /t REG_SZ /d "Norge Ski Club" /f >nul 2>&1
reg add "%UNINSTALL_KEY%" /v "DisplayVersion"  /t REG_SZ /d "1.0" /f >nul 2>&1
reg add "%UNINSTALL_KEY%" /v "NoModify"        /t REG_DWORD /d 1 /f >nul 2>&1
reg add "%UNINSTALL_KEY%" /v "NoRepair"        /t REG_DWORD /d 1 /f >nul 2>&1

REM --- Calculate installed size (in KB for registry) ---
for /f "tokens=3" %%A in ('dir /s /a "%INSTALL_DIR%" 2^>nul ^| find "File(s)"') do set "TOTAL_BYTES=%%A"
set "TOTAL_BYTES=%TOTAL_BYTES:,=%"
set /a "SIZE_KB=%TOTAL_BYTES% / 1024" 2>nul
if defined SIZE_KB (
    reg add "%UNINSTALL_KEY%" /v "EstimatedSize" /t REG_DWORD /d %SIZE_KB% /f >nul 2>&1
)

echo.
echo ===== Installation Complete =====
echo.
echo   Installed to: %INSTALL_DIR%
echo   Start Menu:   %START_MENU%\Norge Replay.lnk
echo   Desktop:      %DESKTOP%\Norge Replay.lnk
echo.
echo   To uninstall, use Apps ^& Features in Windows Settings,
echo   or run: "%INSTALL_DIR%\uninstall.bat"
echo.

pause
