@echo off
:: =============================================================================
:: TeslaLauncher Modern — Installation Script
:: =============================================================================
:: Run as Administrator on each cockpit PC.
:: Must be run from the TeslaLauncher\ folder produced by build.bat:
::
::   TeslaLauncher\
::     install.bat        <- this file
::     Service\
::       TeslaLauncherService.exe
::     Agent\
::       TeslaLauncherAgent.exe
::     oalinst.exe
::     dx9201006\
::       DXSETUP.exe
:: =============================================================================

setlocal enabledelayedexpansion

echo ============================================================
echo  Tesla Launcher v0.1 - Installation
echo ============================================================
echo.

:: ── Paths ─────────────────────────────────────────────────────────────────────
set ROOT=%~dp0
set SERVICE_SRC=%ROOT%Service
set AGENT_SRC=%ROOT%Agent
set INSTALL_DIR=C:\Program Files\TeslaLauncher
set DATA_DIR=C:\ProgramData\TeslaLauncher
set SERVICE_EXE=%INSTALL_DIR%\TeslaLauncherService.exe
set AGENT_EXE=%INSTALL_DIR%\TeslaLauncherAgent.exe
set SERVICE_NAME=Tesla Application Launcher
set SERVICE_DISPLAY=Tesla Application Launcher
set CONSOLE_PORT=53290

:: ── Admin check ───────────────────────────────────────────────────────────────
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo ERROR: This script must be run as Administrator.
    pause & exit /b 1
)

:: ── Verify source files ───────────────────────────────────────────────────────
if not exist "%SERVICE_SRC%\TeslaLauncherService.exe" (
    echo ERROR: TeslaLauncherService.exe not found in %SERVICE_SRC%
    echo        Run build.bat first, then run install.bat from the TeslaLauncher\ folder.
    pause & exit /b 1
)
if not exist "%AGENT_SRC%\TeslaLauncherAgent.exe" (
    echo ERROR: TeslaLauncherAgent.exe not found in %AGENT_SRC%
    echo        Run build.bat first, then run install.bat from the TeslaLauncher\ folder.
    pause & exit /b 1
)

:: ── Detect and clean up existing installation ─────────────────────────────────
set EXISTING=0
if exist "%SERVICE_EXE%" set EXISTING=1
if exist "%AGENT_EXE%"   set EXISTING=1

if "%EXISTING%"=="1" (
    echo Existing installation detected.  Cleaning up...
    echo.

    :: Stop and kill the Agent process
    taskkill /F /IM TeslaLauncherAgent.exe >nul 2>&1
    echo       Agent process stopped.

    :: Stop and remove the service, wait for it to fully stop
    sc stop "%SERVICE_NAME%" >nul 2>&1
    sc stop "Tesla Application Launcher" >nul 2>&1
    timeout /t 3 /nobreak >nul
    sc delete "%SERVICE_NAME%" >nul 2>&1
    sc delete "Tesla Application Launcher" >nul 2>&1
    timeout /t 2 /nobreak >nul
    echo       Service stopped and removed.

    :: Delete old executables, logs, and session key so SecureConfig re-runs
    del /F /Q "%SERVICE_EXE%" >nul 2>&1
    del /F /Q "%AGENT_EXE%"   >nul 2>&1
    del /F /Q "%INSTALL_DIR%\podconf.log" >nul 2>&1
    del /F /Q "%DATA_DIR%\TeslaKeyStore.key" >nul 2>&1
    del /F /Q "%DATA_DIR%\LaunchApps.xml"      >nul 2>&1
    del /F /Q "%DATA_DIR%\configuring.json"  >nul 2>&1
    echo       Old executables, logs, session key, and app config deleted.

    :: Reset all physical Ethernet adapters to DHCP so the SecureConfig
    :: protocol triggers correctly on the next boot.
    :: We must REMOVE the static IP address entry first, then enable DHCP.
    :: Set-NetIPInterface -Dhcp Enabled alone only flips the DHCP flag but
    :: leaves the static address in the registry, so IsMachineConfigured()
    :: still sees a non-DHCP adapter on the next boot and skips SecureConfig.
    echo       Resetting network adapters to DHCP...
    powershell -NoProfile -Command "Get-NetAdapter -Physical | Where-Object {$_.Status -eq 'Up'} | ForEach-Object { $n=$_.Name; Get-NetIPAddress -InterfaceAlias $n -AddressFamily IPv4 -EA 0 | Where-Object {$_.PrefixOrigin -ne 'WellKnown' -and $_.PrefixOrigin -ne 'Dhcp'} | Remove-NetIPAddress -Confirm:$false -EA 0; Set-NetIPInterface -InterfaceAlias $n -Dhcp Enabled -EA 0 }; Get-NetAdapter -Physical | Where-Object {$_.Status -eq 'Up'} | Set-DnsClientServerAddress -ResetServerAddresses" >nul 2>&1
    echo       Network adapters reset to DHCP.
    echo.
) else (
    :: No existing install — still remove any stale service registration
    sc stop "%SERVICE_NAME%"          >nul 2>&1
    sc stop "Tesla Application Launcher" >nul 2>&1
    sc delete "%SERVICE_NAME%"        >nul 2>&1
    sc delete "Tesla Application Launcher" >nul 2>&1
    timeout /t 2 /nobreak >nul
)

:: ── STEP 1: Create directories ────────────────────────────────────────────────
echo [1/8] Creating directories...
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"
if not exist "%DATA_DIR%"    mkdir "%DATA_DIR%"
if not exist "C:\Games"      mkdir "C:\Games"

:: Grant all users modify access to the data and games directories so
:: the Service and Agent can write files (LaunchApps.xml, game installs).
icacls "%DATA_DIR%" /grant *S-1-5-32-545:(OI)(CI)M /T >nul 2>&1
icacls "C:\Games"   /grant *S-1-5-32-545:(OI)(CI)M /T >nul 2>&1
echo       %INSTALL_DIR%
echo       %DATA_DIR% (Users: modify access)
echo       C:\Games   (Users: modify access)

:: ── STEP 2: Copy files ────────────────────────────────────────────────────────
echo [2/8] Copying files...
:: net48 framework-dependent build: each folder holds the exe + its dependency
:: DLLs + .config. Copy both folders into the install dir (shared DLLs overwrite
:: identically; the two .exe.config files coexist).
copy /Y "%SERVICE_SRC%\*.*" "%INSTALL_DIR%\" >nul
copy /Y "%AGENT_SRC%\*.*"   "%INSTALL_DIR%\" >nul

:: Install DirectX June 2010 runtime (required by games)
if exist "%ROOT%\dx9201006\DXSETUP.exe" (
    echo       Installing DirectX runtime...
    "%ROOT%\dx9201006\DXSETUP.exe" /silent
    echo       DirectX installed.
)

:: Install OpenAL runtime (required by game audio)
if exist "%ROOT%oalinst.exe" (
    echo       Installing OpenAL runtime...
    "%ROOT%oalinst.exe" /s
    echo       OpenAL installed.
)

:: Enable SMB1 client (required by game networking)
echo       Enabling SMB1 file sharing...
dism /Online /Enable-Feature /FeatureName:SMB1Protocol /All /NoRestart >nul 2>&1
echo       SMB1 enabled (reboot required to take effect).

:: Enable DirectPlay (required by older games)
echo       Enabling DirectPlay...
dism /Online /Enable-Feature /FeatureName:DirectPlay /All /NoRestart >nul 2>&1
echo       DirectPlay enabled.
echo       Done.

:: ── STEP 3: Install Windows Service ──────────────────────────────────────────
echo [3/8] Installing Windows Service...
sc create "%SERVICE_NAME%" ^
    binPath= "\"%SERVICE_EXE%\"" ^
    DisplayName= "%SERVICE_DISPLAY%" ^
    start= delayed-auto ^
    obj= LocalSystem ^
    type= own

if %errorlevel% neq 0 (
    echo ERROR: Failed to register Windows Service.
    pause & exit /b 1
)

sc description "%SERVICE_NAME%" ^
    "Tesla Application Launcher - accepts TeslaConsole connections and controls simulation software"
sc failure "%SERVICE_NAME%" ^
    reset= 86400 actions= restart/5000/restart/10000/restart/30000

:: Delay auto-start by 20 seconds to let the network stack settle
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Tesla Application Launcher" ^
    /v AutoStartDelay /t REG_DWORD /d 20000 /f >nul

:: Set the global delayed-auto-start delay to 20 seconds (default is 120s)
reg add "HKLM\SYSTEM\CurrentControlSet\Control" ^
    /v AutoStartDelay /t REG_DWORD /d 20000 /f >nul

echo       Service registered: "%SERVICE_NAME%" (20s per-service delay, 20s global delay)

:: ── STEP 4: Disable Windows Firewall (closed network) ────────────────────────
echo.
echo [4/8] Disabling Windows Firewall (closed network)...
:: Disable firewall on all profiles — pods run on a closed network
netsh advfirewall set allprofiles state off
echo       Windows Firewall disabled on all profiles.

:: ── STEP 5: Power settings (max performance, no sleep) ──────────────────────
echo.
echo [5/8] Configuring power settings...

:: Activate Ultimate Performance plan (hidden by default, GUID is well-known)
powercfg -duplicatescheme e9a42b02-d5df-448d-aa00-03f14749eb61 >nul 2>&1
for /f "tokens=4" %%g in ('powercfg -list ^| findstr /i "Ultimate"') do (
    powercfg -setactive %%g
)
echo       Ultimate Performance power plan activated.

:: Disable all sleep/hibernate/standby timeouts (AC power)
powercfg -change -standby-timeout-ac 0
powercfg -change -hibernate-timeout-ac 0
powercfg -change -monitor-timeout-ac 0
powercfg -change -disk-timeout-ac 0
powercfg -h off >nul 2>&1
echo       Sleep, hibernate, monitor timeout, and disk timeout disabled.

:: ── STEP 6: Disable notifications ───────────────────────────────────────────
echo.
echo [6/8] Disabling notifications...

:: Disable Action Center / notification center
reg add "HKCU\SOFTWARE\Policies\Microsoft\Windows\Explorer" ^
    /v DisableNotificationCenter /t REG_DWORD /d 1 /f >nul
:: Disable toast notifications
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\PushNotifications" ^
    /v ToastEnabled /t REG_DWORD /d 0 /f >nul
:: Disable Windows tips and suggestions
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" ^
    /v SubscribedContent-338389Enabled /t REG_DWORD /d 0 /f >nul
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" ^
    /v SubscribedContent-310093Enabled /t REG_DWORD /d 0 /f >nul
:: Disable "Get tips, tricks, and suggestions"
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" ^
    /v SoftLandingEnabled /t REG_DWORD /d 0 /f >nul
echo       Notifications and tips disabled.

:: ── STEP 7: Disable UAC ─────────────────────────────────────────────────────
echo.
echo [7/8] Disabling UAC...
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" ^
    /v EnableLUA /t REG_DWORD /d 0 /f >nul
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" ^
    /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f >nul
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" ^
    /v PromptOnSecureDesktop /t REG_DWORD /d 0 /f >nul
echo       UAC disabled (takes effect after reboot).

:: ── STEP 8: Configure Agent auto-start ───────────────────────────────────────
echo.
echo [8/8] Configuring Agent auto-start...

set AUTORUN_KEY=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
reg add "%AUTORUN_KEY%" /v "TeslaLauncherAgent" /t REG_SZ ^
    /d "\"%AGENT_EXE%\"" /f >nul

echo       Added TeslaLauncherAgent to HKLM\...\Run.
echo.
echo       IMPORTANT: This cockpit PC must be configured for auto-login.
echo       If not already set up, run netplwiz and uncheck
echo       "Users must enter a user name and password", or set:
echo         HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
echo           AutoAdminLogon  = 1
echo           DefaultUserName = ^<cockpit user^>
echo           DefaultPassword = ^<password^>

:: ── Done ──────────────────────────────────────────────────────────────────────
echo.
echo ============================================================
echo  Installation Complete!
echo ============================================================
echo.
echo  Installed to : %INSTALL_DIR%
echo  Config dir   : %DATA_DIR%
echo  Firewall     : disabled (closed network)
echo.
echo  Rebooting in 10 seconds...
echo    The Service and Agent will start automatically in the correct order.
echo    On first boot with DHCP, SecureConfig runs automatically —
echo    watch plasma or on main screen for the Request ID and Passphrase.
echo.
echo    After reboot: use Console to install apps.
echo.
shutdown /r /t 10
