#!/bin/sh

DB=postgis_reg

###################################################
#
# Usage ./run_test <testname> [<testname>]
#
# Create the spatial database 'postgis_reg'
# (or whatever $DB is set to) if it doesn't
# already exist.
#
# Run the <testname>.sql script
# Diff output against <testname>_expected
#
#
###################################################

PGOPTIONS="${PGOPTIONS} -c lc_messages=C"
export PGOPTIONS

if [ -z "$TMPDIR" ]; then
	TMPDIR=/tmp
fi

VERBOSE=0
OPT_DROP=yes
OPT_CREATE=yes
while [ -n "$1" ]; do

	if test "$1" = "-v"; then
		VERBOSE=1
		shift
		continue
	elif test "$1" = "--nodrop"; then
		OPT_DROP=no
		shift
		continue
	elif test "$1" = "--nocreate"; then
		OPT_CREATE=no
		shift
		continue
	else
		break
	fi
done

if [ -z "$1" ]; then
	echo "Usage: $0 [-v] [--nocreate] [--nodrop] <test> [<test>]" >&2
	exit 1
fi


db_exists=`psql -l | grep -w ${DB}`

if test -z "$db_exists"; then

	if test x"$OPT_CREATE" = "xyes"; then
		echo "Creating spatial db ${DB} " 

		createdb $DB > ${TMPDIR}/regress_log_$$
		createlang plpgsql $DB >> ${TMPDIR}/regress_log_$$ 
		psql -f lwpostgis.sql $DB >> ${TMPDIR}/regress_log_$$ 2>&1
	else

		echo "Database ${DB} does not exist" >&2
		echo "Run w/out the --nocreate flag to create it" >&2
		exit 1
	fi
else
	if test x"$OPT_CREATE" = "xyes"; then
		echo "Database ${DB} already exist." >&2
		echo "Run with the --nocreate flag to use it " \
			"or drop it and try again." >&2
		exit 1
	else
		echo "Using existing database ${DB}"
	fi
fi

libver=`psql -tAc "select postgis_lib_version()" $DB`

if [ -z "$libver" ]; then
	echo
	echo " Something went wrong (no postgis installed in $DB)." 
	if [ -z "$db_exists" ]; then
		echo " Check ${TMPDIR}/regress_log_$$ for details."
	else
		echo " Try dropping the database, it will be recreated" \
			" on next run."
	fi
	echo
	exit 1
fi

geosver=`psql -tAc "select postgis_geos_version()" $DB`
jtsver=`psql -tAc "select postgis_jts_version()" $DB`
projver=`psql -tAc "select postgis_proj_version()" $DB`
libbuilddate=`psql -tAc "select postgis_lib_build_date()" $DB`
pgsqlver=`psql -tAc "select version()" $DB`

echo
echo " $pgsqlver"
echo " Postgis $libver - $libbuilddate"
if [ -n "$geosver" ]; then
	echo "   GEOS: $geosver"
fi
if [ -n "$jtsver" ]; then
	echo "   JTS: $jtsver"
fi
if [ -n "$projver" ]; then
	echo "   PROJ: $projver"
fi

echo 
echo "Running tests"
echo

RUN=0
SKIP=0
FAIL=0
SUCCESS=0
while [ -n "$1" ]; do
	TEST="$1"; shift;

	# catch a common mistake (strip trailing .sql)
	TEST=`echo "$TEST" | sed 's/\.sql$//'`

	OUTFILE="${TMPDIR}/regress_${TEST}_out_$$"
	TMPFILE="${TMPDIR}/regress_${TEST}_tmp_$$"
	DIFFILE="${TMPDIR}/regress_${TEST}_diff_$$"

	printf %20s " ${TEST}: "
	#echo -ne " ${TEST}:\t"

	if [ ! -r "${TEST}.sql" ]; then
		echo "Skipped (can't read ${TEST}.sql)"
		SKIP=`expr $SKIP + 1`
		continue
	fi

	if [ ! -r "${TEST}_expected" ]; then
		echo " Skipped (can't read ${TEST}_expected)"
		SKIP=`expr $SKIP + 1`
		continue
	fi

	# Use intermediate file to prevent MingW buffering problems
	psql -tA < "${TEST}.sql" $DB > ${TMPFILE} 2>&1
	cat ${TMPFILE} \
		| grep -v "^$" \
		| grep -v "^INSERT" \
		| grep -v "^UPDATE" \
		| grep -v "^DROP" \
		| grep -v "^CREATE" \
		| grep -v "^SET" \
		| sed 's/Infinity/inf/g;s/Inf/inf/g;s/1\.#INF/inf/g' \
		| sed 's/[eE]\([+-]\)0\{1,\}\([0-9]\{1,\}\)/e\1\2/g' \
		| sed 's/Self-intersection .*/Self-intersection/' \
		> "${OUTFILE}"
	rm ${TMPFILE}

	if diff "${TEST}_expected" "${OUTFILE}" > ${DIFFILE}; then
		SUCCESS=`expr $SUCCESS + 1`
		echo "Ok."
		rm "${OUTFILE}" "${DIFFILE}" # we don't need these anymore
	else
		FAIL=`expr $FAIL + 1`
		if test "$VERBOSE" -eq "1"; then
			echo "Failed (diff ${DIFFILE})"
			echo "-----------------------------------------------------------------------------"
			echo "expected:'<'     obtained:'>'"
			echo "-----------------------------------------------------------------------------"
			cat ${DIFFILE}
			echo "-----------------------------------------------------------------------------"
		else
			echo "Failed (diff ${DIFFILE})"
		fi
		rm "${OUTFILE}" # diff is enough
	fi
	RUN=`expr $RUN + 1`

done

echo
echo "Run tests: $RUN"
#echo "Skipped: $SKIP"
echo "Successful: $SUCCESS"
echo "Failed: $FAIL"

if test x"$OPT_DROP" = "xyes" -a x"$OPT_CREATE" = "xyes"; then
	sleep 1
	dropdb $DB > /dev/null
else
	: echo "Drop database ${DB} manually"
fi

