#!/bin/bash

N=`ps ax | grep -v grep | grep "postgres -D" | head -1 | wc -l`
if [ "x$N" = "x1" ]; then exit 0; fi

X=__META_PREFIX__
D=__META_DATADIR__

# This part will only work if this script is executed as superuser.
#
LOGDIR=/var/log/postgresql
mkdir -p $LOGDIR
chown -R postgres.postgres $LOGDIR/ 2> /dev/null

PG_CTL="PATH=$X/bin:$PATH pg_ctl"
LOG1=$LOGDIR/postgres.log
LOG2=$LOGDIR/pg_ctl.log

su postgres -c \
    "$PG_CTL start -D $D -w -l $LOG1 2>&1 >> $LOG2"
sleep 3
