#!/usr/bin/env bash # log-gate.sh: refuse to close a run unless its correction log grew. # log-gate.sh begin record the current row count in .baseline # log-gate.sh close exit 0 only if the log now has more rows than the baseline # A row is a table line that starts with a date: "| YYYY-MM-DD |". # Exit: 0 ok · 1 log did not grow · 2 could not measure (missing log or baseline) · 3 usage. set -u count_rows() { grep -cE '^\|[[:space:]]*[0-9]{4}-[0-9]{2}-[0-9]{2}[[:space:]]*\|' "$1"; } [ $# -eq 2 ] || { echo "usage: log-gate.sh begin|close " >&2; exit 3; } cmd="$1"; log="$2"; base="$log.baseline" [ -f "$log" ] || { echo "could not measure: log not found: $log" >&2; exit 2; } now="$(count_rows "$log")" case "$cmd" in begin) echo "$now" > "$base" || { echo "could not measure: cannot write $base" >&2; exit 2; } echo "begin: $now rows recorded in $base" ;; close) [ -f "$base" ] || { echo "could not measure: no baseline, run 'begin' first" >&2; exit 2; } was="$(cat "$base")" case "$was" in ''|*[!0-9]*) echo "could not measure: bad baseline in $base" >&2; exit 2;; esac if [ "$now" -gt "$was" ]; then echo "close: log grew from $was to $now rows" rm -f "$base" else echo "close refused: log has $now rows, baseline $was. Append this run's corrections first." >&2 exit 1 fi ;; *) echo "usage: log-gate.sh begin|close " >&2; exit 3 ;; esac