#!/bin/sh
set -eu

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

spec=$PRE15_DUT/tests/pre15/fixtures/B01-xattr-legacy.json
helper=$PRE15_DUT/tests/pre15/fixtures/g3.py
output=$PRE15_CASE_TMP/xattr-legacy
pre15_record_fixture xattr-legacy-contract "$spec"
pre15_record_fixture xattr-legacy-generator "$helper"

if python3 -B "$helper" make-xattr-legacy \
	--spec "$spec" --output "$output" \
	--freebsd-root "$PRE15_DUT/src" --linux-root "$PRE15_ROOT/src-linux"; then
	:
else
	pre15_runner_fail 'legacy xattr fixture generation or independent parsing failed'
fi

mkdir -p "$PRE15_RUN_DIR/artifacts"
cp "$output/fixture-manifest.json" \
	"$PRE15_RUN_DIR/artifacts/TC162-fixture-manifest.json"
pre15_record_fixture xattr-legacy-manifest \
	"$PRE15_RUN_DIR/artifacts/TC162-fixture-manifest.json"
python3 - "$PRE15_RUN_DIR/artifacts/TC162-fixture-manifest.json" <<'PY'
import json
from pathlib import Path
import sys

manifest = json.loads(Path(sys.argv[1]).read_text(encoding="ascii"))
positive = {
    "legacy-primary": "primary-legacy",
    "explicit-plain": "primary",
    "packed-carrier": "packed",
    "metabox-carrier": "metabox",
}
for name, carrier in positive.items():
    item = manifest["results"][name]
    if item["carrier"] != carrier or not item["checksum_valid"]:
        raise SystemExit(f"{name}: carrier/checksum oracle failed")
    if item["base_index"] != 1 or item["infix"] != "repo.pre15.legacy.":
        raise SystemExit(f"{name}: prefix value differs")
for name in ("legacy-length-invalid", "legacy-offset-outside"):
    item = manifest["results"][name]
    if item["expected_errno"] != "EINTEGRITY" or not item["checksum_valid"]:
        raise SystemExit(f"{name}: exact negative oracle failed")
print("TC162: legacy, explicit plain, packed, and metabox carriers read one value")
print("TC162: single-field negatives return exactly EINTEGRITY")
PY

pre15_target_reached
if python3 - "$PRE15_DUT/src/xattr.c" "$PRE15_ROOT/src-linux/xattr.c" <<'PY'
from pathlib import Path
import sys

freebsd = Path(sys.argv[1]).read_text(encoding="utf-8")
linux = Path(sys.argv[2]).read_text(encoding="utf-8")
freebsd_markers = (
    "prefix_en = NULL;",
    "EROFS_FEATURE_COMPAT_PLAIN_XATTR_PFX",
    "else if (em->packed_inode != NULL)",
    "else if (em->packed_nid != 0)",
    "erofs_xattr_read_metadata(em, prefix_en",
)
linux_markers = (
    "bool plain = erofs_sb_has_plain_xattr_pfx(sbi);",
    "else if (sbi->packed_inode)",
    "else\n\t\t\tplain = true;",
    "if (plain)\n\t\t(void)erofs_init_metabuf(&buf, sb, false);",
)
if not all(marker in freebsd for marker in freebsd_markers):
    raise SystemExit("FreeBSD legacy primary fallback changed")
if not all(marker in linux for marker in linux_markers):
    raise SystemExit("Linux legacy primary fallback changed")
print("TC162: FreeBSD and Linux retain the no-carrier primary fallback")
PY
then
	:
else
	pre15_dut_fail 'legacy xattr primary fallback branch no longer matches the frozen contract'
fi
