#!/bin/sh
#
# haproxy
#
# chkconfig:   - 85 15
# description:	HAProxy is a free, very fast and reliable solution \
#				offering high availability, load balancing, and \
#				proxying for TCP and  HTTP-based applications
# processname: haproxy
# config:	   /etc/kazoo/haproxy/haproxy.cfg
# pidfile:	   /var/run/haproxy/kazoo-haproxy.pid

RETVAL=0
LOCK_FILE=/var/lock/subsys/kazoo-haproxy

. /etc/rc.d/init.d/functions

start() {
	echo -n $"Starting HAProxy: "
	/usr/sbin/kazoo-haproxy prepare >/dev/null 2>&1
        daemon "/usr/sbin/kazoo-haproxy background >/dev/null"
	RETVAL=$?
        if [ ${RETVAL} -eq 0 ]; then
                touch ${LOCK_FILE}
                success $"OK"
                echo
        else
                failure $"HAProxy is already running or failed to start!"
                echo
        fi
}

stop() {
	echo -n $"Stopping HAProxy: "
	/usr/sbin/kazoo-haproxy stop >/dev/null 2>&1
	RETVAL=$?
        if [ ${RETVAL} -eq 0 ]; then
                rm -f ${LOCK_FILE} ${pidfile}
                success $"OK"
                echo
        else
                failure $"HAProxy is still running!"
                echo
        fi
}

status() {
	/usr/sbin/kazoo-haproxy status
	RETVAL=$?
}

restart() {
	stop
	start
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        status)
                status
                ;;
	check)
		/usr/sbin/kazoo-haproxy check
		;;
        restart|reload)
                restart
                ;;
        condrestart)
                [ ! -e ${LOCK_FILE} ] && restart
                ;;
        *)
                echo $"Usage: $0 (start|stop|restart|status|check)"
                RETVAL=1
esac

exit ${RETVAL}
