#!/bin/sh
# HOVO Evidence Pack Verifier — v0.1.0
# Usage: ./verify.sh [directory]
# Verifies SHA-256 integrity of evidence pack files against manifest.json
#
# DISCLAIMER: This verifier checks file integrity only.
# It does not validate the truth of decision inputs.
# Sample data is synthetic — for evaluation purposes only.

set -e

DIR="${1:-.}"
MANIFEST="$DIR/manifest.json"

if [ ! -f "$MANIFEST" ]; then
  echo "ERROR: manifest.json not found in $DIR"
  exit 1
fi

echo "HOVO Evidence Pack Verifier v0.1.0"
echo "==================================="
echo ""

ERRORS=0

for FILE in $(grep -o '"[^"]*\.jsonl"' "$MANIFEST" | tr -d '"'); do
  FILEPATH="$DIR/$FILE"
  if [ ! -f "$FILEPATH" ]; then
    echo "FAIL: $FILE — file not found"
    ERRORS=$((ERRORS + 1))
    continue
  fi

  EXPECTED=$(grep -A1 "\"$FILE\"" "$MANIFEST" | grep sha256 | grep -o '"[a-f0-9]\{64\}"' | tr -d '"')
  ACTUAL=$(shasum -a 256 "$FILEPATH" | cut -d' ' -f1)

  if [ "$EXPECTED" = "$ACTUAL" ]; then
    echo "PASS: $FILE — SHA-256 matches"
  else
    echo "FAIL: $FILE — expected $EXPECTED, got $ACTUAL"
    ERRORS=$((ERRORS + 1))
  fi
done

echo ""
if [ "$ERRORS" -eq 0 ]; then
  echo "All files verified successfully."
else
  echo "VERIFICATION FAILED — $ERRORS error(s)"
  exit 1
fi
