#!/bin/bash
set -e

# Function to find Python executable
find_python() {
    if command -v python3 &> /dev/null; then
        echo "python3"
    elif command -v python &> /dev/null; then
        echo "python"
    else
        echo "Python not found. Please install Python 3."
        exit 1
    fi
}

# Check if uv is installed, if not, install it
if ! command -v uv &> /dev/null; then
    echo "uv command not found. Installing uv..."
    PYTHON=$(find_python)
    $PYTHON -m pip install uv
fi

cd "$(dirname "$0")"

# if .venv not found, create virtual environment.
# (The FASTLED_DOCKER skip-guard that lived here was removed in #2812 — the
# niteris/fastled-compiler-* compilation Docker images that set that env var
# are decommissioned.)
if [ ! -d .venv ]; then
    ./install
fi

show_boards_stmt=""
# if no arguments
if [ $# -eq 0 ]; then
    # show supported boards list
    show_boards_stmt="--supported-boards"
fi

if [ -n "$show_boards_stmt" ]; then
    echo -e "\nSupported boards:\n"
    uv run ci/ci-compile.py $show_boards_stmt | tr ',' '\n' | sed 's/^/  /'
    echo -e "\nUsage: bash compile <board> [--examples example1,example2]\n"
else
    # Simple wrapper - all Docker/GitHub Actions detection handled in Python
    echo -e "\nRunning uv run ci/ci-compile.py $@\n"
    uv run ci/ci-compile.py "$@"
fi
