@echo off
setlocal enabledelayedexpansion
rem xrsim setup for Windows — installs Node.js (if needed) and xrsim itself.
rem
rem Two ways to run this:
rem   1. Double-click it in Explorer.
rem   2. From PowerShell:
rem      irm https://unpkg.com/xrsim@latest/install/install-windows.bat -OutFile install-xrsim.bat; .\install-xrsim.bat

echo ==================================
echo  xrsim setup
echo ==================================
echo.

where node >nul 2>nul
if %errorlevel% equ 0 (
    for /f %%V in ('node -p "process.versions.node.split('.')[0]"') do set NODE_MAJOR=%%V
    if !NODE_MAJOR! geq 18 (
        for /f "delims=" %%V in ('node -v') do echo Node.js %%V found - that's all xrsim needs to install.
        goto :install_xrsim
    ) else (
        for /f "delims=" %%V in ('node -v') do echo Found Node.js %%V, but xrsim needs 18 or newer.
    )
) else (
    echo Node.js not found.
)

echo.
echo Installing Node.js...
where winget >nul 2>nul
if %errorlevel% neq 0 (
    echo.
    echo Couldn't find winget to install Node.js automatically ^(needs Windows 10 21H2+^).
    echo Please install it yourself - https://nodejs.org, the LTS version - then run this
    echo script again.
    echo.
    pause
    exit /b 1
)

winget install --id OpenJS.NodeJS.LTS --silent --accept-package-agreements --accept-source-agreements
if %errorlevel% neq 0 (
    echo.
    echo Node.js install didn't complete. If a prompt appeared asking for permission and
    echo you dismissed it, try again and accept it. Otherwise install it yourself from
    echo https://nodejs.org and run this script again.
    echo.
    pause
    exit /b 1
)

echo.
echo Node.js is installed, but this window can't see it yet - Windows only updates PATH
echo for NEW windows. Close this one and run the script again to finish installing xrsim.
echo.
pause
exit /b 0

:install_xrsim
echo.
echo Installing xrsim...
rem --allow-scripts=xrsim: without it, some npm setups silently skip the postinstall step
rem that fetches xrsim's bundled Chromium - xrsim would install fine but "xrsim demo" would
rem then fail on first run with no browser to launch. Falls back to a plain install for an
rem npm old enough not to recognize the flag (which also predates the gating it works around).
call npm install -g xrsim --allow-scripts=xrsim
if %errorlevel% neq 0 (
    call npm install -g xrsim
)
if %errorlevel% neq 0 (
    echo.
    echo xrsim install failed. Try right-clicking this file and choosing
    echo "Run as administrator", then run it again.
    echo.
    pause
    exit /b 1
)

echo.
echo ==================================
echo  Done - xrsim is installed
echo ==================================
call xrsim version
echo.

rem A Desktop shortcut means the student never needs this installer, or a command line, again -
rem double-click it any time to launch xrsim. Points at a NEW cmd.exe process (not this session),
rem so it always sees a current PATH even for the very next launch. /k (not /c) keeps the window
rem open afterward so the on-screen instructions from `xrsim demo` (the cockpit URL, WASD control
rem hints) stay readable instead of flashing shut the instant the command returns.
echo Adding a desktop shortcut so you can launch xrsim any time...
powershell -NoProfile -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%USERPROFILE%\Desktop\xrsim.lnk'); $s.TargetPath='%ComSpec%'; $s.Arguments='/k xrsim demo'; $s.WorkingDirectory='%USERPROFILE%'; $s.Description='Launch xrsim'; $s.Save()" >nul 2>nul
if %errorlevel% equ 0 (
    echo Done - look for "xrsim" on your Desktop next time.
) else (
    echo Couldn't add a desktop shortcut automatically - not a problem, "xrsim demo" from a
    echo Command Prompt still works any time.
)

echo.
echo Press any key to launch xrsim now...
pause >nul
rem `xrsim demo`'s own output already points to `xrsim setup` (what a real Pico/Android app
rem needs) and `xrsim tutorial` (a guided walkthrough) - no need to repeat either here.
call xrsim demo

echo.
echo You can close this window now.
pause >nul
