#!/bin/sh
# chkconfig: - 64 36

# 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

# 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


BOUNCERCONF=/etc/pgbouncer.ini
BOUNCERLOG=/var/log/pgbouncer.log

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

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

script_result=0

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

	# Make sure startup-time log file is valid
	if [ ! -e "$BOUNCERLOG" -a ! -h "$BOUNCERLOG" ]
	then
		touch "$BOUNCERLOG" || exit 1
		chown postgres:postgres "$BOUNCERLOG"
		chmod go-rwx "$BOUNCERLOG"
		[ -x /usr/bin/chcon ] && /usr/bin/chcon -u system_u -r object_r -t postgresql_log_t "$BOUNCERLOG" 2>/dev/null
	fi

	echo -n "$BOUNCER_START"
	$SU -l postgres -c "pgbouncer '$BOUNCERCONF' & " >> "$BOUNCERLOG" 2>&1 < /dev/null
	sleep 2
	pid=`pidof -s pgbouncer`
        if [ $pid ]
        then
          success "$BOUNCER_START"
          touch /var/lock/subsys/${NAME}
          echo "$pid" > "/var/run/${NAME}.pid"
          echo
        else
          failure "$BOUNCER_START"
          echo
          script_result=1
        fi
}

stop(){
        echo -n $"Stopping ${NAME} service: "
        kill -TERM `cat "/var/run/${NAME}.pid"`
        ret=$? 
        if [ $ret -eq 0 ]
        then
                echo_success
        else
                echo_failure
                script_result=1
        fi
        echo
          rm -f "/var/run/${NAME}.pid"
          rm -f "/var/lock/subsys/${NAME}"
}

restart(){
    stop
    start
}

condrestart(){
    [ -e /var/lock/subsys/${NAME} ] && restart
}

condstop(){
    [ -e /var/lock/subsys/${NAME} ] && stop
}

reload(){
    $SU -l postgres -c "kill -HUP `cat /var/run/${NAME}.pid`" > /dev/null 2>&1 < /dev/null
}
pause(){
    $SU -l postgres -c "kill -USR1 `cat /var/run/${NAME}.pid`" > /dev/null 2>&1 < /dev/null
}
resum(){
   $SU -l postgres -c "kill -USR2 `cat /var/run/${NAME}.pid`" > /dev/null 2>&1 < /dev/null
}
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status pgbouncer 
	script_result=$?
	;;
  restart)
	restart
	;;
  pause)
	pause
	;;
  continue)
	resum
	;;
  condrestart)
	condrestart
	;;
  condstop)
	condstop
	;;
  reload|force-reload)
	reload
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|pause|continue|reload|force-reload|condrestart|condstop}"
	exit 1
esac

exit $script_result
