#!/bin/sh

set -e

# we want to test the installed extension, so don't source from the build tree
sed -i.orig 's/\\i plsh.sql/CREATE EXTENSION plsh;/' test/sql/init.sql
mv test/expected/init_1.out test/expected/init_1.out.orig
echo "CREATE EXTENSION plsh;" > test/expected/init_1.out
trap "mv test/sql/init.sql.orig test/sql/init.sql;
      mv test/expected/init_1.out.orig test/expected/init_1.out" EXIT HUP INT QUIT PIPE TERM

for version in $(pg_buildext supported-versions); do
	mkdir -p build-$version/results
	test -e build-$version/test || ln -s ../test build-$version
	if ! pg_virtualenv -v $version \
		make -C build-$version -f $PWD/Makefile installcheck \
		PG_CONFIG=/usr/lib/postgresql/$version/bin/pg_config USE_PGXS=1; then
		if [ -r build-$version/regression.diffs ]; then
			echo "**** regression.diffs ****"
			cat build-$version/regression.diffs
		fi
		exit 1
	fi
done
