@echo off
setlocal enabledelayedexpansion
title rird.ai AI pilot - installer
echo.
echo   rird.ai  -  Hermes 2  (AI pilot for flight sim)
echo   ---------------------------------
echo.
set /p EMAIL=  Email: 
set /p PASS=   Password: 
echo.
echo   Checking your account...
set "BODY={\"email\":\"%EMAIL%\",\"password\":\"%PASS%\",\"device\":\"%COMPUTERNAME%-%USERNAME%\"}"
> "%TEMP%\rird_prov.json" echo %BODY%
for /f "delims=" %%R in ('powershell -NoProfile -Command "try { (Invoke-RestMethod -Method POST -Uri https://rird.ai/api/provision -ContentType 'application/json' -InFile '%TEMP%\rird_prov.json' -TimeoutSec 25) | ConvertTo-Json -Compress } catch { $_.ErrorDetails.Message }"') do set "RESP=%%R"
echo !RESP! | findstr /i "\"ok\":true" >nul
if errorlevel 1 (
  echo.
  echo   Not yet: !RESP!
  echo   Manage it at https://rird.ai/account
  echo.
  pause
  exit /b 1
)
for /f "delims=" %%K in ('powershell -NoProfile -Command "((Get-Content '%TEMP%\rird_prov.json' -Raw) | ConvertFrom-Json) | Out-Null; (Get-Content '%TEMP%\rird_prov.json' -Raw)"') do set "J=%%K"
for /f "delims=" %%K in ('powershell -NoProfile -Command "$j=Get-Content '%TEMP%\rird_prov.json' -Raw; $o=[regex]::Match($j,'\"key\":\"([^\"]+)\"').Groups[1].Value; $o"') do set "KEY=%%K"
for /f "delims=" %%K in ('powershell -NoProfile -Command "$j=Get-Content '%TEMP%\rird_prov.json' -Raw; $o=[regex]::Match($j,'\"aircraft\":\"([^\"]+)\"').Groups[1].Value; $o"') do set "AC=%%K"
del "%TEMP%\rird_prov.json" >nul 2>&1

echo   Account ok. Downloading the client...
set "DST=%LOCALAPPDATA%\rird"
if not exist "%DST%" mkdir "%DST%"
powershell -NoProfile -Command "Invoke-WebRequest -Uri https://rird.ai/download/rird-client.zip -OutFile '%DST%\client.zip' -UseBasicParsing"
if not exist "%DST%\client.zip" (
  echo   Download failed. Try again, or email contact@rird.ai
  pause
  exit /b 1
)
echo   Unpacking Hermes 2...
powershell -NoProfile -Command "Expand-Archive -Force -Path '%DST%\client.zip' -DestinationPath '%DST%'"
> "%DST%\config.json" echo {"key":"!KEY!","aircraft":"!AC!","api":"https://rird.ai"}
echo   Installing rird.ai...
if exist "%DST%\rird-install.ps1" powershell -NoProfile -ExecutionPolicy Bypass -File "%DST%\rird-install.ps1"
echo.
echo   Writing your licence...
powershell -NoProfile -Command "$p=Join-Path $env:LOCALAPPDATA 'rird\config.json'; $k=Get-Content $p -Raw | ConvertFrom-Json; $t=Join-Path $env:USERPROFILE '.rird'; New-Item -ItemType Directory -Force -Path $t | Out-Null; $k | ConvertTo-Json | Set-Content (Join-Path $t 'license.json')"
echo   Done. Starting the pilot with the !AC!.
if exist "%DST%\rird-launch.ps1" (
  start "" powershell -NoProfile -ExecutionPolicy Bypass -File "%DST%\rird-launch.ps1"
) else (
  start "" /min powershell -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "%USERPROFILE%\Desktop\hermes-v2\toggle-hermes2.ps1"
)
echo.
echo   Your key: !KEY!   (keep it - it is also your sign-in)
timeout /t 8 >nul
