#!/bin/sh
set -eu
repository_root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)

is_supported_python() {
  command -v "$1" >/dev/null 2>&1 &&
    "$1" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)' >/dev/null 2>&1
}

if [ -n "${CODE_REPAIR_DEMO_PYTHON:-}" ]; then
  artifact_python=$CODE_REPAIR_DEMO_PYTHON
  if ! is_supported_python "$artifact_python"; then
    echo "artifact requires Python 3.11+; CODE_REPAIR_DEMO_PYTHON must name a supported interpreter." >&2
    exit 2
  fi
else
  artifact_python=
  for candidate in python3.12 python3.11 python3; do
    if is_supported_python "$candidate"; then
      artifact_python=$candidate
      break
    fi
  done
  if [ -z "$artifact_python" ]; then
    echo "artifact requires Python 3.11+; add python3.12, python3.11, or python3 to PATH." >&2
    exit 2
  fi
fi

PYTHONPATH="$repository_root/src${PYTHONPATH:+:$PYTHONPATH}" \
  "$artifact_python" -m code_repair_interventions_demo.cli "$@"
