#!/usr/bin/env sh
#
# Benchlist runner one-line installer
# Usage:  curl -sSf https://benchlist.ai/install.sh | sh
#
# Detects Python 3.10+, pip-installs benchlist-runner, drops a 'benchlist'
# executable into ~/.local/bin (and adds it to PATH if missing).
# Read the source first if you don't trust pipe-to-shell — that's why we host it.

set -e

INSTALL_DIR="${BENCHLIST_INSTALL_DIR:-$HOME/.local/bin}"
PIP_FLAGS="${BENCHLIST_PIP_FLAGS:-}"

say() { printf '\033[1;34m▸\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m!\033[0m %s\n' "$*"; }
fail() { printf '\033[1;31m✗\033[0m %s\n' "$*" >&2; exit 1; }

say "Benchlist installer · v1"

# Detect Python
PY=""
for c in python3.13 python3.12 python3.11 python3.10 python3 python; do
  if command -v "$c" >/dev/null 2>&1; then
    V=$("$c" -c 'import sys; print(sys.version_info[0]*100+sys.version_info[1])' 2>/dev/null || echo 0)
    if [ "$V" -ge 310 ]; then PY="$c"; break; fi
  fi
done
[ -z "$PY" ] && fail "Need Python 3.10+ on PATH. Install from https://python.org or via your package manager."
say "Python: $($PY --version 2>&1)"

# Detect pip
if ! "$PY" -m pip --version >/dev/null 2>&1; then
  warn "pip missing; trying ensurepip…"
  "$PY" -m ensurepip --user || fail "Could not bootstrap pip. Install pip manually then re-run."
fi

# Install
say "Installing benchlist-runner via pip (--user)…"
"$PY" -m pip install --user --upgrade $PIP_FLAGS benchlist-runner

# Locate the resulting executable
USER_BASE=$("$PY" -c 'import site; print(site.USER_BASE)')
BIN="$USER_BASE/bin/benchlist"
if [ ! -x "$BIN" ]; then
  # macOS sometimes lands in ~/Library/Python/X.Y/bin
  BIN=$(find "$USER_BASE" -name benchlist -type f 2>/dev/null | head -1)
fi
[ -z "$BIN" ] || [ ! -x "$BIN" ] && fail "Install completed but 'benchlist' executable not found under $USER_BASE/bin"
say "Executable: $BIN"

# Symlink into INSTALL_DIR if different
mkdir -p "$INSTALL_DIR"
if [ "$(dirname "$BIN")" != "$INSTALL_DIR" ]; then
  ln -sf "$BIN" "$INSTALL_DIR/benchlist"
  say "Linked → $INSTALL_DIR/benchlist"
fi

# PATH check
case ":$PATH:" in
  *":$INSTALL_DIR:"*) : ;;
  *)
    warn "$INSTALL_DIR is not on PATH."
    SHELL_RC=""
    [ -f "$HOME/.zshrc" ] && SHELL_RC="$HOME/.zshrc"
    [ -f "$HOME/.bashrc" ] && SHELL_RC="$HOME/.bashrc"
    if [ -n "$SHELL_RC" ]; then
      printf '\nexport PATH="%s:$PATH"  # benchlist\n' "$INSTALL_DIR" >> "$SHELL_RC"
      say "Added PATH update to $SHELL_RC. Re-source it (or open a new shell)."
    else
      warn "Add this to your shell rc: export PATH=\"$INSTALL_DIR:\$PATH\""
    fi
    ;;
esac

cat <<EOF

\033[1;32m✓ Installed.\033[0m

Next steps:
  benchlist login             # device-code OAuth, opens browser, stores key
  benchlist demo              # synthesise a sample run.json, no inference
  benchlist run gsm8k --model anthropic/claude-haiku-4-5 --n 50
  benchlist verify <run_id>   # replays the Ed25519 check locally

Docs:    https://benchlist.ai/docs
API:     https://benchlist.ai/api
Agents:  https://benchlist.ai/llms.txt
Try:     https://benchlist.ai/try   (no install needed)

EOF
