@echo off
setlocal enabledelayedexpansion
title Install Track Yeah!

rem ============================================================================
rem  Track Yeah! installer
rem
rem  Plain Windows batch on purpose. No PowerShell, no execution policy, no
rem  administrator rights, nothing to allow. Somebody who has never opened a
rem  command prompt should be able to double-click this and be finished.
rem
rem  Everything goes in one folder under the user's own AppData, so it needs no
rem  permissions and uninstalling is deleting a folder.
rem
rem  UPGRADES: the marker is the DATA folder, not the root. If it is there, this
rem  is an upgrade and the data, the backups and the browser profile are left
rem  completely alone. An installer that treats every run as a first install is
rem  an installer that wipes somebody's financial history the day they update.
rem ============================================================================

set "ROOT=%LOCALAPPDATA%\TrackYeah"
set "APPDIR=%ROOT%\app"
set "DATADIR=%ROOT%\data"
set "BAKDIR=%ROOT%\backups"
set "PROFDIR=%ROOT%\browser-profile"
set "SRC=%~dp0trackyeah.html"

echo.
echo   ============================================
echo     Track Yeah!  -  setting up on this computer
echo   ============================================
echo.

if not exist "%SRC%" (
  echo   PROBLEM: trackyeah.html is not in this folder.
  echo.
  echo   This installer and trackyeah.html have to sit side by side.
  echo   If you unzipped a download, make sure you unzipped ALL of it,
  echo   then run this again from inside that folder.
  echo.
  pause
  exit /b 1
)

rem --- fresh install or upgrade? ---------------------------------------------
if exist "%DATADIR%" (
  set "MODE=upgrade"
  echo   Track Yeah! is already installed here.
  echo   Updating the program. Your figures and backups will not be touched.
) else (
  set "MODE=fresh"
  echo   Setting up for the first time.
)
echo.

for %%D in ("%ROOT%" "%APPDIR%" "%DATADIR%" "%BAKDIR%" "%PROFDIR%") do (
  if not exist %%D (
    mkdir %%D 2>nul
    echo   created  %%~D
  ) else (
    echo   kept     %%~D
  )
)
echo.

rem --- keep the last three versions, oldest dropped ---------------------------
rem  Rotating by number rather than by date: the date format in a batch file
rem  depends on Windows regional settings, so a timestamped filename works on
rem  one machine and produces nonsense on another.
if exist "%APPDIR%\trackyeah.html" (
  if exist "%BAKDIR%\trackyeah-previous-3.html" del /q "%BAKDIR%\trackyeah-previous-3.html"
  if exist "%BAKDIR%\trackyeah-previous-2.html" move /y "%BAKDIR%\trackyeah-previous-2.html" "%BAKDIR%\trackyeah-previous-3.html" >nul
  if exist "%BAKDIR%\trackyeah-previous-1.html" move /y "%BAKDIR%\trackyeah-previous-1.html" "%BAKDIR%\trackyeah-previous-2.html" >nul
  move /y "%APPDIR%\trackyeah.html" "%BAKDIR%\trackyeah-previous-1.html" >nul
  echo   The version you were using is kept in backups, in case you want it back.
)

copy /y "%SRC%" "%APPDIR%\trackyeah.html" >nul
if errorlevel 1 (
  echo   PROBLEM: could not copy the program into place.
  echo   Close Track Yeah! if it is open, then run this again.
  pause
  exit /b 1
)
echo   Program installed.
echo.

rem --- find a browser ---------------------------------------------------------
set "BROWSER="
for %%P in (
  "%ProgramFiles%\Google\Chrome\Application\chrome.exe"
  "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"
  "%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe"
  "%ProgramFiles%\Microsoft\Edge\Application\msedge.exe"
  "%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"
) do (
  if not defined BROWSER if exist %%P set "BROWSER=%%~P"
)

rem --- the launcher -----------------------------------------------------------
rem  A separate browser profile with extensions turned off. That profile is the
rem  only thing that actually stops a browser add-on reading the page, and it
rem  also means clearing your normal browsing data cannot wipe your figures.
set "URL=file:///%APPDIR:\=/%/trackyeah.html"
set "LAUNCH=%ROOT%\Track Yeah.cmd"

> "%LAUNCH%" echo @echo off
>> "%LAUNCH%" echo rem Opens Track Yeah! in its own browser window.
>> "%LAUNCH%" echo set "B=%BROWSER%"
>> "%LAUNCH%" echo if not exist "%%B%%" (
>> "%LAUNCH%" echo   start "" "%APPDIR%\trackyeah.html"
>> "%LAUNCH%" echo   exit /b 0
>> "%LAUNCH%" echo ^)
>> "%LAUNCH%" echo start "" "%%B%%" --app="%URL%" --user-data-dir="%PROFDIR%" --disable-extensions

if defined BROWSER (
  echo   Found your browser: %BROWSER%
) else (
  echo   No Chrome or Edge found - Track Yeah! will open in whatever
  echo   browser Windows normally uses. It still works; it just shares
  echo   that browser's add-ons.
)
echo.

rem --- put it where they will find it -----------------------------------------
rem  A .cmd copied to the Desktop rather than a proper .lnk shortcut: making a
rem  real shortcut needs PowerShell or VBScript, and both of those either warn
rem  the user or are on their way out of Windows. This just works.
set "DESK=%USERPROFILE%\Desktop"
if exist "%OneDrive%\Desktop" set "DESK=%OneDrive%\Desktop"
copy /y "%LAUNCH%" "%DESK%\Track Yeah.cmd" >nul 2>&1
if exist "%DESK%\Track Yeah.cmd" (
  echo   Put "Track Yeah" on your desktop.
) else (
  echo   Could not write to the desktop. Open it from here instead:
  echo     %LAUNCH%
)

set "STARTM=%APPDATA%\Microsoft\Windows\Start Menu\Programs"
if exist "%STARTM%" copy /y "%LAUNCH%" "%STARTM%\Track Yeah.cmd" >nul 2>&1

rem --- a note for later -------------------------------------------------------
> "%ROOT%\README.txt" echo Track Yeah!
>> "%ROOT%\README.txt" echo.
>> "%ROOT%\README.txt" echo   app\              the program itself
>> "%ROOT%\README.txt" echo   backups\          your backups, and the last few versions of the program
>> "%ROOT%\README.txt" echo   browser-profile\  where your figures actually live
>> "%ROOT%\README.txt" echo   data\             used only if you pick the file option in Settings
>> "%ROOT%\README.txt" echo.
>> "%ROOT%\README.txt" echo TO OPEN IT: use the Track Yeah icon on your desktop.
>> "%ROOT%\README.txt" echo.
>> "%ROOT%\README.txt" echo TO UPDATE IT: run the installer again over the top. Your figures
>> "%ROOT%\README.txt" echo and backups are left alone.
>> "%ROOT%\README.txt" echo.
>> "%ROOT%\README.txt" echo TO REMOVE IT: delete this whole folder. Nothing is stored anywhere
>> "%ROOT%\README.txt" echo else on your computer. Deleting it deletes your figures too, so use
>> "%ROOT%\README.txt" echo Backup inside the app first if you want to keep them.
>> "%ROOT%\README.txt" echo.
>> "%ROOT%\README.txt" echo Your figures never leave this computer. There is no account, no
>> "%ROOT%\README.txt" echo sign-in and no server.

echo.
echo   ============================================
if "%MODE%"=="upgrade" (
  echo     Updated. Your figures and backups are as they were.
) else (
  echo     All done. Open "Track Yeah" from your desktop.
)
echo   ============================================
echo.
echo   Press any key to close this window.
pause >nul
endlocal
