#! /bin/sh

### BEGIN INIT INFO
# Provides: postgresql
# Required-Start: $local_fs $network $syslog
# Should-Start: $remote_fs $named $time
# Required-Stop: $local_fs $network $syslog
# Should-Stop: $remote_fs $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PostgreSQL RDBMS
# Description: PostgreSQL RDBMS service.
#              The world's most advanced open source database.
#              See http://www.postgresql.org/ for more information.
### END INIT INFO

# This is an example of a Linux LSB conforming init script.
# See http://refspecs.freestandards.org/ for more information on LSB.

# Original author:  Kevin Grittner

# $PostgreSQL$

#--------------------------------------------------------------------
# The only edits needed should be in the INIT INFO block
# and between the lines of dashes below.  If any other
# changes are needed, or you find a way to enhance the script,
# consider posting to the PostgreSQL hackers list.
#--------------------------------------------------------------------

# Installation prefix
prefix=/usr/local/pgsql

# Data directory
PGDATA="/var/local/pgsql/data"

# Who to run the postmaster as, usually "postgres".  (NOT "root")
PGUSER=postgres

# Where to keep a log file
PGLOG="$PGDATA/serverlog"

#--------------------------------------------------------------------

# The path that is to be used for the script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# The LSB functions must be present.
lsbf=/lib/lsb/init-functions
test -r "$lsbf" || {
    echo "$0: not able to read $lsbf: script cannot run" 1>&2
    exit 5
  }

# Source the functions.
. "$lsbf"
# All output from the script should be through the LSB msg functions after this.

# Define usage string, used in more than one place.
usage="Usage: $0 {start|stop|restart|try-restart|reload|force-reload|status}"

# Check that we have one parameter: action
if [ $# -ne 1 ] ; then
  if [ $# -lt 1 -o "$1" = "" ] ] ; then
    log_failure_msg "$0: action not specified"
  else
    log_failure_msg "$0: too many parameters"
  fi
  log_warning_msg "$usage"
  exit 2
fi
action="$1"

# What to use to manipulate the postmaster.
PGCTL="$prefix/bin/pg_ctl"

# Only start if we can find the postmaster.
test -x "$PGCTL" || {
    if [ "$action" = "stop" ] ; then
      log_warning_msg "$0: executable $PGCTL not found: $action request ignored"
      exit 0
    else
      log_failure_msg "$0: executable $PGCTL not found: $action request failed"
      exit 5
    fi
  }

pidfile="$PGDATA/postmaster.pid"
servicename=$( basename "$0" )
daemon="$prefix/bin/postgres"

initd_start () {
  echo -n "Starting $servicename: "
  su -c ". '$lsbf' ; start_daemon -p '$pidfile' '$PGCTL' -w -D '$PGDATA' -l '$PGLOG' start" - $PGUSER
  rc=$?
}

initd_stop () {
  echo -n "Shutting down $servicename "
  su -c ". \"$lsbf\" ; killproc -p '$pidfile' '$daemon' SIGINT" - $PGUSER \
  || su -c ". \"$lsbf\" ; killproc -p '$pidfile' '$daemon' SIGQUIT" - $PGUSER
  rc=$?
  if [ $rc -eq 0 ] ; then
    echo 'ok'
    rm -f "$pidfile"
  else
    echo 'failed'
  fi
}

initd_reload () {
  su -c "$PGCTL reload -D '$PGDATA'" - $PGUSER
  rc=$?
}

initd_status () {
  su -c ". \"$lsbf\" ; pidofproc -p '$pidfile' '$daemon'" - $PGUSER 1>/dev/null \
  && su -c ". \"$lsbf\" ; $PGCTL status -D '$PGDATA'" - $PGUSER
  rc=$?
}

initd_exit () {
  if [ $rc -eq 0 ] ; then
    log_success_msg "$servicename $action: ok"
  else
    if [ "$action" = "status" ] ; then
      log_failure_msg "$servicename $action: not running"
    else
      log_failure_msg "$servicename $action: failed"
    fi
  fi
  exit $rc
}

case "$action" in
  start)
	initd_status
	if [ $rc -eq 0 ] ; then
	  log_warning_msg "$servicename $action: service already running; no action taken"
	  initd_exit
	fi
	initd_start
	initd_exit
	;;
  stop)
	initd_status
	if [ $rc -eq 3 ] ; then
	  log_warning_msg "$servicename $action: service not running; no action taken"
	  rc=0
	  initd_exit
	fi
	initd_stop
	initd_exit
	;;
  restart)
	initd_stop
	initd_start
	initd_exit
	;;
  try-restart)
	initd_status
	if [ $rc -eq 3 ] ; then
	  log_warning_msg "$servicename $action: service not running; no action taken"
	  rc=0
	  initd_exit
	fi
	if [ $rc -ne 0 ] ; then
	  initd_exit
	fi
	initd_stop
	initd_start
	initd_exit
	;;
  reload)
	initd_reload
	initd_exit
	;;
  force-reload)
	initd_reload
	initd_exit
	;;
  status)
	initd_status
	initd_exit
	;;
  *)
	log_failure_msg "$0: action \"$action\" not recognized"
	log_warning_msg "$usage"
	exit 2
	;;
esac
