#!/bin/sh -e

# Create and start a standard 7.4 cluster which should land on the default
# port. First try to start it with an invalid user, which should fail with an
# error. After starting it properly, we check that the postmaster actually runs
# and the correct socket directory. It also gets a nonstandard socket
# directory.

SOCKETDIR=/tmp/postgresql-testsuite/

rm -rf $SOCKETDIR
install -d -m 755 -o postgres -g root $SOCKETDIR
pg_createcluster --socketdir $SOCKETDIR 7.4 pg74

# Chown cluster to an invalid user to test error
chown -R 99:99 /var/lib/postgresql/7.4/pg74
if pg_ctlcluster -s 7.4 pg74 start; then
    echo "pg_ctlcluster with an invalid cluster owner uid should fail, but didn't"
    exit 1
fi
chown -R 99:postgres /var/lib/postgresql/7.4/pg74
if pg_ctlcluster -s 7.4 pg74 start; then
    echo "pg_ctlcluster with an invalid cluster owner gid should fail, but didn't"
    exit 1
fi

# Now start it properly
chown -R postgres:postgres /var/lib/postgresql/7.4/pg74
pg_ctlcluster -s 7.4 pg74 start

pg_lsclusters

echo "Default socket directory /var/run/postgresql:"
ls -a /var/run/postgresql/
echo "This cluster's socket directory $SOCKETDIR:"
ls -a $SOCKETDIR

su -s /bin/sh -c 'psql --version' postgres
su -s /bin/sh -c 'psql -l' postgres
