#!/bin/sh
#
# pound
#
# chkconfig:	345 85 15
# description:	reverse-proxy and load-balancer
#

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# List of instances to start.
POUND_INSTANCES="pound"

# Get service config
[ -f /etc/sysconfig/pound ] && . /etc/sysconfig/pound

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down pound
		exit 1
	fi
else
	exit 0
fi

configtest() {
	local instance="$1"
	pound -c -f /etc/pound/$instance.yaml > /dev/null
}

# check if the $1 instance is up
is_up() {
	local instance="$1"
	local pidfile=/var/run/pound/$instance.pid
	[ -f $pidfile ] || return 1
	local pid=$(cat $pidfile)
	kill -0 $pid 2>/dev/null
	return $?
}

# check if any of the instances are up
any_up() {
	local ret=1 instance pidfile

	for pidfile in /var/run/pound/*.pid; do
		[ -f "$pidfile" ] || continue
		instance=${pidfile#/var/run/pound/}
		instance=${instance%.pid}
		is_up $instance || continue
		ret=0
	done

	return $ret
}

# check if all of the instances are up
all_up() {
	local ret=1 instance pidfile

	for pidfile in /var/run/pound/*.pid; do
		[ -f "$pidfile" ] || continue
		instance=${pidfile#/var/run/pound/}
		instance=${instance%.pid}
		is_up $instance && continue
		ret=0
	done

	return $ret
}

start() {
	local ret started=0 found=0 instance

	# Check if the service is already running?
	if all_up; then
		msg_already_running "Pound"
		return
	fi

	msg_starting "Pound"; started
	for instance in $POUND_INSTANCES; do
		show "Starting Pound instance %s" "$instance"
		if is_up $instance; then
			started
			continue
		fi

		PIDFILE=/var/run/pound/$instance.pid
		start-stop-daemon --start \
			--exec /usr/sbin/pound \
			--pidfile $PIDFILE -- -v -f /etc/pound/$instance.yaml -p $PIDFILE
		ret=$?

		if [ $ret -eq 0 ]; then
			ok
			RETVAL=$ret
			started=1
			found=1
		else
			fail
		fi
	done

	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pound
}

stop() {
	# Stop daemons.
	if ! any_up; then
		msg_not_running "Pound"
		return
	fi

	local ret instance
	msg_stopping "Pound"; started
	for instance in $POUND_INSTANCES; do
		is_up $instance || continue
		show "Stopping Pound instance %s" "$instance"; busy
		killproc --pidfile pound/$instance.pid pound
		ret=$?
	done
	rm -f /var/lock/subsys/pound > /dev/null 2>&1
	rm -f /var/run/pound/$instance.pid > /dev/null 2>&1
}

restart() {
	local instance

	if any_up; then
		# make up list of configured and up instances
		local list
		show "Checking configuration"; busy
		for instance in $POUND_INSTANCES; do
			# skip ones whose config fails
			configtest $instance || continue
			list="$list $instance"
		done

		# nothing left or never was
		if [ -z "$list" ]; then
			fail
			return
		else
			POUND_INSTANCES=$list
			ok
		fi

	fi

	stop
	start
}

condrestart() {
	if [ ! -f /var/lock/subsys/pound ]; then
		msg_not_running "Pound"
		RETVAL=$1
		return
	fi

	stop
	start
}

pound_status() {
	local stat=1 pidfile instance

	nls "Configured Pound instances:"
   	echo " $POUND_INSTANCES"
	nls "Currently active Pound instances:"
	for pidfile in /var/run/pound/*.pid; do
		[ -f "$pidfile" ] || continue
		instance=${pidfile#/var/run/pound/}
		instance=${instance%.pid}
		is_up $instance && echo -n " $instance($(cat $pidfile))"
		stat=0
	done
	echo ""
	exit $stat
}

if [ "$1" != status -a "$2" ]; then
	POUND_INSTANCES="$2"
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	restart
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  flush-logs)
	if [ -f /var/lock/subsys/pound ]; then
		for instance in $POUND_INSTANCES; do
			show "Rotating Pound logs for %s instance" $instance
			killproc --pidfile /var/run/pound/$instance.pid pound -USR1
			ret=$?
			if [ $ret != 0 ]; then
			   	RETVAL=$ret
			fi
		done
	else
		msg_not_running "Pound"
		RETVAL=7
	fi
	;;
  status)
	pound_status
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|flush-logs|status} [INSTANCE NAMES]"
	exit 3
esac

exit $RETVAL
