#!/bin/sh
#
# Slackware startup script for PostgreSQL
# Written by Ken Zalewski <kennyz@nycap.rr.com> on 16 November 2005
# Last revision on 2 January 2006
#
# Based on the default generic startup script included in the
# PostgreSQL source distribution
# Original author:  Ryan Kirkpatrick <pgsql@rkirkpat.net>
# $PostgreSQL: pgsql/contrib/start-scripts/linux,v 1.7 2004/10/01 18:30:21 tgl Exp $
#
#
# NOTES:
#
#  This script can be run directly from a root command shell, using:
#        root# /etc/rc.d/rc.postgresql start
#  or
#        root# /etc/rc.d/rc.postgresql stop
#
#  For automatic startup, it should be added to /etc/rc.d/rc.M, just
#  beneath the MySQL startup.  A sample startup line:
#
#        [ -x /etc/rc.d/rc.postgresql ] && /etc/rc.d/rc.postgresql start
#
#  or more traditionally:
#
#        if [ -x /etc/rc.d/rc.postgresql ]; then
#          /etc/rc.d/rc.postgresql start
#        fi
#
#  Note that this script should NOT be sourced (using the "." command),
#  since it contains "exit" statements which would cause rc.M to exit
#  prematurely.  This is similar to rc.cups, which also cannot be sourced.
#

# Installation prefix
prefix=/usr
def_uid=28
def_gid=28

# PostgreSQL binaries
PGBIN="$prefix/bin"

# PostgreSQL working directory
PGDIR="/var/lib/pgsql"

# Data directory
PGDATA="$PGDIR/data"

# Who to run the postmaster as, usually "postgres".
PGUSER=postgres

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

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

# What to use to start up the postmaster (we do NOT use pg_ctl for this,
# as it adds no value and can cause the postmaster to misrecognize a stale
# lock file)
DAEMON="$PGBIN/postmaster"

# What to use to shut down the postmaster
PGCTL="$PGBIN/pg_ctl"

# Only start if we can find the postmaster.
if [ ! -x $DAEMON ]; then
  echo "PostgreSQL is not installed correctly" >&2
  exit 1
fi

check_postgres_env() {
  # Confirm that the "postgres" user and group exist.
  grep -q "^$PGUSER:" /etc/group || groupadd -g $def_gid $PGUSER || exit 1
  grep -q "^$PGUSER:" /etc/passwd || useradd -c "PostgreSQL" -d $PGDIR -g $PGUSER -u $def_uid $PGUSER || exit 1

  # Confirm that the Postgres home directory ($PGDIR) exists.
  if [ ! -d $PGDIR/ ]; then
    echo -n "Creating PGDIR $PGDIR: "
    mkdir $PGDIR && echo "ok" || exit 1
  fi
  chown $PGUSER:$PGUSER $PGDIR/
  chmod 750 $PGDIR/

  # Check for a valid startup log file.
  if [ ! -e "$PGLOG" ]; then
    touch "$PGLOG" || exit 1
    chown $PGUSER:$PGUSER "$PGLOG"
    chmod go-rwx "$PGLOG"
  fi

  # Check for a valid PGDATA directory
  if [ ! -f "$PGDATA/PG_VERSION" -o ! -d "$PGDATA/base" ]; then
    echo -n "Initializing database: "
    if [ ! -e "$PGDATA" ]; then
      mkdir -p "$PGDATA" || exit 1
      chown $PGUSER:$PGUSER "$PGDATA"
      chmod go-rwx "$PGDATA"
    fi
    su - $PGUSER -c "$PGBIN/initdb --pgdata='$PGDATA'" >>$PGLOG 2>&1
    if [ -f "$PGDATA/PG_VERSION" ]; then
      echo "ok"
    else
      echo "failed"
      exit 1
    fi
  fi
}

start() {
  check_postgres_env
  echo -n "Starting PostgreSQL: "
  su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
  [ $? -eq 0 ] && echo "ok" || echo "failed"
}

stop() {
  echo -n "Stopping PostgreSQL: "
  su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast" >>$PGLOG 2>&1
  [ $? -eq 0 ] && echo "ok" || echo "failed"
}

restart() {
  stop
  start
}

reload() {
  echo -n "Reload PostgreSQL: "
  su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
  [ $? -eq 0 ] && echo "ok" || echo "failed"
}

status() {
  su - $PGUSER -c "$PGCTL status -D '$PGDATA'"
}

# Parse command line parameters.
case $1 in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  reload)
	reload
        ;;
  status)
	status
	;;
  *)
	# Print help
	echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
	exit 1
	;;
esac

exit 0
