#!/usr/bin/env bash
# Drive one OPG run through ASSESS -> PLAN -> APPROVE (real RSA signatures) -> APPLY
# against OPG's own fake Oracle Home, then replay APPLY with one thing tampered each time.
# Reuses the helper functions (setup_case, assess, plan, approval, guard) from OPG's
# tests/run_tests.sh so the fixture is exactly the one upstream tests against.
set -u
cd /opt/opg/project
export OPG_KEEP_TEST_TMP=1
source <(sed -n '1,/^# 1\. Gezonde/p' tests/run_tests.sh | sed '$d') >/dev/null
ROOT=/opt/opg/project   # the harness derives ROOT from BASH_SOURCE, which is /proc/... under process substitution
result() { tail -n 1 "$1"; }

scenario() {   # scenario NAME RUN_ID TAMPER_FN
  local name=$1 run=$2 tamper=${3:-}
  setup_case "$name" healthy_single     # one running, autostart=Y database DB1
  assess "$run" >/dev/null; plan "$run" >/dev/null
  TEST_SIGN_APPROVAL=1; token=$(approval "$run"); unset TEST_SIGN_APPROVAL
  printf '\nTEST_REQUIRE_SIGNATURES=true\n' >>"$FIXTURE_ENV"
  [[ -n "$tamper" ]] && "$tamper" "$run"
  guard apply --non-interactive --run-id "$run" \
    --approved-manifest "$RUN_ROOT/$run/patch_manifest.json" --approval-token "$token" >"$CASE_DIR/apply.out" 2>&1
  local rc=$?
  cp "$CASE_DIR/apply.out" "/out/$name.apply.out"
  local downtime=no
  compgen -G "$RUN_ROOT/$run/shutdown_*.log" >/dev/null && downtime=YES
  printf '%-28s rc=%-3s downtime_started=%-4s %s\n' "$name" "$rc" "$downtime" "$(grep -m1 -oE 'APPROVAL CHECK BLOCKED: .*|STATIC_CONTEXT_MISMATCH\|check=[a-z0-9_]+' "$CASE_DIR/apply.out")"
  printf '%-28s %s\n' '' "$(result "$CASE_DIR/apply.out" | grep -oE 'status=.*')"
}

t_manifest()  { chmod u+w "$RUN_ROOT/$1/patch_manifest.json"; sed -i 's/"db_patch": "39472050"/"db_patch": "39472051"/' "$RUN_ROOT/$1/patch_manifest.json"; }
t_manifest_and_sidecar() { t_manifest "$1"; chmod u+w "$RUN_ROOT/$1/patch_manifest.sha256"; sha256sum "$RUN_ROOT/$1/patch_manifest.json" | awk '{print $1}' >"$RUN_ROOT/$1/patch_manifest.sha256"; }
t_expired()   { sed -i 's/"expires_epoch": [0-9]*/"expires_epoch": 1/' "$token"
                openssl dgst -sha256 -sign "$CASE_DIR/approval-private.pem" -out "$RUN_ROOT/$1/approval.sig" "$token"; }
t_token()     { sed -i 's/"expires_epoch": [0-9]*/"expires_epoch": 4102444800/' "$token"; }
t_rogue_key() { openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$CASE_DIR/rogue.pem" 2>/dev/null
                openssl dgst -sha256 -sign "$CASE_DIR/rogue.pem" -out "$RUN_ROOT/$1/patch_manifest.sig" "$RUN_ROOT/$1/patch_manifest.json"
                openssl dgst -sha256 -sign "$CASE_DIR/rogue.pem" -out "$RUN_ROOT/$1/approval.sig" "$token"; }
t_swap_pubkey() { t_rogue_key "$1"; openssl pkey -in "$CASE_DIR/rogue.pem" -pubout -out "$CASE_DIR/approval-public.pem" 2>/dev/null; }
t_media()     { printf 'changed\n' >>"$PATCH_ROOT/JUL2026/39472050/payload.bin"; }

echo "== happy path, artifacts kept for inspection"
scenario signed-happy H1
cp -r "$RUN_ROOT/H1" /out/H1 2>/dev/null || true
echo; echo "== tamper replays (each starts from a fresh, validly signed run)"
scenario manifest-edited   T1 t_manifest
scenario manifest-sidecar   T1B t_manifest_and_sidecar
scenario token-edited      T2 t_token
scenario token-expired     T2B t_expired
scenario rogue-signer      T3 t_rogue_key
scenario pubkey-swapped    T4 t_swap_pubkey
scenario media-drift       T5 t_media
