#!/bin/sh

set -eux

# re-exec as postgres when running as root
if [ "$(id -un)" = "root" ]; then
    exec su -c "$0" postgres
fi

# put temporary build trees into /tmp
trap "rm -rf /tmp/test-*/" EXIT

export LANG=C.UTF-8
export PGOPTIONS="-c session_preload_libraries=postgis-3"

# pin the PostgreSQL version to the one pg_buildext is iterating over; without
# this, FindPOSTGRESQL.cmake picks pg_config from PATH, which selects whatever
# version is the default (i.e. the newest installed one)
pg_buildext run_installed /usr/bin/cmake -B /tmp/test-%v -DPOSTGRESQL_PG_CONFIG=/usr/lib/postgresql/%v/bin/pg_config
# exclude 'build' test which rebuilds the extension
pg_buildext run_installed /usr/bin/make ARGS="-E build" -C /tmp/test-%v test
