#!/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=c750850264cbc3c52ef4407aaf0c8d02ec26e973
fixture=$PRE15_DUT/tests/pre15/fixtures/B25-codec-errors.json
oracle=$PRE15_DUT/tests/pre15/fixtures/B25-codec-oracle.py
artifacts=$PRE15_RUN_DIR/artifacts

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

pre15_record_fixture b25-case "$PRE15_DUT/tests/pre15/cases/B25-codec-errors.sh"
pre15_record_fixture b25-fixture "$fixture"
pre15_record_fixture b25-oracle "$oracle"
for source in compress.h decompressor.c decompressor_lz4.c \
    decompressor_lzma.c decompressor_deflate.c decompressor_zstd.c zdata.c; do
	pre15_record_fixture "b25-$source" "$PRE15_DUT/src/$source"
done

mkdir -p "$artifacts"
pre15_target_reached

if python3 -B "$oracle" --root "$PRE15_ROOT" --dut "$PRE15_DUT" \
    --baseline "$baseline" --fixture "$fixture" --artifacts "$artifacts"
then
	pre15_record_fixture b25-report "$artifacts/B25-codec-errors.json"
else
	pre15_dut_fail 'B25 typed codec errno oracle failed'
fi
