rem -- Soletude's Update Scripts | CC0 | https://go.soletude.ca/supper --

rem This universal script can be ran during a fresh installation or an update
rem (attended or not). In any case, current user may be any of the 3: regular
rem user, admin, Local System. In portable installs, SUP_ADMIN == ""; in this
rem case don't perform some tasks (like /service:add).

setlocal

set PROGRAMID=Siris
rem All our project names begin with "S".
set PROGRAMIDL=%PROGRAMID:S=s%
set PROGRAMTITLE=Siris
set PROGRAMDESC=Siris - Universal Object-Oriented Graphics Editor
set FILEEXT=siris

rem goto :label - no need to return, will exit.
rem if "%1" == "/1" goto :1

echo %SUP_CL% | find "/fresh"
if not errorlevel 1 set FRESH=1

rem --- Add Sup service for elevation-free updates and /service:launch ---
Sup /service:add /service:remove

if not "%FRESH%" == "" (
  "%~dp0Supper" genid Sup.json id=

  rem --- Create shortcuts ---
  if "%SUP_ADMIN%" == "" (
    "%~dp0Supper" shortcut "%USERPROFILE%\Desktop\%PROGRAMTITLE%.lnk" "%CD%\%PROGRAMID%.exe" /desc "%PROGRAMDESC%"
    "%~dp0Supper" shortcut "%APPDATA%\Microsoft\Windows\Start Menu\%PROGRAMTITLE%.lnk" "%CD%\%PROGRAMID%.exe" /desc "%PROGRAMDESC%"
  ) else (
    "%~dp0Supper" shortcut "%PUBLIC%\Desktop\%PROGRAMTITLE%.lnk" "%CD%\%PROGRAMID%.exe" /desc "%PROGRAMDESC%"
    "%~dp0Supper" shortcut "%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\%PROGRAMTITLE%.lnk" "%CD%\%PROGRAMID%.exe" /desc "%PROGRAMDESC%"
  )

  rem --- Add an Uninstall entry to Control Panel ---
  rem The /archive file must be present in dist\ and must be signed.
  rem /archive path is relative to Sup.exe.
  Sup /uninstall:add ^
    /archive Uninstall.zip /uninstall:remove ^
    /info ^
      "S Publisher Soletude" ^
      "S HelpLink https://go.soletude.ca/%PROGRAMIDL%" ^
      "S URLInfoAbout https://go.soletude.ca/%PROGRAMIDL%/support" ^
      "S Contact %PROGRAMID%@soletude.ca" ^
      "S SettingsIdentifier Soletude\%PROGRAMID%;Soletude\Sup\%PROGRAMID%"

  rem --- Add file association ---
  if not "%FILEEXT%" == "" (
    rem Do not wrap in quotes - breaks assoc and ftype.
    assoc .%FILEEXT%=%PROGRAMID%
    ftype %PROGRAMID%="%CD%\%PROGRAMID%.exe" "%%1" %%*
  )
)

"%~dp0Supper" shortcut Uninstall.lnk "%CD%\Sup.exe" /arg "/uninstall /archive Uninstall.zip"

if exist "%~dp0extra.bat" call "%~dp0extra.bat"

rem --- Remove files from previous versions ---
rem Since Sup is keeping files present in ProgramPath but missing in the update
rem archive, it won't delete unneeded DLLs and such.
for /f "tokens=* skip=1 usebackq" %%l in ("%~dp0deleted.txt") do del /f "%%l"

rem --- Run the just installed or updated program ---
if "%SUP_HIDDEN%" == "" start "" Sup /pipe - "%CD%\%PROGRAMID%.exe"

rem --- Run ngen for all assemblies to speed up the program ---
rem Doing this "in background" (after launching the program) because it may
rem take several minutes and user doesn't have to wait.
rem PROCESSOR_ARCHITECTURE - "x86" or "AMD64".
set NGEN_DIR=%WINDIR%\Microsoft.NET\Framework
if /i %PROCESSOR_ARCHITECTURE% == AMD64 set NGEN_DIR=%NGEN_DIR%64
for /d %%f in ("%NGEN_DIR%"\v4.*) do set NGEN_DIR=%%f
for /r %%f in (*.dll) do "%NGEN_DIR%"\ngen install "%%f"

exit /b

rem --- Bits called from re-exec ---
rem *** Don't access SUP_... variables or others besides explicitly SET ones ***
rem :1
rem ...
rem exit /b
