#!/bin/sh
set -eu

script=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)/B33-qemu-run.sh
source_runner=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)/B32-qemu-run.sh

if test "${1:-}" != --worker; then
	test "$#" -eq 1 && test "$1" = TC168-cache-inflight || {
		printf '%s\n' 'usage: B33-qemu-run.sh TC168-cache-inflight' >&2
		exit 20
	}
	total=${PRE15_QEMU_TIMEOUT:-1200}
	case "$total" in
	''|*[!0-9]*|0) printf '%s\n' 'invalid PRE15_QEMU_TIMEOUT' >&2; exit 20 ;;
	esac
	exec timeout -k 30 "$total" "$script" --worker "$1"
fi

shift
test "$#" -eq 1 && test "$1" = TC168-cache-inflight || exit 20
test -f "$source_runner" || {
	printf '%s\n' 'INFRA_BLOCKED B32 absolute-timeout runner is absent' >&2
	exit 21
}

sed \
	-e "s|^script=.*$|script=$source_runner|" \
	-e 's|case_script=$dut/tests/pre15/cases/B32-cache-state.sh|case_script=$dut/tests/pre15/cases/B33-cache-policy.sh|' \
	-e 's/-qemu-B32-/-qemu-B33-/' \
	"$source_runner" | /bin/sh -s -- --worker "$1"
