@echo off
rem ===========================================================================
rem  RIOJoy shared install core - called by postinstall.bat (cabinet /
rem  TeslaConsole) and install.bat (freestanding). NOT run directly.
rem  Detects the OS and installs the matching flavor's prerequisites:
rem    Windows XP (5.1)   -> .NET 4.0 + KB2468871 from RIOJoy\vendor\xp,
rem                          RioGamepadXP driver INF when bundled;
rem                          app = RIOJoy\app-xp (net40 x86)
rem    Windows 10/11      -> RIOJoy\install-rio.ps1 (ViGEmBus etc.);
rem                          app = RIOJoy\app (net48 x64)
rem  Pure cmd.exe on the XP path (XP has no in-box PowerShell). Idempotent.
rem  Sets RIOJOY_APPDIR for the caller (shortcut creation).
rem ===========================================================================
setlocal EnableExtensions
set CORE_RC=0
set PKGROOT=%~dp0
rem PKGROOT = ...\RIOJoy\ (this file lives inside the payload folder)

ver | findstr /C:"Version 5.1" >nul
if %errorlevel% equ 0 goto xp

rem --- Windows 10/11 ---------------------------------------------------------
echo Detected modern Windows - installing the net48 x64 flavor prerequisites.
powershell -NoProfile -ExecutionPolicy Bypass -File "%PKGROOT%install-rio.ps1"
set CORE_RC=%errorlevel%
endlocal & set "RIOJOY_APPDIR=%~dp0app" & exit /b %CORE_RC%

:xp
rem --- Windows XP SP3 ---------------------------------------------------------
echo Detected Windows XP - installing the net40 x86 flavor prerequisites.

rem .NET Framework 4.0 (XP's ceiling; required by the app).
reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v Install 2>nul | findstr /C:"0x1" >nul
if %errorlevel% equ 0 (
    echo .NET Framework 4.0 already installed.
) else (
    if exist "%PKGROOT%vendor\xp\dotNetFx40_Full_x86_x64.exe" (
        echo Installing .NET Framework 4.0 - this takes several minutes...
        "%PKGROOT%vendor\xp\dotNetFx40_Full_x86_x64.exe" /q /norestart
        if errorlevel 3011 echo .NET install requests a reboot - reboot before first use.
    ) else (
        echo ERROR: vendor\xp\dotNetFx40_Full_x86_x64.exe is missing from the package.
        set CORE_RC=1
        goto xpdone
    )
)

rem KB2468871 - required by the async runtime (Microsoft.Bcl.Async).
rem The update no-ops quickly when already applied, so run unconditionally.
if exist "%PKGROOT%vendor\xp\NDP40-KB2468871-v2-x86.exe" (
    echo Applying .NET 4.0 update KB2468871...
    "%PKGROOT%vendor\xp\NDP40-KB2468871-v2-x86.exe" /q /norestart
) else (
    echo WARNING: vendor\xp\NDP40-KB2468871-v2-x86.exe not bundled - the app
    echo          will not run without KB2468871. Install it before first use.
)

rem RioGamepadXP virtual-joystick driver. Root-enumerated, so it must be
rem created with devcon (rundll32/InstallHinfSection would not make the
rem devnode). devcon is idempotent: re-running "install" updates in place.
if exist "%PKGROOT%vendor\xp\RioGamepadXP.inf" (
    if exist "%PKGROOT%vendor\xp\devcon.exe" (
        echo Installing the RioGamepadXP virtual joystick driver...
        "%PKGROOT%vendor\xp\devcon.exe" install "%PKGROOT%vendor\xp\RioGamepadXP.inf" root\RioGamepadXP
        if errorlevel 1 echo WARNING: driver install returned an error - check Device Manager.
    ) else (
        echo WARNING: vendor\xp\devcon.exe missing - cannot create the driver devnode.
    )
) else (
    echo Note: RioGamepadXP driver not bundled - joystick output is
    echo       unavailable on XP; keyboard/mouse, lamps and plasma still work.
)

:xpdone
endlocal & set "RIOJOY_APPDIR=%~dp0app-xp" & exit /b %CORE_RC%
