@echo off
setlocal EnableExtensions

REM Relaunch ourselves inside a persistent console so it can't "flash and close"
if /i not "%~1"=="__RUN" (
  start "MY.TOWN Blogger Installer" cmd /k ""%~f0" __RUN"
  exit /b
)

cd /d "%~dp0"

echo ==========================================
echo  MY.TOWN Blogger Kit - Windows Installer
echo ==========================================
echo.

REM Open instructions page (optional)
if exist "%~dp0START_HERE.html" (
  start "" "%~dp0START_HERE.html"
)

set "LOG=%~dp0install-windows.log"
echo Logging to: %LOG%
echo.

echo Running installer...
echo.

REM Run the PowerShell installer and capture output to a log file
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0bootstrap_kit\install.ps1" 1>>"%LOG%" 2>>&1
set "EXITCODE=%ERRORLEVEL%"

echo.
if "%EXITCODE%"=="0" (
  echo Install complete.
  echo Next: return to START_HERE.html and continue to Step B.
) else (
  echo Install failed (code %EXITCODE%).
  echo.
  echo Last 30 lines of log:
  echo ------------------------------------------
  powershell -NoProfile -Command "Get-Content -LiteralPath '%LOG%' -Tail 30"
  echo ------------------------------------------
  echo Full log: %LOG%
)

echo.
echo Press any key to close this window...
pause >nul
exit /b %EXITCODE%
