#!/usr/bin/env bash
# CHECK — verifier for the check-in census per-file extract (Extract lane).
#
# Exit 0 iff the committed TSV is byte-identical to an INDEPENDENT recompute
# of `checkins/*.md` (README.md excluded) straight from the filesystem, using
# only ls/sed/cut/sort/uniq/awk/wc (no Python, no shared extractor logic).
# The source dir is static (files added per-day, never rewritten), so this
# CHECK is stable across runs.
#
# Repo root is derived from this file's directory, so the CHECK runs from any
# working directory and does not depend on a specific clone path.

set -u

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
REPO_ROOT="$(cd ../.. && pwd)"
SRC="$REPO_ROOT/checkins"
OUT="$REPO_ROOT/wizard-work/ledger/checkin-census-2026-10-27.tsv"

test -s "$OUT" || { echo "FAIL: TSV missing or empty: $OUT" >&2; exit 1; }

tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT

{
  echo "date,wizard,size"
  for f in "$SRC"/*.md; do
    [ -e "$f" ] || continue
    n=$(basename "$f" .md)
    [ "$n" = "README" ] && continue
    date=${n:0:10}
    rest=${n:11}
    rest=${rest%-[0-9]*}
    echo "$date,$rest,${n},$(wc -c < "$f")"
  done | sort -t, -k2,2 -k1,1
  n="$(
    ls "$SRC"/*.md 2>/dev/null | grep -v 'README\.md$' | wc -l
  )"
  b="$(find "$SRC" -type f ! -name 'README.md' -printf '%s\n' | awk '{s+=$1} END {print s}')"
  echo "TOTAL,$n,$b"
} > "$tmp"

if cmp -s "$OUT" "$tmp"; then
  echo "PASS: rows=$(tail -n +2 "$OUT" | grep -c .) (per-file checkin rows + TOTAL) recomputed from $SRC"
  exit 0
fi

echo "FAIL: census mismatch against independent recompute" >&2
diff -u "$OUT" "$tmp" >&2 || true
exit 1
