#!/bin/sh
# profile-wrap launcher for macOS and Linux (and Git Bash). Finds Python >= 3.10, then runs profile-wrap.py.
# Override the interpreter with PROFILE_WRAP_PYTHON=/path/to/python.
here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
script="$here/profile-wrap.py"

is_ok() {
  "$1" -c 'import sys; sys.exit(0 if sys.version_info[:2] >= (3, 10) else 1)' >/dev/null 2>&1
}

if [ -n "$PROFILE_WRAP_PYTHON" ] && is_ok "$PROFILE_WRAP_PYTHON"; then
  exec "$PROFILE_WRAP_PYTHON" "$script" "$@"
fi
for cand in python3.13 python3.12 python3.11 python3.10 python3 python; do
  if command -v "$cand" >/dev/null 2>&1 && is_ok "$cand"; then
    exec "$cand" "$script" "$@"
  fi
done
if command -v uv >/dev/null 2>&1; then
  exec uv run --no-project --python 3.12 "$script" "$@"
fi

found=""
for cand in python3 python; do
  if command -v "$cand" >/dev/null 2>&1; then
    found=$("$cand" -c 'import sys; print("%d.%d.%d" % sys.version_info[:3])' 2>/dev/null)
    break
  fi
done
if [ -z "$found" ]; then
  echo "profile-wrap did not find any Python on PATH." >&2
else
  echo "profile-wrap needs Python 3.10 or newer, but the newest found is Python $found." >&2
fi
case "$(uname -s)" in
  Darwin) fix="brew install python@3.12" ;;
  *) fix="sudo apt install python3.12   (or: sudo dnf install python3.12)" ;;
esac
echo "  Fix: $fix" >&2
echo "  Or install uv (https://docs.astral.sh/uv/) and run: uv run --python 3.12 profile-wrap ..." >&2
exit 1
