#!/bin/sh

set -eux

# link shape loader/dumper to where the testsuite expects them
for prg in shp2pgsql pgsql2shp; do
  test -x loader/$prg || ln -s /usr/bin/$prg loader/$prg
done

# delete test directory before possibly switching user
rm -rf /tmp/pgis_reg

# re-start ourselves as nobody since PG can't run as root
if [ "$(id -un)" = "root" ]; then
  exec su nobody -s /bin/sh "$0" "$@"
fi

# Makefile.in needs some variables from ./configure, set the most important ones here
POSTGIS_GEOS_VERSION="$(dpkg-query -f '${Version}' --show 'libgeos-*.*' | cut -c 1,3)"

# clean up on exit
trap "rm -rf /tmp/pgis_reg" EXIT

check_fail="exit 2"
if [ `dpkg --print-architecture` = 's390x' ]; then
    check_fail="exit 0"
fi
if [ `dpkg --print-architecture` = 'armhf' ]; then
    check_fail="exit 0"
fi

# run the regession tests
for v in $(pg_buildext supported-versions); do
  pg_virtualenv -v $v <<-EOF
	set -eux
	make -C regress/core -f Makefile.in check PERL=perl RUNTESTFLAGS="--extension --verbose" POSTGIS_GEOS_VERSION=$POSTGIS_GEOS_VERSION || $check_fail
	make -C regress/sfcgal -f Makefile.in check PERL=perl RUNTESTFLAGS="--extension --verbose" HAVE_SFCGAL=yes || $check_fail
	EOF
done
