@echo off
rem VSA Monolith Framework - the double-clickable Windows installer.
rem
rem This file exists for one reason: a .ps1 cannot be double-clicked. Explorer
rem opens it in Notepad, and even from a prompt the default execution policy
rem refuses it. A .cmd is the one thing Explorer will run, so this is the
rem artifact a developer downloads from vsa.vectorasoft.com and opens.
rem
rem It holds no installer logic of its own. It downloads vsa-installer.ps1 from
rem the site and runs that, so a developer who downloaded this file months ago
rem still gets today's installer - the only thing that can go stale is the URL.
rem
rem -ExecutionPolicy Bypass applies to these two processes only; it does not
rem change the machine's policy. -NoProfile keeps a developer's own PowerShell
rem profile out of the install.
rem
rem Arguments pass through:  vsa-install.cmd -Postgres

setlocal
title VSA Monolith Framework - installer

if "%VSA_SITE%"=="" set "VSA_SITE=https://vsa.vectorasoft.com"
set "VSA_PS1=%TEMP%\vsa-installer.ps1"

echo.
echo VSA Monolith Framework
echo Fetching the installer from %VSA_SITE%
echo.

rem Downloaded to a file rather than run from memory, so the two checks below
rem happen before any of it executes. Windows PowerShell 5.1 still defaults to
rem TLS 1.0, which the site refuses, so the protocol is set first.
powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; try { Invoke-WebRequest -UseBasicParsing -TimeoutSec 30 \"$env:VSA_SITE/vsa-installer.ps1\" -OutFile \"$env:VSA_PS1\" } catch { Write-Host $_.Exception.Message -ForegroundColor Red; exit 1 }"

if errorlevel 1 (
    echo.
    echo Could not download %VSA_SITE%/vsa-installer.ps1
    echo Check the network and open this file again.
    echo.
    pause
    endlocal & exit /b 1
)

rem Rejects an HTML error page served with a 200 - a captive portal, or a host
rem that answers every path with its 404 page.
findstr /c:"VSA Monolith Framework" "%VSA_PS1%" >nul 2>&1
if errorlevel 1 (
    echo.
    echo That URL did not return the installer.
    echo You are probably behind a network sign-in page. Connect, then try again.
    echo.
    del "%VSA_PS1%" >nul 2>&1
    pause
    endlocal & exit /b 1
)

rem The installer asks for the GitHub token in this same console window.
powershell -NoProfile -ExecutionPolicy Bypass -File "%VSA_PS1%" %*
set "STATUS=%ERRORLEVEL%"

del "%VSA_PS1%" >nul 2>&1

echo.
if not "%STATUS%"=="0" echo The installer stopped with status %STATUS% (see above).

rem Explorer closes the console the moment the script ends, taking the output
rem - and any error - with it.
pause
endlocal & exit /b %STATUS%
