#! /bin/sh
## WARNING TO MAINTAINERS - DO NOT EDIT this file in debian/.  It will be
## overwritten by debian/rules.  Edit the .in template instead.
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.)
# 
# This is NOT a configuration file.
# If you wish to change any values, the proper place to do so is in
# /etc/postgresql/postmaster.init. ANY CHANGES YOU MAKE TO THIS FILE
# WILL GET OVERWRITTEN BY THE NEXT INSTALLATION.
# To stop postgresql running, do
#   ln -sf /bin/false /usr/lib/postgresql/bin/can_i_run

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
		if [ -f /usr/lib/postgresql/dumpall/default_encoding ]
		then
		   echo "The postinstallation script should attempt to upgrade the database"
		   echo "automatically.  If it fails, it must be done by hand."
		else
		   echo Run postgresql-dump to dump the old database and to reload
		   echo it in the new format.
	       fi
	       echo "*** READ /usr/share/doc/postgresql/README.Debian.migration.gz FIRST! ***"
	    fi
	    echo
	    echo The version ${version} postmaster cannot be started until
	    echo this is done.
	    exit 255
	fi
}

obsolete_postmaster_init () {
	echo "
	***************  Obsolete configuration file  *****************
	
	/etc/postgresql/postmaster.init is obsolete.
	Please update this configuration file by integrating your site-
	specific changes with the new format in
	/etc/postgresql/postmaster.init.dpkg-dist
	***************************************************************
	"
}

. /etc/postgresql/postmaster.init
if [ -n "${PGPORT}" ]
then
	if [ "${PGPORT}" -eq 5341 ] 
	then
		echo /etc/postgresql/postmaster.init sets PGPORT=5341, which is not allowed
		exit 2
	fi
fi

export LANG PGDATESTYLE

# Check for old-style postmaster.init
if [ "${PGDATESTYLE}" = American -o "${PGDATESTYLE}" = European ]
then
	obsolete_postmaster_init
	if [ "${PGDATESTYLE}" = American ]
	then
		PGDATESTYLE=US
	else
		PGDATESTYLE=EURO
	fi
elif [ -z "${PGDATESTYLE}" ]
then
	if grep -qE '^ *# *PGDATESTYLE=(American|European)' /etc/postgresql/postmaster.init
	then
		obsolete_postmaster_init
	fi
	PGDATESTYLE=EURO
fi

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

# Give administrators a means of not allowing postgresql to start:
# Link /usr/lib/postgresql/bin/can_i_run to /bin/false
if [ -x /usr/lib/postgresql/bin/can_i_run -a "$1" != dump_schema ]
then
	if ! /usr/lib/postgresql/bin/can_i_run
	then
			echo PostgreSQL is temporarily disabled
			echo
			echo To enable it, remove /usr/lib/postgresql/bin/can_i_run \(or
			echo link it to /bin/true\) and run \`/etc/init.d/postgresql start\'
			echo
			exit 1
		fi
fi

# (but a dump_schema is needed during upgrading, and must always be allowed)
if [ "$1" = dump_schema ]
then
	PGPORT=5341
	PGALLOWTCPIP=no
	echo PostgreSQL is listening on port 5341 for a schema dump
fi
# 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="${PGBUFFERS}"
fi

# Maximum number of backends
if [ -n "${PGBACKENDCOUNT}" ]
then
	BACKENDCOUNT=${PGBACKENDCOUNT}
	BACKENDOPT="-N ${PGBACKENDCOUNT}"
else
	BACKENDCOUNT=64
fi

# Check relationship between backends and buffers
minbuf=`expr $BACKENDCOUNT \* 2`
if [ -n "$BUFFERS" ]
then
	if [ $BUFFERS -lt $minbuf ]
	then
		echo "Value of PGBUFFERS ($PGBUFFERS) in /etc/postgresql/postmaster.init is too small"
		echo "for the number of backends.  It has been increased to $minbuf"
		BUFFERS=$minbuf
	fi
else
	BUFFERS=$minbuf
fi
BUFFERS="-B ${BUFFERS}"

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

# TCP port
if [ "${PGALLOWTCPIP}" = yes ]
then
	TCP=-i
fi

# Where to listen
if [ ! -z "${PGPORT}" ]
then
	 PORT="-p ${PGPORT}"
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

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

# Make sure that we don't try to start 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 /var/run/postgresql/.s.PGSQL.${PGPORT} ]
then
	rm /var/run/postgresql/.s.PGSQL.${PGPORT}
fi

# Ready to go: stand clear...
echo Starting PostgreSQL postmaster
cd ${POSTGRES_HOME}
if [ -n "${DEBUGLEVEL}" -o "${PGECHO}" = yes ]
then
	touch ${POSTGRES_LOG:=/var/log/postgres.log}
	chown postgres.postgres ${POSTGRES_LOG}
	chmod 660 ${POSTGRES_LOG}
	su postgres -c "${POSTMASTER} -b ${POSTGRES} ${BUFFERS} ${BACKENDOPT} \
	    -D ${PGDATA} ${DEBUGLEVEL} ${TCP} ${PORT} ${OPTIONS}  \
	    >> ${POSTGRES_LOG} 2>&1 &"
else
	su postgres -c "${POSTMASTER} -b ${POSTGRES} ${BUFFERS} ${BACKENDOPT} \
	    -D ${PGDATA} ${TCP} ${PORT} ${OPTIONS} &"
fi

# If possible, increase the size of the kernel file table
fmaxfile=/proc/sys/kernel/file-max
if [ ! -r $fmaxfile ]
then
	fmaxfile=/proc/sys/kernel/fs/file-max
	if [ ! -r $fmaxfile ]
	then
		fmaxfile=
	fi
fi

if [ -n "$fmaxfile" ]
then
	fmax=`cat $fmaxfile`
	if [ $fmax -lt ${KERNEL_FILE_MAX:=1032} ]
	then
		echo ${KERNEL_FILE_MAX} > $fmaxfile
	fi
fi

#  Give it a chance to get going
sleep 5

exit 0

