#!/sbin/sh
# postgresql    This is the init script for starting the PostgreSQL
#               server.  Solaris version.

# Version 7.0.3, 2001/03/02
# Frank Byrum <byrum@greatbridge.com>
# 
# Version 7.0.3-2, 2001/04/01
# Modified by Justin Clift <justin@postgresql.org> to work with a compiled
# version of PostgreSQL 7.0.3.  This will no longer work with the Greatbridge
# package due to path changes.
#
# Version 7.0.3-3 2001/04/08
# Modified by Justin Clift <justin@postgresql.org> after discussion with Frank
# Byrum to assume a different default installation path.
#
# Version 7.1-0 2001/04/15
# Modified by Justin Clift <justin@postgresql.org> after further discussion
# with Frank Byrum to assume a different default installation path.  Also
# updated to work with PostgreSQL 7.1.
#
# Version 7.1-1 2001/05/06
# Modified by Justin Clift <justin@postgresql.org> after discussion with Chris
# Winterrowd.  Now sets the startup options correctly and can direct output
# to a logfile.
#
# Version 7.1.1-0 2001/05/06
# Modified by Justin Clift <justin@postgresql.org> to work with PostgreSQL 7.1.
#
# Version 7.1.2-0 2001/07/18
# Modified by Justin Clift <justin@postgresql.org> to work with PostgreSQL 7.1.2
# and stop TCP/IP connections from being accepted as default.
#
# Version 7.1.3-0 2001/11/15
# Modified by Justin Clift <justin@postgresql.org> to work with PostgreSQL 7.1.3
#
# Version 7.3.1-0 2003/01/08
# Modified by Justin Clift <justin@postgresql.org> to work with PostgreSQL 7.3.1
# and be included with the Solaris packages.  Now uses a path of /opt/pgsql to
# keep things simple, and logs to /var/adm/pgsql/pgsql.log
#
# Version 7.3.2-0 2003/02/08
# Modified by Justin Clift <justin@postgresql.org> to work with PostgreSQL 7.3.2
#
# The version of PostgreSQL this is for
PGVERSION=7.3.2

PGHOME=/opt/pgsql
BINDIR=$PGHOME/bin
POSTGRES=$BINDIR/postgres
INITDB=$BINDIR/initdb
PGCTL=$BINDIR/pg_ctl
POSTMASTER=$BINDIR/postmaster
PGUSER=postgres
PATH=$BINDIR:$PATH
PGDATA=$PGHOME/data
export PATH PGDATA

PGCONF=$PGDATA/postmaster.conf

ERRMSG1="ERROR: PostgreSQL not installed in $PGHOME!"
ERRMSG2="ERROR: 'pg_ctl' program not found in $BINDIR!"
ERRMSG3="ERROR: PostgreSQL already running!"
ERRMSG4="ERROR: PostgreSQL not started!"
ERRMSG5="ERROR: Could not create PostgreSQL data directory for initdb!"
ERRMSG6="ERROR: Could not initialise PostgreSQL data directory!"

OKMSG1="PostgreSQL: $PGDATA Initalized."
OKMSG2="PostgreSQL: $PGVERSION has started."

if [ ! -d "$PGHOME" ]; then
	/usr/bin/logger -p daemon.err  $ERRMSG1
	exit 2
fi


case "$1" in 
'start')

	# If not found	
	if [ ! -f "$PGCTL" ]; then
		/usr/bin/logger -p daemon.err $ERRMSG2	
		exit 3
	fi

	# check to see if PostgreSQL is already running
	pid=`pgrep postgres`
	rtn=$?
	if [ "$rtn" -eq 0 ]; then
		/usr/bin/logger -p daemon.err $ERRMSG3
		exit 4
	fi

	# No existing PGDATA, so create it 
	if [ ! -d "$PGDATA" ]; then
		/usr/bin/mkdir -p $PGDATA
		rtn=$?
		if [ ! "$rtn" ]; then
			/usr/bin/logger -p daemon.err $ERRMSG5
			exit 6
		fi
		/usr/bin/chmod 700 $PGDATA
		/usr/bin/chown postgres:postgres $PGDATA
	fi

	# Initdb the database if needed
	if [ ! -f "$PGCONF" ]; then
		/usr/bin/su - $PGUSER -c "$INITDB -D $PGDATA > /dev/null 2>&1 < /dev/null"
		rtn=$?
		if [ "$rtn" ]; then
			/usr/bin/logger -p daemon.info $OKMSG1
		else
			/usr/bin/logger -p daemon.err $ERRMSG6
			exit 7
		fi
	fi
	# Note this expects the correct environment to be in place
	# for the postgres user, check .profile if things go wrong
	# If you want to enable TCP, adjust the tcpip_socket parameter
	# in the $PGDATA/postgresql.conf file
	/usr/bin/su - $PGUSER -c "$PGCTL start 2>&1 < /dev/null&"
	/usr/bin/sleep 1
	pid=`pgrep postgres`
	rtn=$?
	if [ "$rtn" -eq 0 ]; then
		/usr/bin/logger -p daemon.info $OKMSG2
	else
		/usr/bin/logger -p daemon.err $ERRMSG4
		exit 5
	fi
	;;

'stop')
	/usr/bin/su - $PGUSER -c "$PGCTL stop -m s"
	/usr/bin/sleep 2
	;;

*)
	echo "Usage: $0 { start | stop }"
	exit 1
	;;
esac
exit 0
