#!/bin/sh
set -e

: ${srcdir=.}
. "$srcdir"/shlib

casename="$1"

playback_prepare

ofuzz="fuzz-$casename"
ofuzzraw="fuzzraw-$casename"

rm -f "$ofuzz.tmp"
rm -f "$ofuzzraw.tmp"
exec >"$ofuzz.tmp"
exec 6>"$ofuzzraw.tmp"

fuzzout_complete () {
	mv "$ofuzz.tmp" "$ofuzz"
	mv "$ofuzzraw.tmp" "$ofuzzraw"
	exit 0
}

if test ! -e $playback; then
	echo 0
	echo 0 >&6
	fuzzout_complete
fi

dump_args () {
	printf "%d\n" "$#"
	for a in "$@"; do
		printf "%d\n" "${#a}"
		printf "%s\n" "$a"
	done

	if test -e "$case.in"
	then
		inputsz=$(stat -c'%s' -L "$case.in")
		printf "%d\n" "$inputsz"
		cat "$case.in"
	else
		echo 0
	fi

	echo
}	

playback_execute_hook () {
	dump_args "${program}_fuzz"    "$@"
	dump_args "${program}_fuzzraw" "$@" >&6
	cat <&4 >"$ofuzz.sys.tmp"
	exec 4<$ofuzz.sys.tmp
	ADNS_TEST_FUZZRAW_DUMP_FD=6
	export ADNS_TEST_FUZZRAW_DUMP_FD
}
playback_execute

cat $ofuzz.sys.tmp

fuzzout_complete
