#!/bin/bash
# postgresql	This is the init script for starting up the PostgreSQL
#		server

# Version 6.5.3-2 Lamar Owen
# Added code to determine if PGDATA exists, whether it is current version
#     or not, and initdb if no PGDATA (initdb will not overwrite a database).

# Version 7.0 Lamr Owen
# Added logging code
# Changed PGDATA.

# chkconfig: 345 85 15
# description: Starts and stops the PostgreSQL backend daemon that handles \
#	       all database requests.
# processname: postmaster
# pidfile: /var/run/postmaster.pid
#
#PGVERSION
PGVERSION=7.1

# Version 7.1 Daouda Lo <daouda@mandrakesoft.com>

# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions

# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`
POSTGRESQL="postgresql"

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
# Pretty much need it for postmaster.
[ ${NETWORKING} = "no" ] && exit 0

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

# 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) 
	echo -n "Checking postgresql installation: "

	# Check for older PGDATA location.
	if [ -f /var/lib/pgsql/PG_VERSION ] && [ -d /var/lib/pgsql/base/template1 ]
	then
		export PGDATA=/var/lib/pgsql
	else
		export PGDATA=/var/lib/pgsql/data
	fi

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

		if [ `cat $PGDATA/PG_VERSION` != '7.1' ]
		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  $"An old version of the database format was found."
			echo  $"You need to upgrade the data format before using PostgreSQL."
			echo  $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm for more information"
			exit 1
		else
			if echo "$TYPESET"|grep "declare -f success ()" >/dev/null
			then
				success "Checking postgresql installation: "
			else
				echo_success
			fi
			echo
		fi

	# No existing PGDATA! Initdb it.

	else
		echo "no database files found."
                if [ ! -d $PGDATA ]
		then
			mkdir -p $PGDATA
			chown postgres.postgres $PGDATA
		fi
		#su -l postgres -c '/usr/bin/initdb --pglib=/usr/lib/pgsql --pgdata=/var/lib/pgsql/data' </dev/null > /dev/null 2>& 1
		su -l postgres -c '/usr/bin/initdb --pgdata=/var/lib/pgsql/data' </dev/null > /dev/null 2>& 1
		echo_success
		echo
	fi

	# Check for postmaster already running...
	pid=`pidof postmaster`
	if [ $pid ]
	then
		echo "Postmaster already running."
	else
		#all systems go -- remove any stale lock files
		rm -f /tmp/.s.PGSQL.* > /dev/null
		echo -n "Starting postgresql service: "
		su -l postgres -c "/usr/bin/pg_ctl  -D $PGDATA -p /usr/bin/postmaster -o "-i"  start >/dev/null 2>&1"
		sleep 2
	 	pid=`pidof postmaster`
	 	if [ $pid ]
	 	then
         		if echo "$TYPESET"|grep "declare -f success ()" >/dev/null
         		then
         			success "Starting postgresql service: "
         		else
         			echo " [ OK ]"
         		fi
         		touch /var/lock/subsys/postgresql
         		echo $pid > /var/run/postmaster.pid
                	echo
         	else
         		if echo "$TYPESET"|grep "declare -f failure ()" >/dev/null
        		 then
         			failure "Starting postgresql service: "
         		else
         			echo " [ FAILED ]"
         		fi
         		echo
         	fi
	fi
	;;
  stop)
	echo -n "Stopping postgresql service: "
	killproc postmaster
	sleep 2
	rm -f /var/run/postmaster.pid
	rm -f /var/lock/subsys/postgresql
	echo
	;;
  status)
	status postmaster
	;;
  restart)
	$0 stop
	$0 start
	;;
  condrestart)
  	[ -e /var/lock/subsys/postgresql ] && restart || :
	;;
  *)	
	echo "Usage: postgresql {start|stop|status|restart|condrestart}"
	exit 1
esac

exit 0
