@echo off
:: HEG BRep Two-Pass Inference — entry-point wrapper.
::
:: Usage:
::   run_batch.bat \path\to\step_folder \path\to\output.xlsx
::   run_batch.bat --single \path\to\one_part.step

setlocal enabledelayedexpansion

set "SCRIPT_DIR=%~dp0"
if "%SCRIPT_DIR:~-1%"=="\" set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%"

if "%~1"=="" (
    call :usage
    exit /b 1
)

:: --- Pick the inference Python (clip env's python) -----------------------------
set "INFER_PY="

if defined HEG_CLIP_PYTHON (
    if exist "%HEG_CLIP_PYTHON%" (
        set "INFER_PY=%HEG_CLIP_PYTHON%"
        goto :python_found
    )
)

if not defined HEG_INFER_ENV_DEFAULT (
    set "HEG_INFER_ENV_DEFAULT=clip"
)

if defined CONDA_PREFIX (
    if exist "%CONDA_PREFIX%\..\%HEG_INFER_ENV_DEFAULT%\python.exe" (
        set "INFER_PY=%CONDA_PREFIX%\..\%HEG_INFER_ENV_DEFAULT%\python.exe"
        goto :python_found
    )
)

set "USER_PROFILE_PATHS=%USERPROFILE%\anaconda3\envs\%HEG_INFER_ENV_DEFAULT%\python.exe;%USERPROFILE%\miniconda3\envs\%HEG_INFER_ENV_DEFAULT%\python.exe;%USERPROFILE%\mambaforge\envs\%HEG_INFER_ENV_DEFAULT%\python.exe;%USERPROFILE%\miniforge3\envs\%HEG_INFER_ENV_DEFAULT%\python.exe;%USERPROFILE%\.conda\envs\%HEG_INFER_ENV_DEFAULT%\python.exe;C:\ProgramData\anaconda3\envs\%HEG_INFER_ENV_DEFAULT%\python.exe;C:\ProgramData\miniconda3\envs\%HEG_INFER_ENV_DEFAULT%\python.exe"

for %%P in ("%USER_PROFILE_PATHS:;=" "%") do (
    if exist "%%~P" (
        set "INFER_PY=%%~P"
        goto :python_found
    )
)

where conda >nul 2>&1
if %ERRORLEVEL% equ 0 (
    set "INFER_PY=conda run --no-capture-output -n %HEG_INFER_ENV_DEFAULT% python"
    goto :python_found
)

echo ERROR: could not locate the inference env's Python. >&2
echo Set HEG_CLIP_PYTHON=\path\to\your\clip\python.exe and re-run. >&2
exit /b 1

:python_found
echo [run_batch.bat] inference python: %INFER_PY%

:: --- Mode select ---------------------------------------------------------------
if "%~1"=="--single" (
    :: Use explicit indexing instead of relying solely on sequential shifts
    set "STEP_FILE=%~2"
    if "!STEP_FILE!"=="" (
        echo ERROR: --single requires a STEP file path. >&2
        exit /b 1
    )
    echo [run_batch.bat] single-file mode: !STEP_FILE!
    
    :: Skip the first two parameters (--single and file path)
    shift
    shift
    
    set "ARGS="
    :loop_single
    if not "%~1"=="" (
        set "ARGS=!ARGS! %1"
        shift
        goto :loop_single
    )
    
    call :exec_single
    exit /b !ERRORLEVEL!
)

if "%~2"=="" (
    call :usage
    exit /b 1
)

set "STEP_FOLDER=%~1"
set "OUTPUT_XLSX=%~2"
echo [run_batch.bat] batch mode: %STEP_FOLDER% -^> %OUTPUT_XLSX%

shift
shift

set "ARGS="
:loop_batch
if not "%~1"=="" (
    set "ARGS=!ARGS! %1"
    shift
    goto :loop_batch
)

call :exec_batch
exit /b !ERRORLEVEL!

:: --- Safe Execution Subroutines -------------------------------------------------
:exec_single
%INFER_PY% "%SCRIPT_DIR%\brep_two_pass\tools\run_brep_two_pass_single_step.py" --step-file "%STEP_FILE%" %ARGS%
goto :eof

:exec_batch
%INFER_PY% -u "%SCRIPT_DIR%\brep_two_pass\tools\batch_brep_two_pass_to_excel.py" --step_folder "%STEP_FOLDER%" --output_excel "%OUTPUT_XLSX%" %ARGS%
goto :eof

:: --- Usage Block ---------------------------------------------------------------
:usage
echo HEG BRep Two-Pass Inference
echo.
echo Usage:
echo   run_batch.bat ^<step_folder^> ^<output.xlsx^> [extra args...]   # batch mode
echo   run_batch.bat --single ^<step_file^>                          # single STEP
goto :eof