#!/bin/sh
#
#	%Z% %M% %I% %E% %Q%
#
# Copyright (c) 1999 The Santa Cruz Operation, Inc.. All Rights Reserved.
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF THE SANTA CRUZ OPERATION INC.
# The copyright notice above does not evidence any actual or intended
# publication of such source code.
# 
#
# PostgreSQL start/stop script
# Usage: pgsql [ start | stop | enable | disable | query | status | restart ]
#
PATH="/bin:/usr/bin:/etc:/usr/pgsql/bin:/usr/gnu/bin"; export PATH
PGSQLDIR=/usr/pgsql

if test -e /etc/default/pgsql; then
	. /etc/default/pgsql
fi

#
# If special parameters were not set, set the defaults
#
if test -z "$PGSQLUSER"; then
  if test -e /tcb/files/auth/p/postgres; then
    PGSQLUSER=postgres
  else
    PGSQLUSER=nouser
  fi
fi

if test -z "$PGSQLDATA"; then
  PGSQLDATA="$PGSQLDIR/data"
fi

if test -z "$PGSQLLOG"; then
  PGSQLLOG="/var/adm/postgres"
fi

PGSQLSTARTOPTS="-l $PGSQLLOG"

if test -n "$PGSQLOPTS"; then
  PGSQLSTARTOPTS="$PGSQLSTARTOPTS -o $PGSQLOPTS"
fi

# definitions
USAGE="Usage: %s [ start | stop | enable | disable | query | status | restart ]"
CAT="pgsql.cat"

PGSQLPIDFILE="$PGSQLDATA/postmaster.pid"
PGCTL="/usr/bin/pg_ctl"
SCRIPTDIR="/etc/init.d"
SCRIPT="pgsql"
PGSQL_LABEL="pgsql"
SCRIPTNUM="89"
TRUE=0
FALSE=1

#############################################################################
#
# get_server_pid
#
# returns the server PID, from the PID file, or 0 if no PID file found
#
#############################################################################
get_server_pid() {
  if test -r $PGSQLPIDFILE; then
    PGPID=`sed -n 1p $PGSQLPIDFILE`
    if test "$PGPID" -lt 0; then
      PGPID=`expr 0 - $PGPID`
    fi
    return $PGPID
  fi
  return 0
}

#############################################################################
#
# server_running
#
# check to see if the server is running - returns
#		$TRUE if the daemon is running
#		$FALSE otherwise
#
#############################################################################
server_running() {
  # get the server process PID, or zero if no PID file
  get_server_pid; kill_pid=$?
  if [ $kill_pid -ne 0 ]; then
    # try killing this process with signal zero
    if kill -0 $kill_pid 2>/dev/null; then
      # the process is running
      return $TRUE
    fi
  fi

  # either the PID file does not exist, or the kill failed; in both
  # cases this means that the server isn't running
  return $FALSE
}

#############################################################################
#
# pgsql_start
#
# start the server daemon running
#
#############################################################################
pgsql_start() {
  # see if the server is already running, and if it is then
  # just return without further action
  if server_running; then
    dspmsg $CAT 2 "%s is already running.\n" $PGSQL_LABEL
    return
  fi

  # it's not running, so remove any old, stale PID file that
  # might be lurking about
  rm -f $PGSQLPIDFILE >/dev/null 2>&1

  # start the server
  if test -d $PGSQLDATA -a -f $PGSQLDATA/PG_VERSION; then
    /bin/su -S -s /bin/sh -c "$PGCTL start -D $PGSQLDATA $PGSQLSTARTOPTS" $PGSQLUSER
  else
    echo "The database directory: $PGSQLDATA"
    echo "  does not exist or has not been initialized. Please run mkdev pgsql."
  fi
}


#############################################################################
#
# pgsql_stop
#
# stop the server daemon if it is running
#
#############################################################################
pgsql_stop() {
  # see if the server is already running, and if it is not then
  # return without further action
  if server_running; then
    :
  else
    # the server isn't running
    dspmsg $CAT 7 "%s does not appear to be running.\n" $PGSQL_LABEL
    return
  fi

  /bin/su -S -s /bin/sh -c "$PGCTL $STOP_ARGS -D $PGSQLDATA" $PGSQLUSER
}


#############################################################################
#
# pgctl_restart
#
# restart the server daemon by sending it a SIGHUP
#
#############################################################################
pgsql_restart() {
  # see if the server is running, and if not then return
  # without further action
  if server_running; then
    :
  else
    # the server isn't running
    dspmsg $CAT 11 "%s does not appear to be running.\n" $PGSQL_LABEL
    return
  fi

  if test -d $PGSQLDATA -a -f $PGSQLDATA/PG_VERSION; then
    /bin/su -S -s /bin/sh -c "$PGCTL $RESTART_ARGS -D $PGSQLDATA $PGSQLSTARTOPTS" $PGSQLUSER
  else
    echo "The database directory: $PGSQLDATA"
    echo "  does not exist or has not been initialized. Please run mkdev pgsql."
  fi
}


#############################################################################
#
# pgsql_enable
#
# add a link to this script into the /etc/rc2.d directory, then start
# the server
#
#############################################################################
pgsql_enable() {
  # create the /etc/rc2.d startup file
  if [ -f /etc/rc2.d/P$SCRIPTNUM$SCRIPT ]; then
    # file already exists
    test -z "$ISL" && dspmsg $CAT 15 "%s is already enabled.\n" $PGSQL_LABEL
  else
    # link in the file
    test -z "$ISL" && dspmsg $CAT 16 "Enabling %s ... " $PGSQL_LABEL
    ln $SCRIPTDIR/$SCRIPT /etc/rc2.d/P$SCRIPTNUM$SCRIPT
    ln $SCRIPTDIR/$SCRIPT /etc/rc0.d/P$SCRIPTNUM$SCRIPT
    test -z "$ISL" && dspmsg $CAT 17 "done.\n"
  fi

  # start the server
  test -z "$ISL" && pgsql_start
}


#############################################################################
#
# pgsql_disable
#
# stop the server, then remove any links to this script in /etc/rc2.d
#
#############################################################################
pgsql_disable() {
  # stop the server
  pgsql_stop

  # remove the /etc/rc2.d startup file
  if [ -f /etc/rc2.d/P$SCRIPTNUM$SCRIPT ]; then
    # unlink the file
    test -z "$ISL" && dspmsg $CAT 18 "Disabling %s ... " $PGSQL_LABEL
    rm -f /etc/rc2.d/P$SCRIPTNUM$SCRIPT
    rm -f /etc/rc0.d/P$SCRIPTNUM$SCRIPT
    test -z "$ISL" && dspmsg $CAT 19 "done.\n"
  else
    # file does not exist
    test -z "$ISL" && dspmsg $CAT 20 "%s is already disabled.\n" $PGSQL_LABEL
  fi
}


#############################################################################
#
# pgsql_query
#
# query the server to see if it is running
#
#############################################################################
pgsql_query() {
  # test for the server process
  if server_running; then
    if test -z "$MKDEV_INVOKED"; then
      dspmsg $CAT 21 "%s is currently running.\n" $PGSQL_LABEL
    fi
    MKDEVRET="R"
  else
    if test -z "$MKDEV_INVOKED"; then
      dspmsg $CAT 22 "%s is not running.\n" $PGSQL_LABEL
    fi
    MKDEVRET="N"
  fi

  # test for the /etc/rc2.d startup on boot file
  if [ -f /etc/rc2.d/P$SCRIPTNUM$SCRIPT ]; then
    if test -z "$MKDEV_INVOKED"; then
      dspmsg $CAT 23 "%s is enabled.\n" $PGSQL_LABEL
    fi
    MKDEVRET="${MKDEVRET} E"
  else
    if test -z "$MKDEV_INVOKED"; then
      dspmsg $CAT 24 "%s is disabled.\n" $PGSQL_LABEL
    fi
    MKDEVRET="${MKDEVRET} D"
  fi
  if test -n "$MKDEV_INVOKED"; then
    echo "$MKDEVRET"
  fi
}


#############################################################################
# main()
#############################################################################

# function to perform is in $1
function="$1"

# execute the routine for the requested function
case $function in
start)
	pgsql_start	;;
stop)
        STOP_ARGS="$*"
	pgsql_stop	;;
enable)
	pgsql_enable	;;
disable)
	pgsql_disable	;;
query)
	pgsql_query	;;
status)
	pgsql_query	;;
restart)
        RESTART_ARGS="$*"
	pgsql_restart	;;
*)
	dspmsg $CAT 1 "$USAGE\n" `basename $0`
	exit 1		;;
esac

# done
exit 0
