#! /bin/sh
# Execute test.

# setup for zcat, echo.
ZCAT='gzip -cd'
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
	if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
		ac_n= ac_c='
' ac_t='	'
	else
		ac_n=-n ac_c= ac_t=
	fi
else
	ac_n= ac_c='\c' ac_t=
fi

# trans "auto" -> "${basename}.{in,out,err}".
if test -z "$program" -o "$program" = "auto"; then program=$basename; fi
if test "x$stdin" = "xauto"; then stdin=${basename}.cin; fi
if test "x$stdout" = "xauto"; then stdout=${basename}.cout; fi
if test "x$stderr" = "xauto"; then stderr=${basename}.cerr; fi

# print for verbose-mode.
test -z "$VERBOSE" && exec > /dev/null 2>&1
echo "=== Running test $0"

echo "  program: $program"
if test -n "$stdout"; then
	stdoutlog=${stdout}.log
else 
	stdoutlog=${program}.cout.log
fi
if test -n "$stderr"; then
	stderrlog=${stderr}.log
else
	stderrlog=${program}.cerr.log
fi
program=./$program

echo $ac_n "  args:    $ac_c"
if test -n "$args"; then echo "$ac_t\"$args\""; else echo "-"; fi

echo $ac_n "  stdin:   $ac_c"
if test -n "$echo"; then
	echo "$ac_t""\"$echo\""
	echo "$echo" > tmp0; stdin_real=tmp0
elif test -n "$stdin"; then
	echo "$ac_t""$stdin"
	if test -r "$stdin"; then
		stdin_real="$stdin"
	else
		stdin_real="${srcdir}/$stdin"
		if test ! -r "$stdin_real"; then
			rm -f tmp0
			if test -r "${stdin_real}.gz"; then
				$ZCAT ${stdin_real}.gz > tmp0
			fi
			stdin_real=tmp0
		fi
	fi
else
	echo "-"
	rm -f tmp0; touch tmp0; stdin_real=tmp0
fi

echo $ac_n "  stdout:  $ac_c"
if test "x$stdout" = "xignore"; then
	echo "$ac_t""(ignore)"
	stdout=ignore
elif test -n "$answer"; then
	echo "$ac_t""\"$answer\""
	echo "$answer" > tmp1; stdout_real=tmp1
elif test -n "$stdout"; then
	echo "$ac_t""$stdout"
	if test -r "$stdout"; then
		stdout_real="$stdout"
	else
		stdout_real="${srcdir}/$stdout"
		if test ! -r "$stdout_real"; then
			rm -f tmp1
			if test -r "${stdout_real}.gz"; then
				$ZCAT ${stdout_real}.gz > tmp1
			fi
			stdout_real=tmp1
		fi
	fi
else
	echo "$ac_t""-"
	rm -f tmp1; touch tmp1; stdout_real=tmp1
fi

echo $ac_n "  stderr:  $ac_c"
if test "x$stderr" = "xignore"; then
	echo "$ac_t""(ignore)"
	stderr=ignore
elif test -n "$error"; then
	echo "$ac_t""\"$error\""
	echo "$error" > tmp2; stderr_real=tmp2
elif test -n "$stderr"; then
	echo "$ac_t""$stderr"
	if test -r "$stderr"; then
		stderr_real="$stderr"
	else
		stderr_real="${srcdir}/$stderr"
		if test ! -r "$stderr_real"; then
			rm -f tmp2
			if test -r "${stderr_real}.gz"; then
				$ZCAT ${stderr_real}.gz > tmp2
			fi
			stderr_real=tmp2
		fi
	fi
else
	echo "$ac_t""-"
	rm -f tmp2; touch tmp2; stderr_real=tmp2
fi

# execute test.
failed=
status=0
cat $stdin_real | $program $args 1> $stdoutlog 2> $stderrlog || status=$?
test "x$status" = "x0" || failed=yes

# print result.
echo "--- result ---"

echo $ac_n "  exec:    $ac_c"
if test -z "$failed"; then
	echo "$ac_t""ok";
else
	echo "$ac_t""bad (status: $status)";
fi

echo $ac_n "  stdout:  $ac_c"
if test "x$stdout" = "xignore"; then
	echo "$ac_t""ok"
	rm -f $stdoutlog
elif test ! -r $stdout_real; then
	echo "$ac_t""bad (missing source file)"
	failed=yes
elif diff $stdout_real $stdoutlog > /dev/null; then
	echo "$ac_t""ok"
	rm -f $stdoutlog
else
	echo "$ac_t""bad (log: $stdoutlog)"
	failed=yes
fi

echo $ac_n "  stderr:  $ac_c"
if test "x$stderr" = "xignore"; then
	echo "$ac_t""ok"
	rm -f $stderrlog
elif test ! -r $stderr_real; then
	echo "$ac_t""bad (missing source file)"
	failed=yes
elif diff $stderr_real $stderrlog > /dev/null; then
	echo "$ac_t""ok"
	rm -f $stderrlog
else
	echo "$ac_t""bad (log: $stderrlog)"
	failed=yes
fi

# exit.
rm -f tmp0 tmp1 tmp2
test -z "$failed"
