@echo off
setlocal
for %%I in ("%~dp0..") do set "PROJECT_DIR=%%~fI"
set "SCRIPT_DIR=%~dp0"
cd /d "%PROJECT_DIR%"

set "PYTHON_CMD="

if exist ".venv\Scripts\python.exe" (
  ".venv\Scripts\python.exe" --version >nul 2>&1
  if not errorlevel 1 (
    set "PYTHON_CMD=.venv\Scripts\python.exe"
  )
)

if not defined PYTHON_CMD (
  py -3.11 --version >nul 2>&1
  if not errorlevel 1 (
    set "PYTHON_CMD=py -3.11"
  )
)

if not defined PYTHON_CMD (
  py --version >nul 2>&1
  if not errorlevel 1 (
    set "PYTHON_CMD=py"
  )
)

if not defined PYTHON_CMD (
  python --version >nul 2>&1
  if not errorlevel 1 (
    set "PYTHON_CMD=python"
  )
)

if not defined PYTHON_CMD (
  echo Could not find a working Python interpreter.
  echo Please install Python 3.11+ or recreate .venv.
  exit /b 1
)

if exist "%SCRIPT_DIR%backup_code.py" (
  %PYTHON_CMD% "%SCRIPT_DIR%backup_code.py"
) else (
  %PYTHON_CMD% backup_code.py
)
if errorlevel 1 (
  echo Backup failed.
  exit /b 1
)

echo Backup finished.
