#!/usr/bin/env bash
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$SCRIPT_DIR/BEANalyzer_UltraPro"
VENV_DIR="$PROJECT_DIR/.venv"

cd "$PROJECT_DIR"

PYTHON_BIN=""
for candidate in python3.12 python3; do
    if command -v "$candidate" >/dev/null 2>&1; then
        PYTHON_BIN="$(command -v "$candidate")"
        break
    fi
done

if [ -z "$PYTHON_BIN" ]; then
    echo "Python 3.12 was not found."
    echo "Install it with your distro package manager, for example:"
    echo "sudo apt install python3.12 python3.12-venv python3-pip"
    read -p "Press ENTER to exit..."
    exit 1
fi

if [ ! -x "$VENV_DIR/bin/python" ]; then
    echo "Creating virtual environment..."
    "$PYTHON_BIN" -m venv "$VENV_DIR"
fi

source "$VENV_DIR/bin/activate"

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
