#!/usr/bin/env bash
set -euo pipefail

usage() {
  cat <<'USAGE'
Usage:
  ./scripts/periodic/compare_cir_round.sh \
    --ours-cir <path> \
    --rpki-client-cir <path> \
    --out-dir <path> \
    [--sample-limit <n>]
USAGE
}

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
OURS_CIR=""
CLIENT_CIR=""
OUT_DIR=""
SAMPLE_LIMIT="20"
CIR_STATE_COMPARE_BIN="$ROOT_DIR/target/release/cir_state_compare"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --ours-cir) OURS_CIR="$2"; shift 2 ;;
    --rpki-client-cir) CLIENT_CIR="$2"; shift 2 ;;
    --out-dir) OUT_DIR="$2"; shift 2 ;;
    --sample-limit) SAMPLE_LIMIT="$2"; shift 2 ;;
    -h|--help) usage; exit 0 ;;
    *) echo "unknown argument: $1" >&2; usage; exit 2 ;;
  esac
done

[[ -n "$OURS_CIR" && -n "$CLIENT_CIR" && -n "$OUT_DIR" ]] || { usage >&2; exit 2; }
mkdir -p "$OUT_DIR"

if [[ ! -x "$CIR_STATE_COMPARE_BIN" ]]; then
  (cd "$ROOT_DIR" && cargo build --release --bin cir_state_compare)
fi

"$CIR_STATE_COMPARE_BIN" \
  --ours-cir "$OURS_CIR" \
  --rpki-client-cir "$CLIENT_CIR" \
  --out-json "$OUT_DIR/cir-compare-summary.json" \
  --out-md "$OUT_DIR/cir-compare-summary.md" \
  --sample-limit "$SAMPLE_LIMIT"

echo "$OUT_DIR"
