@echo off
rem Resolve a regular/portable Python runtime. No virtual environment is required.
set "H3_RUNTIME_PYTHON="

if defined H3_ENGINE_PYTHON if exist "%H3_ENGINE_PYTHON%" set "H3_RUNTIME_PYTHON=%H3_ENGINE_PYTHON%"
if not defined H3_RUNTIME_PYTHON if defined H3_PYTHON if exist "%H3_PYTHON%" set "H3_RUNTIME_PYTHON=%H3_PYTHON%"
if not defined H3_RUNTIME_PYTHON if defined H3_STUDIO_PYTHON if exist "%H3_STUDIO_PYTHON%" set "H3_RUNTIME_PYTHON=%H3_STUDIO_PYTHON%"
if not defined H3_RUNTIME_PYTHON if exist "%~dp0..\.runtime\python\python.exe" set "H3_RUNTIME_PYTHON=%~dp0..\.runtime\python\python.exe"
rem Compatibility for this pre-installer development workspace only. Production
rem packages do not contain .venv and therefore continue to require .runtime.
if not defined H3_RUNTIME_PYTHON if exist "%~dp0..\.venv\Scripts\python.exe" set "H3_RUNTIME_PYTHON=%~dp0..\.venv\Scripts\python.exe"

if not defined H3_RUNTIME_PYTHON for /f "delims=" %%P in ('where python.exe 2^>nul') do if not defined H3_RUNTIME_PYTHON set "H3_RUNTIME_PYTHON=%%P"
if not defined H3_RUNTIME_PYTHON for /f "delims=" %%P in ('where python3.exe 2^>nul') do if not defined H3_RUNTIME_PYTHON set "H3_RUNTIME_PYTHON=%%P"

if not defined H3_RUNTIME_PYTHON (
  echo [ERROR] No compatible Python runtime was found.
  echo Run setup_h3_cn.cmd, or set H3_ENGINE_PYTHON to an existing Python executable.
  exit /b 1
)

"%H3_RUNTIME_PYTHON%" -c "import sys,aiohttp,av,numpy; assert sys.version_info >= (3, 11)" >nul 2>nul
if errorlevel 1 (
  echo [ERROR] Python 3.11+ with aiohttp, av, and numpy is required: %H3_RUNTIME_PYTHON%
  set "H3_RUNTIME_PYTHON="
  exit /b 1
)
exit /b 0
