#!/bin/sh
# pgpool    This is the init script for starting up pgpool-II
#
# chkconfig: - 74 33
# description: Starts and stops the pgpool daemon
# processname: pgpool
# pidfile:	/var/run/pgpool.pid
#
# v1.0.0 Devrim GUNDUZ <devrim@gunduz.org>
# - Initial version of Red Hat / Fedora init script
#
# v2.2 Devrim GUNDUZ <devrim@gunduz.org>
# - New and improved version which has some fixes.
#
# v2.2.5 Devrim GUNDUZ <devrim@gunduz.org>
# - Fix logging.
#
# v2.3 Devrim GUNDUZ <devrim@gunduz.org>
# - Adjust order of startup and kill, per RH bugzilla #545739.
#
# v3.4 Jeff Frost <jeff@pgexperts.com>
# - Bring pgpool init script more in line with
#   pgpool community init script

# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions

# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
# We need it for pgpool
[ "${NETWORKING}" = "no" ] && exit 0

# 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

# Set defaults for configuration variables
PGPOOLUSER=postgres
PGPOOLENGINE=/usr/pgpool-9.1/bin
PGPOOLDAEMON=$PGPOOLENGINE/pgpool
PGPOOLCONF=/etc/pgpool-II-91/pgpool.conf
PGPOOLPIDDIR=/var/run/pgpool-II-91
PGPOOLLOG=/var/log/pgpool-II-91.log

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

# Import configuration from /etc/sysconfig, if it exists
if [ -r /etc/sysconfig/pgpool-II-91 ]; then
    . /etc/sysconfig/pgpool-II-91
fi

test -x $PGPOOLDAEMON || exit 5

# Check whether the config file exists or not
if [ ! -r $PGPOOLCONF ]
then
    echo "$PGPOOLCONF not found"
    echo_failure
    echo
    exit 1
fi

# Create the log file if it does not exist
if [ ! -x $PGPOOLLOG ]
then
    touch $PGPOOLLOG
    chown ${PGPOOLUSER}: $PGPOOLLOG
fi

if [ ! -d $PGPOOLPIDDIR ]
then
    mkdir $PGPOOLPIDDIR
    chown ${PGPOOLUSER}: $PGPOOLPIDDIR
fi

script_result=0

start(){
    PGPOOL_START=$"Starting ${NAME} service: "

    echo -n "$PGPOOL_START"
    pid="`pidofproc -p "$pidfile" -s "$PGPOOLDAEMON"`"
    if [ -n "$pid" ] ; then
        echo -n "pgpool is already running with pid $pid "
        echo_failure
        script_result=1
    else

        $SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF $OPTS & " >> "$PGPOOLLOG" 2>&1 < /dev/null
        sleep 2

        pid="`pidofproc -p "$pidfile" -s "$PGPOOLDAEMON"`"
        if [ -n "$pid" ] ; then
            echo_success
            touch "$lockfile"
        else
            echo_failure
            script_result=1
        fi
    fi
    echo
}

stop(){
    PGPOOL_STOP=$"Stopping ${NAME} service: "

    echo -n "$PGPOOL_STOP"
    if [ -e "$lockfile" ] ; then

        $SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF -m fast stop" >> "$PGPOOLLOG" 2>&1 < /dev/null

        RETVAL=$?
        if [ $RETVAL -eq 0 ] ; then
            echo_success
            rm -f "$lockfile"
        else
            echo_failure
            script_result=1
        fi
    else
        echo_success
    fi
    echo
}

restart(){
    stop
    start
}

reload(){
    PGPOOL_RELOAD=$"Reloading ${NAME} configuration: "

    echo -n "$PGPOOL_RELOAD"
    pid="`pidofproc -p $pidfile $PGPOOLDAEMON`"
    if [ -n "$pid" ] ; then
        $SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF reload" >> "$PGPOOLLOG" 2>&1 < /dev/null
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            echo_success
        else
            echo_failure
            script_result=1
        fi
    else
        echo -n "pgpool is not running "
        echo_failure
        script_result=1
    fi
    echo
}

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

condstop(){
    [ -e "${lockfile}" ] && stop
}

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

exit $script_result
