#!/usr/bin/env bash
# Launcher for an installed or unpacked ai-proxy bundle.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
export PATH="${ROOT}/bin:${HOME}/.local/bin:/usr/local/go/bin:${PATH}"
CMD="${1:-help}"
shift || true

case "$CMD" in
  up)
    mkdir -p "${ROOT}/var/keys"
    export ADDR="${ADDR:-:18080}"
    export AIPROXY_SSE_BYPASS_OK="${AIPROXY_SSE_BYPASS_OK:-1}"
    export KEY_DIR="${ROOT}/var/keys"
    exec "${ROOT}/bin/aiproxy-controlplane"
    ;;
  status)
    curl -sf "http://127.0.0.1:${PORT:-18080}/healthz" || echo "control plane not listening"
    echo
    python3 "${ROOT}/scripts/probe_enforcement.py" || true
    ;;
  corpus)
    python3 "${ROOT}/scripts/run_escape_corpus.py"
    ;;
  escape)
    "${ROOT}/bin/aiproxy-supervisor" --policy "${ROOT}/policy/max-boundary.yaml" --keys "${ROOT}/var/keys" --help >/dev/null
    echo "use: python3 ${ROOT}/scripts/run_escape_corpus.py"
    ;;
  plugins)
    bash "${ROOT}/scripts/install_enforcement_plugins.sh"
    ;;
  agents)
    python3 "${ROOT}/scripts/enable_agent_hooks.py" ${1:+"$1"}
    ;;
  features)
    python3 "${ROOT}/scripts/feature_inventory.py"
    ;;
  help|*)
    cat <<EOF
aiproxy commands:
  up         start the control plane on :18080
  status     health plus enforcement plugin probe
  corpus     run the 136-fixture host policy corpus
  escape     run the original escape suite
  plugins    install OpenShell, srt, nono, landrun (this also runs during install)
  agents     turn on supervisor checks for Claude Code, Codex, and Gemini
  features   print the feature count
EOF
    ;;
esac
