@echo off
REM Run Windrose Server Manager as a console app (NOT as a Windows service).
REM Use this for first-time testing, debugging, or headless boxes that don't have WebView2.

setlocal

REM === Prereq 1: appsettings.Local.json must exist ===
if not exist "%~dp0appsettings.Local.json" (
  echo.
  echo ====================================================================
  echo  appsettings.Local.json not found.
  echo.
  echo  Copy one of these:
  echo    appsettings.Local.minimal.json   (5-line local-only config)
  echo    appsettings.Local.example.json   (full reference w/ Discord, hub)
  echo  to:
  echo    appsettings.Local.json
  echo  and set your RCON password.
  echo ====================================================================
  echo.
  pause
  exit /b 1
)

REM === Prereq 2: warn if it still has the placeholder password ===
findstr /C:"YOUR_RCON_PASSWORD_HERE" "%~dp0appsettings.Local.json" >nul
if %ERRORLEVEL% EQU 0 (
  echo.
  echo ====================================================================
  echo  appsettings.Local.json still has the placeholder RCON password.
  echo  Edit it: replace YOUR_RCON_PASSWORD_HERE with the real password
  echo  from your dedicated server's WindroseRCON engine.ini config.
  echo ====================================================================
  echo.
  pause
  exit /b 1
)

REM === Prereq 3: detect an existing instance to avoid the silent-flash issue ===
for /f "tokens=2 delims=," %%P in ('tasklist /FI "IMAGENAME eq WindroseServerManager.Web.exe" /FO CSV /NH 2^>nul ^| findstr /v "INFO:"') do (
  set EXISTING_PID=%%P
  goto found_existing
)
goto no_existing

:found_existing
echo.
echo ====================================================================
echo  Windrose Server Manager is ALREADY RUNNING (PID %EXISTING_PID%).
echo  Open http://localhost:8080 in your browser.
echo.
echo  To stop the old one:
echo    powershell -Command "Stop-Process -Id %EXISTING_PID% -Force"
echo  Or to kill any instance:
echo    powershell -Command "Get-Process WindroseServerManager.Web ^| Stop-Process -Force"
echo ====================================================================
echo.
pause
exit /b 0

:no_existing
cd /d "%~dp0"
echo Starting Windrose Server Manager — http://localhost:8080
echo Press Ctrl+C to stop.
echo.
"%~dp0WindroseServerManager.Web.exe"
set EXIT_CODE=%ERRORLEVEL%
echo.
echo ====================================================================
if %EXIT_CODE% EQU 0 (
  echo  Process exited cleanly. Press any key to close.
) else (
  echo  Process exited with code %EXIT_CODE%.
  echo  Scroll up to read the error.  See REQUIREMENTS.md if confused.
  echo  Press any key to close.
)
echo ====================================================================
pause >nul
exit /b %EXIT_CODE%
