@echo off
rem ===========================================================================
rem  RIOJoy cockpit post-install entry point (run by TeslaConsole after the
rem  package is extracted into C:\games). Sits beside the 'RIOJoy' payload
rem  folder; elevates, runs the shared install core (which picks the XP or
rem  modern flavor), then removes the standalone-install files so only
rem  launcher-managed files stay at the C:\games root.
rem ===========================================================================
setlocal
title RIOJoy post-install

rem --- elevate if we are not already administrator --------------------------
net session >nul 2>&1
if %errorlevel% neq 0 (
    rem "if errorlevel" evaluates at run time (a %var% here would be stale).
    ver | findstr /C:"Version 5.1" >nul
    if not errorlevel 1 (
        echo This installer must run as an Administrator user.
        exit /b 1
    )
    echo Requesting administrator privileges...
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b 0
)

cd /d "%~dp0"

call "%~dp0RIOJoy\install-core.bat"
set RC=%errorlevel%

rem --- cleanup: cabinet deploys keep the C:\games root clean ----------------
rem (del tolerates already-missing files, so re-runs stay idempotent)
if exist "%~dp0install.bat" del /q "%~dp0install.bat"
if exist "%~dp0README.txt"  del /q "%~dp0README.txt"

echo.
if %RC% neq 0 (
    echo postinstall FAILED with exit code %RC%.
) else (
    echo postinstall completed.
)
exit /b %RC%
