#!/bin/sh

set -eux

export LC_ALL=C
export PGBINDIR=$(ls -d /usr/lib/postgresql/*/bin | tail -n1)
export PGDATA="test_database_check_postgres/data space"

cleanup () {
	$PGBINDIR/pg_ctl stop || :
	rm -rf test_database_check_postgres*
	rm -f /tmp/cptesting_socket
}
trap cleanup 0 2 3 15
cleanup

mkdir test_database_check_postgres
$PGBINDIR/initdb
usd=$(grep -o 'unix_socket_dir[a-z]*' "$PGDATA/postgresql.conf") # changed between 9.2 and 9.3
echo "$usd = 'socket'" >> "$PGDATA/postgresql.conf"
mkdir "$PGDATA/socket"
echo "max_connections = 10" >> "$PGDATA/postgresql.conf"
$PGBINDIR/pg_ctl start
sleep 3

psql -h localhost -c 'CREATE USER check_postgres_testing SUPERUSER'

make test
