#!/usr/bin/env bash
# Lab step 11: damage copies of a clean audit log three ways and run the verifier on each.
# Usage: 10-tamper.sh <audit.jsonl> <jwks.json> <audit-chain.json>
LOG="$1"; JWKS="$2"; STATE="$3"; cd /lab
V="./verifychain -jwks $JWKS -state $STATE -log"
show() { jq -c '{events, first_invalid_index, missing, truncated, complete}'; }
echo "## untouched";                                  $V "$LOG" | show
echo "## (a) flip one decision from denied to allowed"
awk '!done && /"decision":"denied"/ {sub(/"decision":"denied"/,"\"decision\":\"allowed\""); done=1} {print}' "$LOG" > t-edit.log
$V t-edit.log | show
echo "## (b) delete one event from the middle"
n=$(grep -n '"seq":12,' "$LOG" | cut -d: -f1); sed "${n}d" "$LOG" > t-del.log; $V t-del.log | show
echo "## (c) cut the last three events off the end"
total=$(grep -c '"seq":' "$LOG"); grep '"seq":' "$LOG" | head -n $((total-3)) > t-trunc.log; $V t-trunc.log | show
echo "## (c2) same file, verifier not given the state file"
./verifychain -jwks $JWKS -log t-trunc.log | jq -c '{events, first_invalid_index, missing, truncated, high_water_known, complete}'
