#!/usr/bin/env bash
# Pancake printer: one-time setup (Claude for Andrin). Creates .venv next to this file.
set -e
cd "$(dirname "$0")"
echo "=== Pancake printer setup ==="

PY=python3
if ! command -v "$PY" &>/dev/null; then
    PY=python
fi

if [ ! -f ".venv/bin/python3" ]; then
    echo "Creating .venv with $PY ..."
    "$PY" -m venv .venv
else
    # Recreate if tkinter is missing (e.g. python-tk was installed after the venv)
    if ! ".venv/bin/python3" -c "import tkinter" 2>/dev/null; then
        echo ".venv is missing tkinter — recreating it ..."
        rm -rf .venv
        "$PY" -m venv .venv
    fi
fi

VPY=".venv/bin/python3"
"$VPY" -m pip install --upgrade pip

echo ""
echo "--- team requirements (Aaron: team/software/requirements.txt)"
"$VPY" -m pip install -r team/software/requirements.txt

echo ""
echo "--- serial ports (ufp) and the photo converter"
"$VPY" -m pip install pyserial pillow scipy contourpy

echo ""
echo "--- background removal for photos (optional, about 200 MB with the model)"
"$VPY" -m pip install "rembg[cpu]" || echo "rembg could not be installed: photos will keep their background."

echo ""
echo "Setup done. Start the GUI with ./run_gui.sh"
