#!/bin/sh

set -e -u

sudo -u postgres psql -c "CREATE EXTENSION IF NOT EXISTS hstore;"
PGVER=$(ls -1d /etc/postgresql/*/main/ | head -n1)

test -d $PGVER || exit 1

echo "host pg8000_md5           all 127.0.0.1/32 md5" | sudo tee -a "$PGVER/pg_hba.conf" >/dev/null
echo "host pg8000_gss           all 127.0.0.1/32 gss" | sudo tee -a "$PGVER/pg_hba.conf" >/dev/null
echo "host pg8000_password      all 127.0.0.1/32 password" | sudo tee -a "$PGVER/pg_hba.conf" >/dev/null
echo "host pg8000_scram_sha_256 all 127.0.0.1/32 scram-sha-256" | sudo tee -a "$PGVER/pg_hba.conf" >/dev/null
echo "host all                  all 127.0.0.1/32 trust" | sudo tee -a "$PGVER/pg_hba.conf" >/dev/null

echo "password_encryption = 'scram-sha-256'" | sudo tee -a "$PGVER/postgresql.conf" >/dev/null

sudo -u postgres psql -c "SELECT pg_reload_conf();"
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'pw';"

for py in $(py3versions -s); do
    echo "Running testsuite with $py:"
    $py -m pytest --ignore=test/test_readme.py --ignore-glob=test/*/auth/test_gss.py
done
