#!/usr/bin/env bash
#
# Verify the ASOR delegation-chain vectors against the reference
# implementation's own interoperability corpus.
#
# The corpus lives in the reference repository, not in this package, so a plain
# `mvn -B test` here SKIPS it and still reports success. This script fetches the
# reference repository at a pinned baseline and runs the corpus for real.
#
# Usage:
#   ./verify-corpus.sh                 clone the reference repo at the pinned
#                                      baseline into ./.attenu-guard, then score
#   ./verify-corpus.sh --corpus PATH   use an existing checkout instead
#   ./verify-corpus.sh --ref REF       clone or check out a different revision
#   ./verify-corpus.sh --no-maven      JDK only; skip the JUnit suite
#
# Environment:
#   ATTENU_GUARD_URL   repository to clone. Default is the upstream GitHub URL;
#                      point it at a mirror or a local path when offline.
#
# What it checks, in order:
#   1. the vectors bundled in this package, scored by tools/Score.java
#   2. the reference corpus at the resolved commit, scored the same way
#   3. the reference implementation's own JUnit suite, if Maven is available
#
# Needs: a JDK 17 or newer, and git for step 2. Maven is optional.
set -euo pipefail

PINNED_REF="e2fd09466f12e3dd66eba8219ef059687e023e39"
REPO_URL="${ATTENU_GUARD_URL:-https://github.com/attenu-io/attenu-guard}"
REF="$PINNED_REF"
CORPUS=""
RUN_MAVEN=1

usage() {
    sed -n '3,26p' "$0" | sed 's/^# \{0,1\}//'
}

while [ $# -gt 0 ]; do
    case "$1" in
        --corpus)   CORPUS="$2"; shift 2 ;;
        --ref)      REF="$2"; shift 2 ;;
        --no-maven) RUN_MAVEN=0; shift ;;
        -h|--help)  usage; exit 0 ;;
        *)          echo "unknown option: $1" >&2; usage >&2; exit 2 ;;
    esac
done

# Find the package root by walking up for the Java sources, so the script works
# both from tools/ inside attenu-guard-java-0.1.0-src.zip and from a checkout
# where it sits next to src/. Published on its own (with no sources anywhere
# above it) it cannot build anything, and says so rather than failing obscurely.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT=""
PROBE="$SCRIPT_DIR"
for _ in 1 2 3 4; do
    if [ -d "$PROBE/src/main/java" ]; then ROOT="$PROBE"; break; fi
    PROBE="$(cd "$PROBE/.." && pwd)"
done
if [ -z "$ROOT" ]; then
    echo "FAIL: no Java sources found. This script has to run from the unzipped" >&2
    echo "      attenu-guard-java-0.1.0-src.zip, e.g. ./tools/verify-corpus.sh." >&2
    exit 1
fi
cd "$ROOT"

command -v javac >/dev/null 2>&1 || { echo "FAIL: javac not found; a JDK 17+ is required" >&2; exit 1; }
command -v java  >/dev/null 2>&1 || { echo "FAIL: java not found" >&2; exit 1; }

# In the source zip the vectors sit at ./vectors; on the download page they are
# one level up from the java/ directory the sources unpack into.
VECTORS=""
for candidate in "$ROOT/vectors" "$ROOT/../vectors"; do
    if [ -d "$candidate" ]; then VECTORS="$candidate"; break; fi
done
if [ -z "$VECTORS" ]; then
    echo "FAIL: no vectors directory found next to this package" >&2
    exit 1
fi

echo "== building with the JDK only (no Maven, no network)"
rm -rf .build
mkdir -p .build/classes
find src/main/java -name '*.java' > .build/sources.txt
javac -d .build/classes @.build/sources.txt
javac -cp .build/classes -d .build/classes tools/Score.java

echo
echo "== 1/3 bundled vectors ($VECTORS)"
java -cp .build/classes Score "$VECTORS"

if [ -z "$CORPUS" ]; then
    CORPUS=".attenu-guard"
    if [ ! -d "$CORPUS/.git" ]; then
        echo
        echo "== fetching the reference implementation"
        git clone --quiet "$REPO_URL" "$CORPUS"
    fi
    git -C "$CORPUS" fetch --quiet origin || true
    if ! git -C "$CORPUS" checkout --quiet "$REF" 2>/dev/null; then
        git -C "$CORPUS" fetch --quiet origin "$REF" && git -C "$CORPUS" checkout --quiet FETCH_HEAD
    fi
else
    if [ ! -d "$CORPUS/tests/vectors" ]; then
        echo "FAIL: $CORPUS does not look like a reference checkout (no tests/vectors)" >&2
        exit 1
    fi
fi

CORPUS_ABS="$(cd "$CORPUS" && pwd)"
RESOLVED="$(git -C "$CORPUS" rev-parse HEAD 2>/dev/null || echo unknown)"

echo
echo "== 2/3 reference corpus at $RESOLVED"
java -cp .build/classes Score "$CORPUS_ABS/tests/vectors"

echo
if [ "$RUN_MAVEN" = 1 ] && command -v mvn >/dev/null 2>&1; then
    echo "== 3/3 reference implementation's own JUnit suite, corpus included"
    mvn -B test -Dattenu.vectors="$CORPUS_ABS/tests/vectors"
else
    echo "== 3/3 skipped: $([ "$RUN_MAVEN" = 0 ] && echo '--no-maven' || echo 'Maven not installed')"
fi

echo
echo "all checks passed"
