@echo off
setlocal EnableExtensions
set "SCRIPT=%~dp0run-windows.ps1"
if not exist "%SCRIPT%" (
  echo [INFO] run-windows.ps1 nao encontrado na pasta atual.
  echo [INFO] Baixando a versao mais recente do inicializador...
  where curl >nul 2>nul
  if %ERRORLEVEL%==0 (
    curl -fsSL https://wiki.jupiteranalytics.com.br/downloads/run-windows.ps1 -o "%SCRIPT%"
  ) else (
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('https://wiki.jupiteranalytics.com.br/downloads/run-windows.ps1', '%SCRIPT%')"
  )
)
if not exist "%SCRIPT%" (
  echo [ERRO] Nao foi possivel obter o script run-windows.ps1.
  echo Verifique sua conexao com a internet ou baixe manualmente em:
  echo https://wiki.jupiteranalytics.com.br/comecar/instalar
  pause
  exit /b 1
)
set "OPTIONS="
:parse_args
if "%~1"=="" goto launch
if /I "%~1"=="--keep-alive" (
  set "OPTIONS=%OPTIONS% -KeepAlive"
  shift
  goto parse_args
)
if /I "%~1"=="--no-kill" (
  set "OPTIONS=%OPTIONS% -KeepAlive"
  shift
  goto parse_args
)
if /I "%~1"=="--select-version" (
  set "OPTIONS=%OPTIONS% -SelectVersion"
  shift
  goto parse_args
)
if /I "%~1"=="--version" (
  set "OPTIONS=%OPTIONS% -TargetVersion ""%~2"""
  shift
  shift
  goto parse_args
)
if /I "%~1"=="--target-version" (
  set "OPTIONS=%OPTIONS% -TargetVersion ""%~2"""
  shift
  shift
  goto parse_args
)
echo [ERRO] Argumento desconhecido: %~1
pause
exit /b 2

:launch
echo [INFO] Acompanhe o progresso das etapas nesta janela.
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT%" %OPTIONS%
set "RESULT=%ERRORLEVEL%"
if not "%RESULT%"=="0" pause
exit /b %RESULT%
