#!/usr/bin/env bash
# pact: launch the harness with a suitable interpreter.
#
# The package requires Python 3.12. Resolving that here, before importing
# anything, is what keeps a machine whose default python3 is older from failing
# with a traceback instead of a clear message. Order matches the harness policy:
# PACT_PYTHON, then python3.12, then python3, then the interpreter DeepSeek
# Harness ships. Installed distributions use the console script generated by the
# package manager instead of this launcher.
set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PKG_ROOT="$(dirname "$HERE")"
DSH_PYTHON="/Applications/DeepSeek Harness.app/Contents/Resources/runtime/primary-runtime/dependencies/python/bin/python3"

suitable() {
  [ -n "$1" ] || return 1
  command -v "$1" >/dev/null 2>&1 || [ -x "$1" ] || return 1
  "$1" -c 'import sys; raise SystemExit(0 if sys.version_info[:2] >= (3, 12) else 1)' >/dev/null 2>&1
}

PYTHON=""
for candidate in "${PACT_PYTHON:-}" "python3.12" "python3" "$DSH_PYTHON"; do
  if suitable "$candidate"; then
    PYTHON="$candidate"
    break
  fi
done

if [ -z "$PYTHON" ]; then
  echo "pact: no Python 3.12 or later found." >&2
  echo "      Set PACT_PYTHON to a 3.12+ interpreter and retry." >&2
  echo "      Checked: PACT_PYTHON, python3.12, python3, the DeepSeek Harness interpreter." >&2
  exit 127
fi

export PYTHONPATH="$PKG_ROOT/src${PYTHONPATH:+:$PYTHONPATH}"
exec "$PYTHON" -m pact_harness "$@"
