@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "SCRIPT_DIR=%~dp0"
set "PROJECT_DIR=%SCRIPT_DIR%BEANalyzer_UltraPro"
set "VENV_DIR=%PROJECT_DIR%\.venv"
set "PYTHON_EXE="

where py >nul 2>nul
if %ERRORLEVEL%==0 (
    py -3.12 --version >nul 2>nul
    if %ERRORLEVEL%==0 set "PYTHON_EXE=py -3.12"
)

if "%PYTHON_EXE%"=="" (
    where python >nul 2>nul
    if %ERRORLEVEL%==0 set "PYTHON_EXE=python"
)

if "%PYTHON_EXE%"=="" (
    echo Python 3.12 was not found.
    echo Install Python 3.12 from https://www.python.org/downloads/
    echo During installation, enable: Add python.exe to PATH
    pause
    exit /b 1
)

cd /d "%PROJECT_DIR%"

if not exist "%VENV_DIR%\Scripts\python.exe" (
    echo Creating virtual environment...
    %PYTHON_EXE% -m venv "%VENV_DIR%"
    if errorlevel 1 (
        echo Failed to create virtual environment.
        pause
        exit /b 1
    )
)

call "%VENV_DIR%\Scripts\activate.bat"

echo Installing/updating requirements...
python -m pip install --upgrade pip setuptools wheel
python -m pip install -r requirements.txt

echo Starting BEANalyzer UltraPro...
python beanalyzer_ultrapro.py

endlocal
