#!/bin/sh

pgbindir="@pgbindir@"
if [ -z "$pgbindir" ]; then
	PSQL=psql
else
	PSQL="$pgbindir"/psql
fi

echo "Testing stderr log parsing ... "
../pgreplay -f -o parse_err.rf -b '2010-12-31 10:59:52.241' -e '2010-12-31 11:00:30.005' errlog >parse_err.out 2>parse_err.err
if [ $? -ne 0 ]; then
	echo "command failed, error messages:"
	cat parse_err.err
	rm -f parse_err.rf parse_err.out parse_err.err
	exit 1
fi
diff -u -b parse_err.err parse_err.exp >parse_err.diff
if [ $? -ne 0 ]; then
	echo "unexpected messages, difference to expected:"
	cat parse_err.diff
	rm -f parse_err.rf parse_err.out parse_err.err parse_err.diff
	exit 1
fi
diff -u -b parse_err.out parse_err_out.exp >parse_err.diff
if [ $? -ne 0 ]; then
	echo "unexpected output, difference to expected:"
	cat parse_err.diff
	rm -f parse_err.rf parse_err.out parse_err.err parse_err.diff
	exit 1
fi
cmp parse_err.rf replayfile >parse_err.diff
if [ $? -ne 0 ]; then
	echo "unexpected replay file:"
	cat parse_err.diff
	rm -f parse_err.rf parse_err.out parse_err.err parse_err.diff
	exit 1
fi
echo "ok"
rm -f parse_err.rf parse_err.out parse_err.err parse_err.diff

echo "Testing CSV log parsing ... "
../pgreplay -f -c -o parse_csv.rf -b '2010-12-31 10:59:52.241' -e '2010-12-31 11:00:30.005' csvlog >parse_csv.out 2>parse_csv.err
if [ $? -ne 0 ]; then
	echo "command failed, error messages:"
	cat parse_csv.err
	rm -f parse_csv.rf parse_csv.out parse_csv.err
	exit 1
fi
diff -u -b parse_csv.err parse_csv.exp >parse_csv.diff
if [ $? -ne 0 ]; then
	echo "unexpected messages, difference to expected:"
	cat parse_csv.diff
	rm -f parse_csv.rf parse_csv.out parse_csv.err parse_csv.diff
	exit 1
fi
diff -u -b parse_csv.out parse_csv_out.exp >parse_csv.diff
if [ $? -ne 0 ]; then
	echo "unexpected output, difference to expected:"
	cat parse_csv.diff
	rm -f parse_csv.rf parse_csv.out parse_csv.err parse_csv.diff
	exit 1
fi
cmp parse_csv.rf replayfile >parse_csv.diff
if [ $? -ne 0 ]; then
	echo "unexpected replay file:"
	cat parse_csv.diff
	rm -f parse_csv.rf parse_csv.out parse_csv.err parse_csv.diff
	exit 1
fi
echo "ok"
rm -f parse_csv.rf parse_csv.out parse_csv.err parse_csv.diff

echo "Testing replay ... "
"$PSQL" -U postgres -d postgres -l >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "skipped, cannot connect to database"
	echo "To run this test, configure your environment so that this command succeeds:"
	echo "\"$PSQL\" -U postgres -d postgres -l"
	exit 1
fi
../pgreplay -r -s 2 -E UTF8 -d 1 replayfile 2>replay.err|sed '/^Replay statistics$/,$d' >replay.out
if [ $? -ne 0 ]; then
	echo "command failed, error messages:"
	cat replay.err
	rm -f replay.out replay.err
	exit 1
fi
diff -u -b replay.out replay_out.exp >replay.diff
if [ $? -ne 0 ]; then
	echo "unexpected output, difference to expected:"
	cat replay.diff
	rm -f replay.out replay.err replay.diff
	exit 1
fi
diff -u -b replay.err replay_err.exp >replay.diff
if [ $? -ne 0 ]; then
	echo "unexpected messages, difference to expected:"
	cat replay.diff
	rm -f replay.out replay.err replay.diff
	exit 1
fi
echo "Testing dry run ... "
../pgreplay -r -n -E UTF8 replayfile 2>dryrun.err|sed '/calls per second/d' >dryrun.out
if [ $? -ne 0 ]; then
	echo "command failed, error messages:"
	cat dryrun.err
	rm -f dryrun.out dryrun.err
	exit 1
fi
diff -u -b dryrun.out dryrun_out.exp >dryrun.diff
if [ $? -ne 0 ]; then
	echo "unexpected output, difference to expected:"
	cat dryrun.diff
	rm -f dryrun.out dryrun.err dryrun.diff
	exit 1
fi
diff -u -b dryrun.err dryrun_err.exp >dryrun.diff
if [ $? -ne 0 ]; then
	echo "unexpected messages, difference to expected:"
	cat dryrun.diff
	rm -f dryrun.out dryrun.err dryrun.diff
	exit 1
fi

echo "ok"
rm -f replay.out replay.err replay.diff dryrun.out dryrun.err dryrun.diff
exit 0
