#!/bin/sh

set -eux

if [ $(id -u) = 0 ]; then
    rm -f test/sql/*-*.sql # testsuite writes to these files in the main tree
    chmod 777 . test/sql # make accessible for postgres
    exec su -c $0 postgres
fi

EXIT=0

for v in $(pg_buildext installed-versions); do
    echo "### PostgreSQL $v ###"
    rm -rf test-$v
    mkdir test-$v
    cmake -B test-$v -DPG_CONFIG=/usr/lib/postgresql/$v/bin/pg_config -DAPACHE_ONLY=1
    if ! make -C test-$v/ installcheck PSQL=/usr/lib/postgresql/$v/bin/psql; then
        tail -n1000 test-$v/test/log/*.log test-$v/test/regression.diffs || :
        EXIT=1
    fi
done

exit $EXIT
