#! /bin/sh
#
# /etc/init.d/postgresql
#
# postgresql	This is the init script for starting up the PostgreSQL
#		server
# chkconfig: - 85 15
# description: Starts and stops the PostgreSQL backend daemon that handles \
#	       all database requests.
# processname: postgres
# pidfile: /var/run/postmaster.pid
#

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

RETVAL=0
LOCKFILE=/var/lock/subsys/postgresql
PG_CHROOT_DIR=/var/lib/pgsql-root
PGDATA=/var/lib/pgsql/data
PIDFILE="$PGDATA/postmaster.pid"

# FIXME: pg_ctl is buggy and lockup if pid file incorrect
# zero-size pidfile must be cleaned
[ -f "$PIDFILE" ] && [ ! -s "$PIDFILE" ] &&  /bin/rm "$PIDFILE"

delete_wrong_pidfile()
{
	if [ ! -f "$PIDFILE" ]; then
	    return
	fi

	PID=`head -n1 "$PIDFILE"`

	# PID-file is buggy
	if [ "x" == "x$PID" ]; then
	    rm -f "$PIDFILE"
	    return
	fi

	# Process not exists
	if [ ! -d "/proc/$PID" ]; then
	    rm -f "$PIDFILE"
	    return
	fi

	# This process not postgres process
	if [ "`readlink "/proc/$PID/exe"`" != "/usr/bin/postgres" ]; then
	    rm -f "$PIDFILE"
	    return
	fi
}

delete_wrong_pidfile

adjust()
{
	[ -n "$PG_CHROOT_DIR" ] || return 0

	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()
{
	adjust || return 1
	
	export PGDATA

	# 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"` != '8.2' ];	then
		    msg_starting postgresql
		    failure "Old version. Need to Upgrade. See /usr/share/doc/postgresql-8.2/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"

		locale_list=`/bin/su -l postgres -s /bin/sh -c '/usr/bin/locale'`
		locale=`/bin/echo $locale_list | /usr/bin/tr ' ' "\n" | /bin/grep LANG | cut -f2 -d=`
                action "Creating default database:" /bin/su -s /bin/sh -l postgres -c "\"/usr/bin/initdb --pgdata=\"\"$PG_CHROOT_DIR$PGDATA\"\" --locale=\"\"$locale\"\" \""
		RETVAL=$?
		[ "$RETVAL" -ne "0" ] && return $RETVAL;
	fi

	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`
	    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --make-pidfile -- /usr/bin/postgres -D "$PGDATA" -C "$PG_CHROOT_DIR"
	else
	    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --user postgres --make-pidfile -- /usr/bin/postgres -D "$PGDATA"
	fi
	
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
		touch "$LOCKFILE"
		if [ "$PG_CHROOT_DIR" ]; then
			rm -f /tmp/.s.PGSQL.* >/dev/null 2>&1
			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
	
	return $RETVAL
}	

stop()
{
    # pidfile exists -- may be server running
    if [ -f "$PIDFILE" ]; then
	msg_stopping postgresql
	if /bin/su -s /bin/sh -c "/usr/bin/pg_ctl -D \"$PGDATA\" stop -mf" postgres 2> /dev/null > /dev/null; then
		echo_success
	    else
		echo_failure
	    fi
    else
	msg_not_running postgresql
	echo_passed
    fi
    echo
}

restart()
{
    # not use pg_ctl restart, it's not suppot chrooting
    stop
    start
}

reload()
{
	adjust || return 1

	msg_reloading postgresql
	if /bin/su -s /bin/sh -c "/usr/bin/pg_ctl -D \"$PGDATA\" reload" postgres 2> /dev/null > /dev/null; then
	    echo_success
	else
	    echo_failure
	fi
	echo
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	adjust)
		adjust
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --expect-user postgres postgres
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|adjust|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL

