#!/bin/bash
set -evu

apt-get update
apt-get upgrade -yqq

./bootstrap
./configure CFLAGS="-ggdb -O0" \
      --prefix=/usr/local \
      --libdir=lib \
	    --enable-logging=verbose \
	    --disable-doc
make
make install

PG_BIN="$(pg_config --bindir)"
PG_ETC="$(pg_config --sharedir | sed 's|/usr/share/postgresql|/etc/postgresql|')"
sudo -u postgres "${PG_BIN}/postgres" -D ${PG_ETC}/main -h localhost -p 5432 &

sleep 10
sudo -u postgres createuser -p 5432 root
sudo -u postgres createdb -p 5432 -O root donaucheck

check_command()
{
	# Set LD_LIBRARY_PATH so tests can find the installed libs
	LD_LIBRARY_PATH=/usr/local/lib PGPORT=5432 make check
}

print_logs()
{
    set +e
	log=build/meson-logs/testlog.txt
		echo "Printing ${log}"
        cat "$log"
}


if ! check_command ; then
	print_logs
	exit 1
fi
