#!/bin/sh
set -eu

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

baseline=e2e3fb86b6fffcb01d6fd29c17dd95628ad070de
gate=$PRE15_DUT/tests/pre15/gates/P15-081.sh
input=$PRE15_DUT/tests/pre15/gates/P15-081-input.json
spec=$PRE15_DUT/tests/pre15/fixtures/B11-dtype-spec.json
oracle=$PRE15_DUT/tests/pre15/fixtures/B11-dtype-oracle.py
artifacts=$PRE15_RUN_DIR/artifacts
gate_output=$artifacts/gate

for tool in cc dump.erofs fsck.erofs git mkfs.erofs python3 sha256sum; do
	command -v "$tool" >/dev/null 2>&1 || \
		pre15_infra_blocked "missing B11 host tool: $tool"
done

pre15_record_fixture b11-gate "$gate"
pre15_record_fixture b11-gate-input "$input"
pre15_record_fixture b11-spec "$spec"
pre15_record_fixture b11-oracle "$oracle"
for source in inode.c internal.h namei.c dir.c erofs_vnops.c; do
	pre15_record_fixture "b11-$source" "$PRE15_DUT/src/$source"
done

mkdir -p "$artifacts"
if "$gate" --base "$baseline" --output "$gate_output" \
	>"$artifacts/gate.stdout" 2>"$artifacts/gate.stderr"; then
	:
else
	pre15_runner_fail 'P15-081 frozen gate replay failed'
fi

pre15_target_reached
if python3 -B "$oracle" --root "$PRE15_ROOT" --dut "$PRE15_DUT" \
	--spec "$spec" --gate-output "$gate_output" --artifacts "$artifacts"
then
	pre15_record_fixture b11-report "$artifacts/B11-dtype-report.json"
else
	pre15_dut_fail 'B11 dtype source or fixture oracle failed'
fi
