#!/bin/sh
set -eu

: "${PRE15_DUT:?PRE15_DUT is required}"
: "${PRE15_CASE_TMP:?PRE15_CASE_TMP is required}"
: "${PRE15_LIB_DIR:?PRE15_LIB_DIR is required}"
. "$PRE15_LIB_DIR/runner.sh"
umask 022

spec=$PRE15_DUT/tests/pre15/fixtures/B01-g3-archives.json
helper=$PRE15_DUT/tests/pre15/fixtures/g3.py
metadata_archive=$PRE15_DUT/tests/results/manual/2026-08-08T2337Z-metadata-vfs/prepare-fixtures.sh
final_archive=$PRE15_DUT/tests/results/manual/2026-08-09T0124Z-final-review/prepare-fixtures.sh

for tool in mkfs.erofs fsck.erofs dump.erofs python3 sha256sum; do
	command -v "$tool" >/dev/null 2>&1 || \
		pre15_infra_blocked "missing G3 equivalence tool: $tool"
done
test "$(id -u)" -eq 0 || \
	pre15_infra_blocked 'root is required for deterministic device-node fixtures'

python3 - "$spec" "$PRE15_DUT" <<'PY'
import hashlib
import json
from pathlib import Path
import sys

spec = json.loads(Path(sys.argv[1]).read_text(encoding="ascii"))
dut = Path(sys.argv[2])
for item in spec["archives"]:
    path = dut / item["path"]
    data = path.read_bytes()
    digest = hashlib.sha256(data).hexdigest()
    lines = len(data.splitlines())
    if digest != item["sha256"] or lines != item["lines"]:
        raise SystemExit(
            f"{item['id']}: hash/line mismatch {digest}/{lines}, "
            f"expected {item['sha256']}/{item['lines']}"
        )
PY

pre15_record_fixture g3-archive-spec "$spec"
pre15_record_fixture g3-stable-helper "$helper"
pre15_record_fixture g3-archive-metadata "$metadata_archive"
pre15_record_fixture g3-archive-final "$final_archive"

mkdir "$PRE15_CASE_TMP/archive-metadata-source" "$PRE15_CASE_TMP/archive-metadata"
rmdir "$PRE15_CASE_TMP/archive-metadata-source" "$PRE15_CASE_TMP/archive-metadata"
FIXTURE_DIR="$PRE15_CASE_TMP/archive-metadata-source" \
ARTIFACT_DIR="$PRE15_CASE_TMP/archive-metadata" "$metadata_archive"
find "$PRE15_CASE_TMP/archive-metadata-source/namei/wide" \
	-mindepth 1 -maxdepth 1 -type f -printf '%f\n' | LC_ALL=C sort \
	> "$PRE15_CASE_TMP/archive-metadata/expected-wide.txt"

python3 -B "$helper" make-metadata \
	--source "$PRE15_CASE_TMP/stable-a-metadata-source" \
	--output "$PRE15_CASE_TMP/stable-a-metadata"
python3 -B "$helper" make-metadata \
	--source "$PRE15_CASE_TMP/stable-b-metadata-source" \
	--output "$PRE15_CASE_TMP/stable-b-metadata"
mkdir -p "$PRE15_RUN_DIR/artifacts"
python3 -B "$helper" compare-set \
	--left-source "$PRE15_CASE_TMP/archive-metadata-source" \
	--left-artifacts "$PRE15_CASE_TMP/archive-metadata" \
	--right-source "$PRE15_CASE_TMP/stable-a-metadata-source" \
	--right-artifacts "$PRE15_CASE_TMP/stable-a-metadata" \
	--output "$PRE15_RUN_DIR/artifacts/metadata-archive-equivalence.json"
python3 -B "$helper" compare-set \
	--left-source "$PRE15_CASE_TMP/stable-a-metadata-source" \
	--left-artifacts "$PRE15_CASE_TMP/stable-a-metadata" \
	--right-source "$PRE15_CASE_TMP/stable-b-metadata-source" \
	--right-artifacts "$PRE15_CASE_TMP/stable-b-metadata" \
	--output "$PRE15_RUN_DIR/artifacts/metadata-repeat-equivalence.json"

FIXTURE_DIR="$PRE15_CASE_TMP/archive-final-source" \
ARTIFACT_DIR="$PRE15_CASE_TMP/archive-final" "$final_archive"
python3 -B "$helper" make-final \
	--source "$PRE15_CASE_TMP/stable-a-final-source" \
	--output "$PRE15_CASE_TMP/stable-a-final"
python3 -B "$helper" make-final \
	--source "$PRE15_CASE_TMP/stable-b-final-source" \
	--output "$PRE15_CASE_TMP/stable-b-final"
python3 -B "$helper" compare-set \
	--left-source "$PRE15_CASE_TMP/archive-final-source" \
	--left-artifacts "$PRE15_CASE_TMP/archive-final" \
	--right-source "$PRE15_CASE_TMP/stable-a-final-source" \
	--right-artifacts "$PRE15_CASE_TMP/stable-a-final" \
	--output "$PRE15_RUN_DIR/artifacts/final-archive-equivalence.json"
python3 -B "$helper" compare-set \
	--left-source "$PRE15_CASE_TMP/stable-a-final-source" \
	--left-artifacts "$PRE15_CASE_TMP/stable-a-final" \
	--right-source "$PRE15_CASE_TMP/stable-b-final-source" \
	--right-artifacts "$PRE15_CASE_TMP/stable-b-final" \
	--output "$PRE15_RUN_DIR/artifacts/final-repeat-equivalence.json"

for evidence in \
	metadata-archive-equivalence.json metadata-repeat-equivalence.json \
	final-archive-equivalence.json final-repeat-equivalence.json; do
	pre15_record_fixture "g3-$evidence" "$PRE15_RUN_DIR/artifacts/$evidence"
done

python3 - "$PRE15_RUN_DIR/artifacts" <<'PY'
import json
from pathlib import Path
import sys

root = Path(sys.argv[1])
names = (
    "metadata-archive-equivalence.json",
    "metadata-repeat-equivalence.json",
    "final-archive-equivalence.json",
    "final-repeat-equivalence.json",
)
for name in names:
    result = json.loads((root / name).read_text(encoding="ascii"))
    if result["status"] != "PASS":
        raise SystemExit(f"{name}: {result['status']}")
print("G3 archived=548 lines; source/artifact bytes and expectations are equivalent")
PY

pre15_target_reached
