@echo off
setlocal
cd /d "%~dp0"

rem Keep the virtual environment outside the deeply nested project directory.
rem This prevents Windows Error 206 during NumPy/SciPy installation.
set "GBI_ENV_ROOT=%LOCALAPPDATA%\UNNS\GBI010"
set "GBI_PY=%GBI_ENV_ROOT%\Scripts\python.exe"

set "PYLAUNCHER=python"
where py >nul 2>nul && set "PYLAUNCHER=py -3"

if not exist "%GBI_PY%" (
  echo Creating short-path Python environment:
  echo   %GBI_ENV_ROOT%
  if not exist "%LOCALAPPDATA%\UNNS" mkdir "%LOCALAPPDATA%\UNNS"
  %PYLAUNCHER% -m venv "%GBI_ENV_ROOT%" || goto :error
)

if exist ".venv" (
  echo Note: the incomplete local .venv is ignored. It may be deleted manually.
)

if not exist "%GBI_PY%" goto :error
"%GBI_PY%" -c "import sklearn,pandas,numpy,matplotlib" >nul 2>nul
if errorlevel 1 (
  echo The chamber environment is not fully installed.
  echo Run RUN_GRAV_BRIDGE_I.bat once first.
  goto :error
)
set "PYTHONPATH=%~dp0;%PYTHONPATH%"
"%GBI_PY%" -m grav_bridge_i.cli --output outputs\registered_manual_run
pause
exit /b 0

:error
echo.
echo GRAV-BRIDGE-I registered analysis could not start.
pause
exit /b 1
