#!/usr/bin/env bash
# proof-engine — top-level dispatcher.
#
#   proof-engine verify --claim "..."        # → tools/verify_cli.py
#   proof-engine registry serve ./proofs     # → proof-registry (package CLI)
#   proof-engine citations verify --url ...  # → proof-citations (package CLI)
#
# Keeps a single stable command surface even though the implementations live
# in separate packages.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

# Python version preflight (fix #5). The packages declare requires-python>=3.11.
# Without this check, a user on Python 3.10 would get a cryptic ImportError
# ("from typing import...") deep in the package modules instead of a clear
# diagnostic.
if ! command -v python3 >/dev/null 2>&1; then
  echo "proof-engine: python3 not found on \$PATH" >&2
  exit 127
fi
_PY_VERSION=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
case "$_PY_VERSION" in
  3.11|3.12|3.13|3.14|3.15|3.16|3.17|3.18|3.19|4.*)
    : # supported
    ;;
  *)
    echo "proof-engine: Python 3.11+ required (found $_PY_VERSION)." >&2
    echo "  Install a newer Python (e.g. via pyenv) and re-run." >&2
    exit 78  # EX_CONFIG — configuration error
    ;;
esac

_install_hint() {
  cat >&2 <<EOF

Install the Proof Engine packages into the current Python env:
  pip install -e $SCRIPT_DIR/packages/proof-citations
  pip install -e $SCRIPT_DIR/packages/proof-engine-registry
  # and, for 'wiki' subcommands, packages/proof-engine-wiki

Or make sure the venv that has them is active.
EOF
}

_require() {
  # Abort with a clear install hint if a CLI dependency is not on $PATH.
  if ! command -v "$1" >/dev/null 2>&1; then
    echo "proof-engine: '$1' not found on \$PATH." >&2
    _install_hint
    exit 127
  fi
}

_require_python_module() {
  # Abort cleanly if a Python module needed by `verify` isn't importable —
  # otherwise the user would get a raw ModuleNotFoundError traceback.
  if ! python3 -c "import $1" >/dev/null 2>&1; then
    echo "proof-engine: Python module '$1' not importable in the current env." >&2
    _install_hint
    exit 127
  fi
}

cmd="${1:-}"
shift || true

case "$cmd" in
  verify)
    _require_python_module proof_engine_registry
    _require_python_module proof_citations
    exec python3 "$SCRIPT_DIR/tools/verify_cli.py" "$@"
    ;;
  registry)
    _require proof-registry
    exec proof-registry "$@"
    ;;
  citations)
    _require proof-citations
    exec proof-citations "$@"
    ;;
  -h|--help|"")
    cat <<USAGE
proof-engine — verify claims, look up existing proofs, self-host a registry.

Usage:
  proof-engine verify --claim "..." [--registry-check] [--model opus|sonnet] [--json]
  proof-engine registry <serve|emit|lookup|publish> [...]
  proof-engine citations <verify> [...]

Each subcommand has its own --help.

Exit codes (verify):
  0  pass verdict (PROVED / SUPPORTED / PARTIALLY VERIFIED)
  1  fail verdict (DISPROVED / UNDETERMINED)
  2  error before a verdict could be produced
  3  --registry-only mode: no hit in any configured registry

Spec: docs/registry-protocol.md (registry) · docs/headless-verify.md (verify)
USAGE
    ;;
  *)
    echo "proof-engine: unknown command '$cmd'" >&2
    echo "run 'proof-engine --help' for usage" >&2
    exit 2
    ;;
esac
