#! /bin/sh
#
# /etc/init.d/postgresql
#
# 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: - 85 15
# description: Starts and stops the PostgreSQL backend daemon that handles \
#	       all database requests.
# processname: postmaster
# pidfile: /var/run/postmaster.pid
#

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

# 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


RETVAL=0
LOCKFILE=/var/lock/subsys/postgresql
PG_CHROOT_DIR=/var/lib/pgsql-root
TMPDIR=$PG_CHROOT_DIR/tmp

adjust()
{
	action "Adjusting environment for postgresql:" /etc/chroot.d/postgresql.all
	RETVAL=$?
	return $RETVAL
}

find_socket()
{
	local n f
	for n in "$@"; do
		for f in "$n"*; do
		    if [ -S "$f" ]; then
			echo "$f"
			return 0
		    fi
		done
	done
	return 1
}

wait_socket()
{
	local w_times="$1"
	shift
	local w_delay="$1"
	shift

	local i=0
	while [ $i -lt "$w_times" ]; do
		filelist=`find_socket $PG_CHROOT_DIR/tmp/.s.PGSQL.*`
		if [ -n "$filelist" ]; then
		    echo $filelist
		    return 0
		fi
		sleep "$w_delay"
		i=$[1+i]
	done
	return 1
}

start()
{
	if [ $PG_CHROOT_DIR ]; then
	    adjust || return 1
	fi
	
	# Check for older PGDATA location.
	if [ -f $PG_CHROOT_DIR/var/lib/pgsql/PG_VERSION ] && [ -d $PG_CHROOT_DIR/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 $PG_CHROOT_DIR$PGDATA/PG_VERSION ] && [ -d $PG_CHROOT_DIR$PGDATA/base ]
	then
	# Check version of existing PGDATA

		if [ `cat $PG_CHROOT_DIR$PGDATA/PG_VERSION` != '7.3' ];	then
		    echo -n "Starting postgresql: "
		    failure "Old version. Need to Upgrade. See /usr/share/doc/postgresql-7.3.1/README.rpm-dist for more information."
		    echo
		    return 1
		fi

	# No existing PGDATA! Initdb it.
	else
                if [ ! -d $PG_CHROOT_DIR$PGDATA ]; then
		    mkdir -p $PG_CHROOT_DIR$PGDATA
		    chown postgres.postgres $PG_CHROOT_DIR$PGDATA
		fi
		# 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" >> $PG_CHROOT_DIR$PGDATA/../initdb.i18n
		
		action "Creating default database:" /bin/su -s /bin/sh -l postgres -c \'/usr/bin/initdb --pgdata=$PG_CHROOT_DIR$PGDATA\'
		RETVAL=$?
		[ "$RETVAL" -ne "0" ] && return $RETVAL;
	fi

	echo -n "Starting postgresql: "
	# Check for postmaster already running...
	local pidlist=`pidofproc postmaster`
	local pid= p
	for p in $pidlist; do
	    if checkpid "$p"; then
		[ -z "$pid" ] && pid="$p" || pid="$pid $p"
	    fi
	done
	
	[ -z "$pid" ] || { echo_passed; echo; return 1; }
	
	#all systems go -- remove any stale lock files
	rm -f $TMPDIR/tmp/.s.PGSQL.* >/dev/null 2>&1
	rm -f $PG_CHROOT_DIR/$PGDATA/postmaster.pid >/dev/null 2>&1
	
	if [ $PG_CHROOT_DIR ]; then
	    locale_list=`/bin/su -l postgres -s /bin/sh -c '/usr/bin/locale'`
	    export `/bin/echo $locale_list | /usr/bin/tr ' ' "\n" | /bin/grep LANG`
	    daemon /usr/bin/postmaster -D $PGDATA -S -r $PG_CHROOT_DIR
	else
	    daemon --user postgres /usr/bin/postmaster -D $PGDATA -S
	fi
	
	RETVAL=$?
	echo
	
	if [ $RETVAL -eq 0 ]; then 
		touch "$LOCKFILE"
		if [ $PG_CHROOT_DIR ]; then
			echo -n "Link postgresql socket: "
    			socket=`wait_socket 30 1s`
        		if [ -n "$socket" ]; then
    		    		for f in $socket; do
        			    ln -s $f /tmp/${f##*/} >/dev/null 2>&1
	        		done
				echo_success
				echo
				return $RETVAL
			else
    				failure ""
				echo
				return 1
			fi
		fi
	fi    
}	

stop()
{
	echo -n "Shutting down postgresql: "
	# stop daemons, perhaps with the killproc function, for example:
	killproc postmaster
	RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
	    rm -f "$LOCKFILE"
	    if [ $PG_CHROOT_DIR ]; then
	        rm -f /tmp/.s.PGSQL.* >/dev/null 2>&1
	    fi
	fi
	echo
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	# cause the service configuration to be reread, either with kill -HUP:
	echo -n "Reloading postgresql: "
	killproc postmaster -HUP
	RETVAL=$?
	echo
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	adjust)
		adjust
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		# Stop the servce if it is already running, for example:
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		# Restart the servce if it is already running, for example:
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		# report the status of the daemons in free-form format,
		# perhaps with the status function, for example:
		status postmaster
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|adjust|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL

