#!/bin/sh

set -e

SRCDIR=$(pwd)

# Make sure to have a writable copy of the source
WORKDIR=$(mktemp -d -t regresscheck.XXXXXX)
cleanup () {
	mkdir -p $SRCDIR/tmp_check/log
	cp -a $WORKDIR/tmp_check/log/* $SRCDIR/tmp_check/log
	rm -rf $WORKDIR
}
trap cleanup 0 HUP INT QUIT ILL ABRT PIPE TERM
cp -r $SRCDIR/* $WORKDIR/
cd $WORKDIR

# If executed as root become nobody
if [ $(id -u) -eq 0 ]
then
        chown nobody -R $WORKDIR/
        SU='su nobody -p -c'
else
        SU='bash -c'
fi

# Execute pg_prove for all the supported versions
for v in $(pg_buildext supported-versions); do
        if [ "$v" = "9.4" ]; then continue; fi
        VERSION_PATH="/usr/lib/postgresql/$v/bin:$PATH"
        if ! $SU "PATH=$VERSION_PATH pg_prove -I /usr/lib/postgresql/$v/lib/pgxs/src/makefiles/../../src/test/perl/ -ovf \
			t/010_pglogical_create_subscriber.pl \
			t/020_non_default_replication_set.pl"; then
                set +e
                for log in tmp_check/log/*.log; do
                        echo "*** $log (last 100 lines) ***"
                        tail -100 $log
                done
                exit 1
        fi
done
