#!/usr/bin/env bash
# CHECK -- quill: draft-compliance report 2026-10-31 (forge: quill lane; gate by
# volt, Q142; pin and revision modes added by volt, Q154).
#
# The artifact is a SNAPSHOT: draft-compliance-report-2026-10-31.{tsv,md} counts
# the .md drafts of this lane at the revision named in
# draft-compliance-report-2026-10-31.pin. Five legs, all run from this script's
# own directory:
#   selftest    15 cells: one green control, eight mutants, four pin-contract
#               cells, rule-agreement and no-host-path
#   pinned      the committed TSV and report re-derive from the lane AT THE PIN
#               (default mode), with the drift since the pin printed as a note
#   rev-probe   `--rev` measures: the pin's own rev reads 0, and 8847c7c -- the
#               commit that hand-edited this table -- reads 1 against it
#   regenerate  the generator run on the lane extracted at the pin is
#               byte-identical to the committed TSV
#   copy-parity the same artifact and verifier in a COPY outside any repository
#               read rc 0 there and rc 1 with one row moved: the copy grades its
#               own bytes, not this worktree
#
# Exit codes: 0 all legs passed, 1 a leg failed, 2 a leg could not be graded.
set -u
set -o pipefail

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 2
ROOT="$(pwd)"
LANE="$ROOT/wizard-work/quill"
PIN="$LANE/draft-compliance-report-2026-10-31.pin"
TSV="$LANE/draft-compliance-report-2026-10-31.tsv"
WORK="$(mktemp -d "${TMPDIR:-/tmp}/check-quill.XXXXXX")" || exit 2
trap 'rm -rf "$WORK"' EXIT

passed=0
failed=0
ungradable=0
row() { printf '  %-12s %s\n' "$1" "$2"; }

PIN_REV="$(sed -n 's/^rev[[:space:]]*//p' "$PIN" 2>/dev/null | head -1)"

# --- leg 1: the verifier's own cells -----------------------------------------
python3 "$LANE/verify_draft_compliance.py" --selftest >"$WORK/selftest.out" 2>&1
st_rc=$?
if [ "$st_rc" -eq 0 ]; then
    row selftest "PASS  $(tail -n 1 "$WORK/selftest.out")"
    passed=$((passed + 1))
else
    row selftest "FAULT rc=$st_rc $(tail -n 1 "$WORK/selftest.out")"
    cat "$WORK/selftest.out"
    failed=$((failed + 1))
fi

# --- leg 2: the committed artifact re-derives at the pin ---------------------
python3 "$LANE/verify_draft_compliance.py" >"$WORK/pinned.out" 2>&1
pin_rc=$?
if [ "$pin_rc" -eq 0 ]; then
    row pinned "PASS  $(head -n 1 "$WORK/pinned.out")  [$(sed -n 2p "$WORK/pinned.out")]"
    passed=$((passed + 1))
elif [ "$pin_rc" -eq 2 ]; then
    # Ungradable is not a fault and is not a pass: the pin, the table or the rev
    # could not be read, so the census's truth was not tested at all.
    row pinned "UNGRADABLE rc=2 $(head -n 2 "$WORK/pinned.out" | tr '\n' ' ')"
    cat "$WORK/pinned.out"
    ungradable=$((ungradable + 1))
else
    row pinned "FAULT rc=$pin_rc"
    cat "$WORK/pinned.out"
    failed=$((failed + 1))
fi

# --- leg 3: --rev is a measurement, not a flag ------------------------------
if [ -z "$PIN_REV" ]; then
    row rev-probe "UNGRADABLE pin names no rev"
    ungradable=$((ungradable + 1))
else
    python3 "$LANE/verify_draft_compliance.py" --rev "$PIN_REV" >"$WORK/rev-ok.out" 2>&1
    ok_rc=$?
    python3 "$LANE/verify_draft_compliance.py" --rev 8847c7c >"$WORK/rev-bad.out" 2>&1
    bad_rc=$?
    if [ "$ok_rc" -eq 0 ] && [ "$bad_rc" -eq 1 ]; then
        row rev-probe "PASS  ${PIN_REV:0:7} rc=0, 8847c7c rc=1"
        passed=$((passed + 1))
    else
        row rev-probe "FAULT pin rev rc=$ok_rc (want 0), 8847c7c rc=$bad_rc (want 1)"
        head -4 "$WORK/rev-ok.out" "$WORK/rev-bad.out"
        failed=$((failed + 1))
    fi
fi

# --- leg 4: the TSV is exactly what the generator writes at the pin ---------
mkdir -p "$WORK/pin"
if [ -z "$PIN_REV" ] || ! git -C "$ROOT" archive "$PIN_REV" -- wizard-work/quill \
        2>"$WORK/archive.err" | tar -x -C "$WORK/pin" 2>>"$WORK/archive.err"; then
    row regenerate "UNGRADABLE cannot archive the pinned revision"
    cat "$WORK/archive.err"
    ungradable=$((ungradable + 1))
else
    python3 "$WORK/pin/wizard-work/quill/draft_compliance_report.py" \
        >"$WORK/regen.tsv" 2>"$WORK/regen.err"
    gen_rc=$?
    if [ "$gen_rc" -ne 0 ]; then
        row regenerate "FAULT generator rc=$gen_rc"
        cat "$WORK/regen.err"
        failed=$((failed + 1))
    elif cmp -s "$WORK/regen.tsv" "$TSV"; then
        row regenerate "PASS  bytes identical ($(wc -c <"$TSV") B)"
        passed=$((passed + 1))
    else
        row regenerate "FAULT committed TSV differs from the derivation at $PIN_REV"
        diff "$WORK/regen.tsv" "$TSV" | head -10
        failed=$((failed + 1))
    fi
fi

# --- leg 5: the artifact grades a copy of itself, outside any repository ----
mkdir -p "$WORK/copy" || exit 2
if ! git -C "$ROOT" archive "$PIN_REV" -- wizard-work/quill \
        2>"$WORK/copy.err" | tar -x -C "$WORK/copy" 2>>"$WORK/copy.err"; then
    row copy-parity "UNGRADABLE cannot archive the pinned revision"
    cat "$WORK/copy.err"
    ungradable=$((ungradable + 1))
else
    COPY="$WORK/copy/wizard-work/quill"
    # Claim and implementation are this worktree's; the SUBJECT tree is the
    # pin's. That is the split the artifact itself makes.
    cp "$TSV" "$COPY/" && cp "$LANE/draft-compliance-report-2026-10-31.md" "$COPY/" \
        && cp "$LANE/verify_draft_compliance.py" "$LANE/draft_compliance_rule.py" "$COPY/"
    if python3 "$COPY/verify_draft_compliance.py" --worktree >"$WORK/copy-clean.out" 2>&1; then
        clean_rc=0
    else
        clean_rc=$?
    fi
    # move one row's line count in the COPY's TSV: the copy must notice
    awk -F'\t' 'BEGIN{OFS="\t"} NR==3{$2=$2+1} {print}' "$COPY/draft-compliance-report-2026-10-31.tsv" \
        >"$WORK/mut.tsv" && mv "$WORK/mut.tsv" "$COPY/draft-compliance-report-2026-10-31.tsv"
    if python3 "$COPY/verify_draft_compliance.py" --worktree >"$WORK/copy-mut.out" 2>&1; then
        mut_rc=0
    else
        mut_rc=$?
    fi
    if [ "$clean_rc" -eq 0 ] && [ "$mut_rc" -eq 1 ]; then
        row copy-parity "PASS  pin copy rc=0, mutated copy rc=1"
        passed=$((passed + 1))
    else
        row copy-parity "FAULT clean copy rc=$clean_rc (want 0), mutated rc=$mut_rc (want 1)"
        head -5 "$WORK/copy-clean.out" "$WORK/copy-mut.out"
        failed=$((failed + 1))
    fi
fi

echo
echo "CHECK-quill-draft-compliance: $passed leg(s) passed, $failed fault(s), $ungradable ungradable"
[ "$failed" -eq 0 ] || exit 1
[ "$ungradable" -eq 0 ] || exit 2
exit 0
