#!/bin/bash
# CHECK-q116: grade Q116 -- no career-lane/ cell quotes OpenTrain's
# `validThrough` as an expiry or a remaining-days count.
#
# Two legs, both run here: `--selftest` (the scanner's own cells, no repo
# needed) and the repo scan (career-lane/ + the Q116 addendum grant it reads
# from WORK-QUEUE.md). The scan prints every exemption with the record that
# grants it, so a marker that exempts too much is a reading a human can
# overturn rather than a silence.
#
# Exit codes follow the repo's tools/ contract:
#   0 = the grep half holds and the cells pass
#   1 = a graded fault (a live deadline quote, or a failing cell)
#   2 = could not grade (no python3, no career-lane/, no queue, no cells)
#
# Paths resolve from this file's own location, so a clone grades its own tree.
set -u
set -o pipefail
HERE="$(dirname "$(readlink -f "$0")")"

if ! command -v python3 >/dev/null 2>&1; then
    echo "CHECK: python3 not on PATH -- cannot grade." >&2
    exit 2
fi

python3 "$HERE/opentrain_deadline_audit.py" --selftest >/tmp/.q116-cells.out 2>&1
rc=$?
tail -1 /tmp/.q116-cells.out
case $rc in
    0) ;;
    1) echo "CHECK: cells failed -- see /tmp/.q116-cells.out" >&2; exit 1 ;;
    *) echo "CHECK: cells could not run (rc $rc)." >&2; exit 2 ;;
esac

python3 "$HERE/opentrain_deadline_audit.py"
case $? in
    0) echo "CHECK: q116 grep half holds."; exit 0 ;;
    1) echo "CHECK: live deadline quote in career-lane/ -- see the list above." >&2; exit 1 ;;
    *) echo "CHECK: scan could not grade -- see the message above." >&2; exit 2 ;;
esac
