#!/usr/bin/env bash
# swarm-gate — run the code-review-swarm lanes over a change and turn them into a verdict.
#
#   swarm-gate [--repo PATH] [--base REF] [--scope PATH...] [--lanes correctness,quality]
#              [--max-rounds N] [--same-signature-limit N] [--timeout SEC]
#              [--agents DIR] [--ledger FILE] [--json] [--dry-run]
#
# Exit codes: 0 no SEV1/2 · 1 SEV1/2 present · 2 usage/nothing to review
#             3 same-signature loop guard · 4 round budget exhausted
#             5 a lane is unusable (dispatch failed, no table, no COVERAGE line)
#
# The implementation is lib/swarm_gate.py: markdown-table parsing, a JSONL ledger and
# two concurrent subprocess dispatches are all cheap there and brittle in shell. This
# wrapper exists so the call site stays a script, and so `python3` is resolved once.
set -euo pipefail

# Resolve the symlink: this script is expected to be linked onto PATH (~/.local/bin), and a
# shim that trusts $0 would look for lib/ beside the link instead of beside the checkout.
SELF=${BASH_SOURCE[0]}
while [ -L "$SELF" ]; do
	LINK=$(readlink "$SELF")
	case $LINK in
		/*) SELF=$LINK ;;
		*) SELF=$(dirname -- "$SELF")/$LINK ;;
	esac
done
HERE=$(cd -- "$(dirname -- "$SELF")/.." && pwd)

if ! command -v python3 >/dev/null 2>&1; then
	printf 'swarm-gate: python3 not on PATH\n' >&2
	exit 2
fi

exec python3 "$HERE/lib/swarm_gate.py" "$@"
