#!/usr/bin/env bash
# ai_node/run_anomaly.sh — Benford / round-number / just-under-threshold anomaly
# scan on a CSV you provide, via the vendored CPA Audit Pack "anomaly" module.
# Every threshold is a named, human-set parameter (see audit_pack/anomaly/README.md);
# the tool never picks its own risk cutoff. This script computes nothing itself.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
[ "$#" -ge 1 ] || { echo "usage: $0 <ledger.csv>" >&2; exit 2; }
CSV="$1"
[ -f "$CSV" ] || { echo "no such file: $CSV" >&2; exit 3; }
OUTDIR="$(cd "$(dirname "$CSV")" && pwd)"
BASE="$(basename "$CSV" .csv)"
LEDGER_JSON="$OUTDIR/${BASE}.ai_node_ledger.json"
RESULTS_JSON="$OUTDIR/${BASE}.anomaly_results.json"
python3 "$HERE/audit_pack/csv_to_ledger.py" "$CSV" "$LEDGER_JSON"
CPA_AUDIT_LEDGER_JSON="$LEDGER_JSON" CPA_AUDIT_OUT_JSON="$RESULTS_JSON" \
  python3 "$HERE/audit_pack/anomaly/run.py"
echo
echo "Full results: $RESULTS_JSON"
