#!/bin/sh
#
# postgresql    This is the init script for starting up the PostgreSQL
#               server.
#
# chkconfig: - 64 36
# description: PostgreSQL database server.
# processname: postmaster
# pidfile="/var/run/${NAME}.pid"

# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)

# Version 9.0 Devrim Gunduz <devrim@gunduz.org>
# Get rid of duplicate PGDATA assignment.
# Ensure pgstartup.log gets the right ownership/permissions during initdb

# Version 9.1 Devrim Gunduz <devrim@gunduz.org>
# Update for 9.1
# Add an option to initdb to specify locale (default is $LANG):
# service postgresql initdb tr_TR.UTF-8

# Version 9.2 Devrim Gunduz <devrim@gunduz.org>
# Update for 9.2

# Version 9.2.1 Devrim Gunduz <devrim@gunduz.org>
# Fix version number in initdb warning message, per Jose Pedro Oliveira.
# Add new functionality: Upgrade from previous version.
# Usage: service postgresql-9.2 upgrade

# Version 9.2.3 Devrim Gunduz <devrim@gunduz.org>
# Fix longstanding bug: Enable pidfile and lockfile variables to be defined
# in sysconfig file.
# Use $pidfile in status().

# Version 9.2.4 Devrim Gunduz <devrim@gunduz.org>
# Fix pid file name in init script, so that it is more suitable for
# multiple postmasters. Per suggestion from Andrew Dunstan. Fixes #92.

# Version 9.3.0 Devrim Gunduz <devrim@gunduz.org>
# Add support for pg_ctl promote. Per suggestion from Magnus Hagander. Fixes #93.
# Remove hardcoded script names in init script. Fixes #102.

# Version 9.3.1 Devrim Gunduz <devrim@gunduz.org>
# Fix PGPREVMAJORVERSION parameter, per report from Igor Poteryaev.
# Remove extra whitespace in upgrade() code, per report from Igor Poteryaev.

# Version 9.3.2 Devrim Gunduz <devrim@gunduz.org>
# Add process name to the status() call. Patch from Darrin Smart

# Version 9.4.0 Devrim Gunduz <devrim@gunduz.org>
# Remove PGPORT variable.

# PGVERSION is the full package version, e.g., 9.4.0
# Note: the specfile inserts the correct value during package build
PGVERSION=__RPM_PGVERSION__
# PGMAJORVERSION is major version, e.g., 9.4 (this should match PG_VERSION)
PGMAJORVERSION=__RPM_PGMAJORVERSION__
PGPREVMAJORVERSION=__RPM_PREVMAJORVERSION__

# Source function library.
INITD=__RPM_INITDIR__
. $INITD/functions

# Get network config.
. /etc/sysconfig/network

# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
	NAME=${NAME:3}
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
    SU=runuser
else
    SU=su
fi

# Define variable for locale parameter:
LOCALEPARAMETER=$2

# Set defaults for configuration variables
PGENGINE=__RPM_PGENGINE__
PGDATA=__RPM_PGDATA__
PGLOG=__RPM_PGVARLIB__/pgstartup.log
# Log file for pg_upgrade
PGUPLOG=__RPM_PGVARLIB__/pgupgrade.log

PGSERVERUSER=__RPM_PGUSER__
PGSERVERGROUP=__RPM_PGGROUP__

lockfile="/var/lock/subsys/${NAME}"
pidfile="/var/run/${NAME}.pid"

# Override defaults from /etc/sysconfig/pgsql if file is present
[ -f /etc/sysconfig/__RPM_SHORTNAME__/${NAME} ] && . /etc/sysconfig/__RPM_SHORTNAME__/${NAME}

export PGDATA

[ -f "$PGENGINE/postmaster" ] || exit 1

script_result=0

start(){
	[ -x "$PGENGINE/postmaster" ] || exit 5

	PSQL_START=$"Starting ${NAME} service: "

	# Make sure startup-time log file is valid
	if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
	then
		touch "$PGLOG" || exit 1
		chown $PGSERVERUSER:$PGSERVERGROUP "$PGLOG"
		chmod go-rwx "$PGLOG"
		[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
	fi

	# Check to see if the datadir is OK and for the current version
	# and bail out if we find any problems.
	__RPM_PGENGINE__/__RPM_PGPACKAGENAME__ "$PGDATA" || exit 1

	echo -n "$PSQL_START"
	$SU -l $PGSERVERUSER -c "$PGENGINE/postmaster -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
	sleep 2
	pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
	if [ "x$pid" != x ]
	then
		success "$PSQL_START"
		touch "$lockfile"
		echo $pid > "$pidfile"
		echo
	else
		failure "$PSQL_START"
		echo
		script_result=1
	fi
}

stop(){
	echo -n $"Stopping ${NAME} service: "
	if [ -e "$lockfile" ]
	then
		$SU -l $PGSERVERUSER -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null
		ret=$? 
		if [ $ret -eq 0 ]
		then
			echo_success
			rm -f "$pidfile"
			rm -f "$lockfile"
		else
			echo_failure
			script_result=1
		fi
		else
			# not running; per LSB standards this is "ok"	
			echo_success
		fi
		echo
}

restart(){
    stop
    start
}

initdb(){
	exec __RPM_PGENGINE__/__RPM_PGPACKAGENAME__-setup initdb 
}

upgrade(){
	exec __RPM_PGENGINE__/__RPM_PGPACKAGENAME__-setup upgrade $2 $3
}


condrestart(){
	[ -e "$lockfile" ] && restart || :
}

reload(){
    $SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}

promote(){
    $SU -l postgres -c "$PGENGINE/pg_ctl promote -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status -p $pidfile $NAME
	script_result=$?
	;;
  restart)
	restart
	;;
  initdb)
	initdb
	;;
  promote)
	promote
	;;
  upgrade)
	upgrade
	;;
  condrestart|try-restart)
	condrestart
	;;
  reload)
	reload
	;;
  force-reload)
	restart
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|upgrade|condrestart|try-restart|reload|force-reload|initdb|promote}"
	exit 2
esac

exit $script_result
