#!/usr/bin/env bash
# ai_node/run_auditor.sh — run one of the vendored auditor-skill tools on a
# JSON input file. Wire-only: every result comes from the pack's SKILL.run.
#
#   usage: run_auditor.sh rollforward|liabilities|leadsheet <input.json>
#
#   rollforward  -> rollforward_check when the input has "tb_ending_balance",
#                   rollforward_ending otherwise
#   liabilities  -> unrecorded_liability_flags
#   leadsheet    -> leadsheet_tie
#
# Worked example inputs ship in ai_node/auditor_skills/examples/. A typed
# refusal is a valid decision (exit 0); see GUIDE.md "Auditor skills".
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
if [ "$#" -ne 2 ]; then
  echo "usage: $0 rollforward|liabilities|leadsheet <input.json>" >&2
  exit 2
fi
SUB="$1"
INPUT="$2"
[ -f "$INPUT" ] || { echo "no such file: $INPUT" >&2; exit 3; }
case "$SUB" in
  rollforward)
    if python3 -c "import json,sys; raise SystemExit(0 if 'tb_ending_balance' in json.load(open(sys.argv[1])) else 1)" "$INPUT" 2>/dev/null; then
      TOOL="rollforward_check"
    else
      TOOL="rollforward_ending"
    fi ;;
  liabilities) TOOL="unrecorded_liability_flags" ;;
  leadsheet)   TOOL="leadsheet_tie" ;;
  *)
    echo "unknown subcommand: $SUB (want rollforward|liabilities|leadsheet)" >&2
    exit 2 ;;
esac
exec python3 "$HERE/auditor_skills/run_tool.py" "$TOOL" "$INPUT"
