#!/bin/sh
# Run the grillspec CLI with a suitable Python (3.11 or newer), wherever it is.
# Order: $GRILLSPEC_PYTHON, python3.15 … python3.11, python3, python, the Windows
# `py` launcher (present under Git Bash), then `uv run`, which fetches a Python.
# Exits 127 when nothing works, so callers (the hook runner) can tell.
case "$0" in
  */*|*\\*) HERE="$(cd "${0%[/\\]*}" && pwd)" ;;
  *) HERE="$(pwd)" ;;
esac
SCRIPT="$HERE/grillspec.py"
CHECK='import sys; sys.exit(not (3, 11) <= sys.version_info[:2])'

for candidate in "${GRILLSPEC_PYTHON:-}" python3.15 python3.14 python3.13 python3.12 \
  python3.11 python3 python; do
  [ -n "$candidate" ] || continue
  if command -v "$candidate" >/dev/null 2>&1 && "$candidate" -c "$CHECK" >/dev/null 2>&1; then
    exec "$candidate" "$SCRIPT" "$@"
  fi
done

if command -v py >/dev/null 2>&1; then
  for version in 3.15 3.14 3.13 3.12 3.11; do
    if py "-$version" -c "$CHECK" >/dev/null 2>&1; then
      exec py "-$version" "$SCRIPT" "$@"
    fi
  done
fi

if command -v uv >/dev/null 2>&1; then
  exec uv run --no-project --quiet --python ">=3.11" python "$SCRIPT" "$@"
fi

echo "grillspec: no Python 3.11 or newer found (tried \$GRILLSPEC_PYTHON, python3.15..python3.11, python3, python, py, uv)." >&2
echo "Install Python 3.11+ or uv, or set GRILLSPEC_PYTHON to a Python 3.11+ executable." >&2
exit 127
