#!/bin/sh
#
# PostgreSQL init script for Owl.
#
# chkconfig: - 85 15
# description: Starts and stops the PostgreSQL server
# processname: postmaster
# pidfile: /var/run/postmaster.pid

# Written by Andreas Ericsson <ae@op5.se>
#
# This script is based on the init-file shipped with postgresql-server-7.3.2
# for RedHat Linux 9, but much of the ugly code has been done away with and
# things have been updated to fit the Owl standards (and this particular RPM).
# The unused variables have also been done away with.

PGVERSION=@VERSION@

# Source function library.
. /etc/rc.d/init.d/functions

[ -f /etc/sysconfig/postgresql ] && . /etc/sysconfig/postgresql

# Set defaults for port and database directory
[ -z "$PGPORT" ] && PGPORT=5432
[ -z "$PGDATA" ] && PGDATA=/var/lib/pgsql

# options can only be set in sysconfig file
export PGDATA PGPORT OPTIONS

[ -x /usr/bin/postmaster ] || exit 0

start(){
	PSQL_START=$"Starting PostgreSQL server: "

	# Check for the PGDATA structure
	if [ -f $PGDATA/PG_VERSION -a -d $PGDATA/base ]; then
		# Check version of existing PGDATA

		if [ `cat $PGDATA/PG_VERSION` != '7.3' ]; then
			SYSDOCDIR="(Your System's documentation directory)"
			if [ -d /usr/doc/postgresql-$PGVERSION ]; then
				SYSDOCDIR=/usr/doc
			fi
			if [ -d /usr/share/doc/postgresql-$PGVERSION ]; then
				SYSDOCDIR=/usr/share/doc
			fi
			if [ -d /usr/doc/packages/postgresql-$PGVERSION ]; then
				SYSDOCDIR=/usr/doc/packages
			fi
			if [ -d /usr/share/doc/packages/postgresql-$PGVERSION ]; then
				SYSDOCDIR=/usr/share/doc/packages
			fi
			echo
			echo $"An old version of the database format was found.\nYou need to upgrade the data format before using PostgreSQL.\nSee $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information."
			exit 1
		fi

	# No existing PGDATA! Initdb it.

	else
		echo -n $"Initializing database: "
		if [ ! -d $PGDATA ]; then
			mkdir -p $PGDATA
			chown postgres.postgres $PGDATA
			chmod go-rwx $PGDATA
		fi
		# Make sure the locale from the initdb is preserved for later startups...
		[ -f /etc/sysconfig/i18n ] && cp /etc/sysconfig/i18n $PGDATA/../initdb.i18n
		# Just in case no locale was set, use en_US
		[ ! -f /etc/sysconfig/i18n ] && echo "LANG=en_US" > $PGDATA/../initdb.i18n

		# Is expanded this early to be used in the command su runs
		echo "export LANG LC_ALL LC_CTYPE LC_COLLATE LC_NUMERIC LC_CTYPE LC_TIME" >> $PGDATA/../initdb.i18n

 	fi

	# Check for postmaster already running...
	# note that pg_ctl only looks at the data structures in PGDATA
	# you really do need the pidof()
	pid=`pidof -s /usr/bin/postmaster`
	if [ $pid ] && /usr/bin/pg_ctl status -D $PGDATA > /dev/null 2>&1
	then
		echo $"Postmaster already running."
	else
		#all systems go -- remove any stale lock files
		rm -f /tmp/.s.PGSQL.${PGPORT} > /dev/null
		echo -n "$PSQL_START"
		su -l postgres -s /bin/sh -c "/usr/bin/pg_ctl  -D $PGDATA -p /usr/bin/postmaster -o '-p ${PGPORT}' start  > /dev/null 2>&1" < /dev/null
 		sleep 1
 		pid=`pidof -s /usr/bin/postmaster`
 		if [ $pid ]
		then
			success "$PSQL_START"
			touch /var/lock/subsys/${NAME}
			echo $pid > /var/run/postmaster.${PGPORT}.pid
			echo
		else
			failure "$PSQL_START"
			echo
		fi
	fi
}

stop(){
	echo -n $"Stopping PostgreSQL server: "
	su -l postgres -s /bin/sh -c "/usr/bin/pg_ctl stop -D $PGDATA -s -m fast" > /dev/null 2>&1
	ret=$?
	if [ $ret -eq 0 ]; then
		echo_success
	else
		echo_failure
	fi
	echo
	rm -f /var/run/postmaster.${PGPORT}.pid
	rm -f /var/lock/subsys/${NAME}
}

function restart() {
	stop
	start
}

function condrestart() {
	[ -e /var/lock/subsys/${NAME} ] && restart
}

function reload() {
	su -l postgres -s /bin/sh -c "/usr/bin/pg_ctl reload -D $PGDATA -s" > /dev/null 2>&1
}

# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status postmaster
	;;
  restart)
	restart
	;;
  condrestart)
	condrestart
	;;
  reload|force-reload)
	reload
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|force-reload}"
	exit 1
esac

exit 0
