@echo off
REM =========================================================================
REM uninstall.bat — Remove Norge Replay
REM
REM Removes application files, shortcuts, registry entries, and cache.
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"

echo.
echo ===== Norge Replay Uninstaller =====
echo.
echo This will remove %APP_NAME% from:
echo   %INSTALL_DIR%
echo.

REM --- Check for cached videos ---
set "HAS_CACHE=0"
if exist "%INSTALL_DIR%\cache" set "HAS_CACHE=1"

if "%HAS_CACHE%"=="1" (
    echo NOTE: A video cache folder exists at:
    echo   %INSTALL_DIR%\cache
    echo.
)

set /p "CONFIRM=Proceed with uninstall? [Y/N] "
if /i not "%CONFIRM%"=="Y" (
    echo Uninstall cancelled.
    pause
    exit /b 0
)

if "%HAS_CACHE%"=="1" (
    echo.
    set /p "DEL_CACHE=Also delete cached videos? [Y/N] "
)

echo.
echo Uninstalling...

REM --- Remove Start Menu shortcut ---
echo   Removing Start Menu shortcut...
if exist "%START_MENU%\Norge Replay.lnk" del /f "%START_MENU%\Norge Replay.lnk" >nul 2>&1

REM --- Remove Desktop shortcut ---
echo   Removing Desktop shortcut...
if exist "%DESKTOP%\Norge Replay.lnk" del /f "%DESKTOP%\Norge Replay.lnk" >nul 2>&1

REM --- Remove registry entry ---
echo   Removing Apps ^& Features entry...
reg delete "%UNINSTALL_KEY%" /f >nul 2>&1

REM --- Remove cache if requested ---
if "%HAS_CACHE%"=="1" (
    if /i "%DEL_CACHE%"=="Y" (
        echo   Removing cached videos and settings...
        rmdir /s /q "%INSTALL_DIR%\cache" >nul 2>&1
    ) else (
        echo   Keeping cached videos at: %INSTALL_DIR%\cache
    )
)

REM --- Remove application files ---
echo   Removing application files...
del /f "%INSTALL_DIR%\NorgeReplay.exe" >nul 2>&1
del /f "%INSTALL_DIR%\fileviewer.exe"  >nul 2>&1
del /f "%INSTALL_DIR%\SDL3.dll"        >nul 2>&1
del /f "%INSTALL_DIR%\NorgeLogo.png"   >nul 2>&1
del /f "%INSTALL_DIR%\numbers.txt"     >nul 2>&1

REM --- Remove help documentation ---
if exist "%INSTALL_DIR%\doc\help" rmdir /s /q "%INSTALL_DIR%\doc\help" >nul 2>&1
if exist "%INSTALL_DIR%\doc"      rmdir /q "%INSTALL_DIR%\doc"         >nul 2>&1

REM --- Remove install directory (if empty or only uninstall.bat remains) ---
REM The uninstall.bat is currently running, so we use a self-deleting trick:
REM schedule deletion after this script exits.
echo   Cleaning up...

echo.
echo ===== Uninstall Complete =====
echo.
if "%HAS_CACHE%"=="1" (
    if /i not "%DEL_CACHE%"=="Y" (
        echo   Cached videos were preserved at:
        echo     %INSTALL_DIR%\cache
        echo.
    )
)
echo   %APP_NAME% has been removed.
echo.

pause

REM --- Self-delete: remove uninstall.bat and the directory ---
REM Uses cmd /c with a delay to delete after this script exits
start /b "" cmd /c "timeout /t 2 /nobreak >nul & del /f "%INSTALL_DIR%\uninstall.bat" >nul 2>&1 & rmdir /q "%INSTALL_DIR%" >nul 2>&1"
exit /b 0
