#! /bin/sh
set -e
#
# Initialisation
#
# (This should set up values for POSTGRES_HOME and POSTGRES_DATA, which
# say where the library and database are.  It should also set up DATEFORMAT
# which governs whether the backend sends back dates in European or
# American format, and the logging options.)
# 
# If you wish to change any values, the proper place to do so is in
# /etc/postgresql/postmaster.init

check_version () {
	# compare the database format with the format expected by the software
	dbformat=`cat ${PGDATA}/PG_VERSION 2>/dev/null`
	if [ A${dbformat} != A${version} ]
	then
	    if [ -z "${dbformat}" ]
	    then
		echo The database framework has not yet been created. Use
		echo initdb to do this.
	    else
		echo The database is in an older format that cannot be read by
		echo version ${version} of PostgreSQL.
		echo
		echo Run postgresql-dump to dump the old database and to reload
		echo it in the new format.
	    fi
	    echo
	    echo The version ${version} postmaster cannot be started until
	    echo this is done.
	    exit 255
	fi
}

startup () {
	# Make sure we have a database directory
	if [ ! -d ${PGDATA} ]
	then
	    echo No readable database directory for postgresql
	    exit 3
	fi

	if [ ! -d ${PGDATA}/base ]
	then
	    echo There is no PostgreSQL database framework in $PGDATA.
	    echo Run initdb as the postgres user to create it
	    exit 3
	fi

	# First, check that the database is the right version
	check_version

	# Shared-memory buffers
	if [ ! -z "${PGBUFFERS}" ]
	then
	    BUFFERS="-B ${PGBUFFERS}"
	fi

	# Debugging level
	if [ ! -z "${PGDEBUG}" ]
	then
	    DEBUGLEVEL="-d ${PGDEBUG}"
	fi

	# Authentication
	# TCP port
	if [ "${PGALLOWTCPIP}" = yes ]
	then
		PORT=-i
		if [ ! -z "${PGPORT}" ]
		then
		    PORT="-i -p ${PGPORT}"
		fi
	fi
	
	# Backend options
	# Query echoing
	if [ "${PGECHO}" = yes ]
	then
	    OPTIONS=-E
	fi

	# Timing stats
	if [ "${PGSTATS}" = yes ]
	then
	    OPTIONS="${OPTIONS} -s"
	fi

	# Fsync()
	if [ "${PGFSYNC}" = no ]
	then
	    OPTIONS="${OPTIONS} -F"
	fi

	# Sort memory
	if [ ! -z "${PGSORTMEM}" ]
	then
	    OPTIONS="${OPTIONS} -S ${PGSORTMEM}"
	fi

	# American or European date format
	if [ "${PGDATESTYLE}" != American ]
	then
	    OPTIONS="${OPTIONS} -e"
	fi

	if [ ! -z "${OPTIONS}" ]
	then
	    OPTIONS="-o "\'${OPTIONS}\'
	fi

	# Make sure that we don't get started if there is no options file.
	# We certainly don't want to get started if the executable is missing.
	if [ ! -x ${POSTMASTER} ]
	then
	    echo No postmaster executable for postgresql
	    exit 3
	fi

	# Make sure there is no UNIX-socket file lurking around; this socket
	# can get left behind if there is a system crash.

	if [ -S /tmp/.s.PGSQL.${PGPORT} ]
	then
		rm /tmp/.s.PGSQL.${PGPORT}
	fi

	# Ready to go: stand clear...
	echo Starting PostgreSQL postmaster
        if [ -n "${DEBUGLEVEL}" ]
        then
		touch ${POSTGRES_LOG:=/var/log/postgres.log}
		chown postgres.postgres ${POSTGRES_LOG}
		su postgres -c "nohup ${POSTMASTER} -b ${POSTGRES} ${BUFFERS} \
		    -D ${PGDATA} ${DEBUGLEVEL} ${PORT} ${OPTIONS}  \
		    > ${POSTGRES_LOG} 2>&1 &"
        else
		su postgres -c "${POSTMASTER} -b ${POSTGRES} ${BUFFERS} \
		    -D ${PGDATA} -S ${PORT} ${OPTIONS}"
        fi
}
. /etc/postgresql/postmaster.init
export LANG

# --------------------------------------------------------------------------
# No user configurable parts below this line.
PGDATA=${POSTGRES_DATA:-/var/postgres/data}
PGLIB=/usr/lib/postgresql
export PGLIB PGDATA
POSTMASTER=${PGLIB}/bin/postmaster
POSTGRES=${PGLIB}/bin/postgres
version=6.3		# Note: this is not necessarily the same as the
			# software version.

case "$1" in
    start)
	startup
        ;;
    stop)
	echo Stopping PostgreSQL postmaster
        start-stop-daemon --stop --verbose --exec ${POSTMASTER} || exit 0
        ;;
    restart)
	echo Restarting PostgreSQL postmaster
	start-stop-daemon --stop --verbose --exec ${POSTMASTER}
	startup
	;;
    force-reload)
	echo Reload not supported by PostgreSQL - restarting postmaster
	start-stop-daemon --stop --verbose --exec ${POSTMASTER}
	startup
	;;
    reload)
	echo PostgreSQL does not support a reload option
	exit 2
	;;
    *)
        echo "Usage: /etc/init.d/postgresql {start|stop|restart}"
        exit 1
	;;
esac

exit 0

