#!/usr/bin/env bash
#
# champollion.dev/harness — installer for the MT Eval Arena harness.
#
#   curl -fsSL champollion.dev/harness | bash
#
# What it does (and nothing more):
#   1. Checks for python3 (>= 3.10).
#   2. Installs via pipx if available (isolated, recommended); otherwise
#      offers `pip install --user` as a fallback.
#   3. Installs mt-eval-harness from the public mirror:
#      git+https://github.com/gamedaysuits/gds-mt-eval-harness.git
#   4. Prints quickstart next steps.
#
# Safety posture:
#   - Never uses sudo. Never writes outside pip/pipx user directories.
#   - `--dry-run` prints what would run, runs nothing.
#   - set -u and explicit error handling; no curl-pipe-to-shell chains
#     inside the script itself.

set -u

REPO_URL="git+https://github.com/gamedaysuits/gds-mt-eval-harness.git"
MIN_PYTHON_MINOR=10
DRY_RUN=0

for arg in "$@"; do
  case "$arg" in
    --dry-run) DRY_RUN=1 ;;
    -h|--help)
      sed -n '2,20p' "$0" 2>/dev/null || true
      echo "Usage: harness [--dry-run]"
      exit 0
      ;;
    *)
      echo "Unknown option: $arg (try --dry-run)" >&2
      exit 2
      ;;
  esac
done

say()  { printf '%s\n' "$*"; }
err()  { printf 'error: %s\n' "$*" >&2; }

run() {
  # Echo every mutating command; skip execution under --dry-run.
  say "+ $*"
  if [ "$DRY_RUN" -eq 0 ]; then
    "$@"
  fi
}

say "MT Eval Arena harness installer"
say "-------------------------------"

# --- 1. python3 ------------------------------------------------------------
if ! command -v python3 >/dev/null 2>&1; then
  err "python3 not found."
  err "Install Python 3.${MIN_PYTHON_MINOR}+ first: https://www.python.org/downloads/"
  exit 1
fi

PY_MINOR="$(python3 -c 'import sys; print(sys.version_info.minor)' 2>/dev/null || echo 0)"
PY_MAJOR="$(python3 -c 'import sys; print(sys.version_info.major)' 2>/dev/null || echo 0)"
if [ "$PY_MAJOR" -lt 3 ] || { [ "$PY_MAJOR" -eq 3 ] && [ "$PY_MINOR" -lt "$MIN_PYTHON_MINOR" ]; }; then
  err "Python 3.${MIN_PYTHON_MINOR}+ required (found $(python3 --version 2>&1))."
  exit 1
fi
say "✓ $(python3 --version 2>&1)"

# --- 2. installer: pipx preferred, pip --user fallback ----------------------
if command -v pipx >/dev/null 2>&1; then
  say "✓ pipx found — installing into an isolated environment"
  run pipx install "$REPO_URL"
else
  say "pipx not found. pipx keeps the harness isolated from your other"
  say "Python packages; you can get it with:"
  say "    python3 -m pip install --user pipx && python3 -m pipx ensurepath"
  say ""
  if [ "$DRY_RUN" -eq 1 ]; then
    say "(dry-run) would fall back to: python3 -m pip install --user $REPO_URL"
  elif [ -t 0 ]; then
    printf 'Fall back to "pip install --user" instead? [y/N] '
    read -r REPLY
    case "$REPLY" in
      y|Y|yes|YES)
        run python3 -m pip install --user "$REPO_URL"
        ;;
      *)
        say "Aborted. Install pipx and re-run this script."
        exit 1
        ;;
    esac
  else
    # Non-interactive (curl | bash): take the documented pip fallback
    # rather than failing silently mid-pipe.
    say "Non-interactive shell — using pip --user fallback."
    run python3 -m pip install --user "$REPO_URL"
  fi
fi

# --- 3. verify + next steps -------------------------------------------------
if [ "$DRY_RUN" -eq 0 ] && ! command -v mt-eval >/dev/null 2>&1; then
  say ""
  say "Note: 'mt-eval' is not on your PATH yet. If you used pipx, run:"
  say "    pipx ensurepath"
  say "then open a new shell. With pip --user, ensure ~/.local/bin is on PATH."
fi

say ""
say "Next steps:"
say "  mt-eval setup                # interactive wizard (keys, datasets)"
say "  mt-eval run --help           # run your first benchmark"
say "  https://mtevalarena.org/docs/how-it-works"
say ""
say "Done."
