#!/bin/sh
set -eu

PRE15_LIB_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/lib" && pwd -P)
PRE15_SMOKE_SCRIPT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)/run-smoke.sh
export PRE15_LIB_DIR PRE15_SMOKE_SCRIPT
. "$PRE15_LIB_DIR/runner.sh"
. "$PRE15_LIB_DIR/manifest.sh"

pre15_smoke_worker()
{
	pre15_require_paths
	pre15_target_reached
	for pre15_smoke_case in SMOKE-PLAIN SMOKE-LZ4 SMOKE-LZMA SMOKE-ZSTD; do
		if PRE15_EVIDENCE_ROOT="$PRE15_RUN_DIR/subruns" \
		    "$PRE15_DUT/tests/pre15/run-qemu.sh" "$pre15_smoke_case"; then
			:
		else
			pre15_smoke_rc=$?
			case "$pre15_smoke_rc" in
			"$PRE15_RC_DUT_FAIL") pre15_dut_fail "$pre15_smoke_case failed its DUT oracle" ;;
			"$PRE15_RC_INFRA_BLOCKED") pre15_infra_blocked "$pre15_smoke_case was infrastructure-blocked" ;;
			*) pre15_runner_fail "$pre15_smoke_case runner failed" ;;
			esac
		fi
	done
}

if test "${1:-}" = --worker; then
	shift
	test "$#" -eq 0 || pre15_fail_usage 'internal smoke worker takes no arguments'
	pre15_smoke_worker
	exit 0
fi

test "$#" -eq 1 && test "$1" = final-four-codec || \
	pre15_fail_usage 'usage: run-smoke.sh final-four-codec'
pre15_run_command smoke final-four-codec "${PRE15_SMOKE_TIMEOUT:-1500}" \
	"$PRE15_SMOKE_SCRIPT" --worker
