#! /bin/bash
#
# NOTE TO SYSTEM ADMINISTRATORS #
# To stop postgresql running, use the update-rc.d facility, or file-rc
# from the file-rc package.

export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin

POSTMASTER=/usr/lib/postgresql/bin/postmaster
PG_CTL=/usr/lib/postgresql/bin/pg_ctl
PG_AUTOVACUUM=/usr/lib/postgresql/bin/pg_autovacuum
PGPORT=$(grep -si '^port *=' /etc/postgresql/postgresql.conf | cut -f2 -d=)

startup () {
        touch ${POSTGRES_LOG:=/var/log/postgresql/postgres.log}
        chown postgres.postgres $POSTGRES_LOG
	# Clear away postmaster.pid if it doesn't match a running process
	if [ -f $PGDATA/postmaster.pid ]
	then
		running_process=$(ps -p $(cat $PGDATA/postmaster.pid | head -1) | grep -v PID | awk '{print $NF}')
		if [ "$running_process" != postmaster ]
		then
			rm $PGDATA/postmaster.pid
		fi
	fi
	echo -n " postmaster"
        /sbin/start-stop-daemon --pidfile $PGDATA/postmaster.pid --chuid postgres --startas /usr/lib/postgresql/bin/postgresql-startup --start
	#
	# See if we should start autovacuuming
	if [ -x $PG_AUTOVACUUM ]
	then
	    case "$AUTOVACUUM" in yes | Yes | YES | y | Y | on | ON | True | true | TRUE | 1)
		OPTS='-D -p ${PGPORT:=5432}'
		if [ -n "$AVAC_DEBUG" ]
		then
			OPTS="$OPTS -d $AVAC_DEBUG"
		fi
		if [ -n "$AVAC_SLEEP_BASE" ]
		then
			OPTS="$OPTS -s $AVAC_SLEEP_BASE"
		fi
		if [ -n "$AVAC_SLEEP_SCALE" ]
		then
			OPTS="$OPTS -S $AVAC_SLEEP_SCALE"
		fi
		if [ -n "$AVAC_VAC_BASE" ]
		then
			OPTS="$OPTS -v $AVAC_VAC_BASE"
		fi
		if [ -n "$AVAC_VAC_SCALE" ]
		then
			OPTS="$OPTS -V $AVAC_VAC_SCALE"
		fi
		if [ -n "$AVAC_ANAL_BASE" ]
		then
			OPTS="$OPTS -a $AVAC_ANAL_BASE"
		fi
		if [ -n "$AVAC_ANAL_SCALE" ]
		then
			OPTS="$OPTS -A $AVAC_ANAL_SCALE"
		fi
		if [ -z "$AVAC_LOG" ]
		then
			AVAC_LOG=/var/log/postgresql/autovacuum_log
		fi
		OPTS="$OPTS -L $AVAC_LOG"
		echo -n " autovacuum"
		/sbin/start-stop-daemon --pidfile $PGDATA/autovacuum.pid --make-pidfile --chuid postgres --exec /usr/lib/postgresql/bin/pg_autovacuum --start -- $OPTS
		;;
	    esac
	fi
	# set file-max kernel parameter
	. /etc/postgresql/postmaster.conf
	fmaxfile=/proc/sys/kernel/file-max
	if [ ! -r $fmaxfile ]
	then
		fmaxfile=/proc/sys/kernel/fs/file-max
		if [ ! -r $fmaxfile ]
		then
			fmaxfile=/proc/sys/fs/file-max
			if [ ! -r $fmaxfile ]
			then
				fmaxfile=
			fi
		fi
	fi

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

stop () {
	if [ -f $PGDATA/autovacuum.pid ]
	then
		echo -n " autovacuum"
		start-stop-daemon --stop --pidfile $PGDATA/autovacuum.pid --name pg_autovacuum
		rm $PGDATA/autovacuum.pid
	fi
        echo -n " postmaster"
        $PG_CTL stop -s -w -m fast
}

if [ ! $EUID -eq 0 ]
then
	echo $0 needs to be run by root.
	exit 1
fi

[ -x $POSTMASTER ] || exit

if [ -r /etc/postgresql/postmaster.conf ]
then
	. /etc/postgresql/postmaster.conf
else
	echo /etc/postgresql/postmaster.conf is missing; cannot start postgresql
	exit 1
fi
PGDATA=${POSTGRES_DATA:-/var/lib/postgres/data}
export PGDATA

## Use of $PG_CTL to stop the postmaster:
## we use $PG_CTL stop -m fast, which will abort and roll back any current
## transactions.  Don't use -immediate, because it's equivalent to 
## kill -9.  Don't use -smart because it waits for people to disconnect
## and we want to force them off when we are shutting down or else the 
## system itself will do a kill -9 to get rid of us.

case "$1" in
    start)
        echo -n "Starting PostgreSQL database server:"
        startup
	echo "."
        ;;
    stop)
        echo -n "Stopping PostgreSQL database server:"
	stop
	echo "."
        ;;
    restart)
	$0 stop
	$0 start
        ;;
    force-reload | reload)
        $PG_CTL reload -D ${PGDATA}
        ;;
    status)
	$PG_CTL status
	;;
    *)
        echo "Usage: /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status}"
        exit 1
        ;;
esac

exit 0

