#!/bin/sh
#
# glbd          Start/Stop the Galera Load Balancer daemon.
#
# processname: glbd
# chkconfig: 2345 90 60
# description: GLB is a TCP load balancer similar to Pen. \
#              It lacks most of advanced Pen features, as \
#              the aim was to make a user-space TCP proxy which is \
#              as fast as possible. It can utilize multiple CPU cores. \
#              A list of destinations can be configured at runtime. \
#              Destination "draining" is supported. It features \
#              weight-based connection balancing (which becomes \
#              round-robin if weights are equal).

### BEGIN INIT INFO
# Provides: glbd
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start:  2345
# Default-Stop: 90
# Short-Description: run glbd daemon
# Description: GLB is a TCP load balancer similar to Pen.
### END INIT INFO

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

[ `id -u` = 0 ] || exit 0

prog="glbd"

GLB_THREADS="2"
GLB_CONTROL_HOST="127.0.0.1"
GLB_CONTROL_PORT="3333"
GLB_LISTEN="3300"
GLB_TARGETS=""

# Source usr/localions file
if [ -f /etc/sysconfig/$prog ]; then
        . /etc/sysconfig/$prog
fi

RETVAL=0

start() {
	[ -f /var/run/$prog.pid ] && exit 0
	if [ -z "${GLB_TARGETS}" ]; then
		echo "    ERROR: Targets not defined in /etc/sysconfig/$prog."
		exit 1
	fi
	echo -n $"Starting $prog: "
	daemon --pidfile /var/run/$prog.pid "$prog --daemon --control $GLB_CONTROL_HOST:$GLB_CONTROL_PORT --threads $GLB_THREADS $GLB_LISTEN $GLB_TARGETS >/dev/null 2>&1"
	RETVAL=$?
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc $prog
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
	return $RETVAL
}

restart() {
	stop
	start
}

getinfo() {
	echo getinfo | /usr/bin/nc $GLB_CONTROL_HOST $GLB_CONTROL_PORT && exit 0
	echo "[`/bin/date`] $prog: failed to query 'getinfo' from $GLB_CONTROL_HOST:$GLB_CONTROL_PORT"
	exit 1
}

getstats() {
	echo getstats | /usr/bin/nc $GLB_CONTROL_HOST $GLB_CONTROL_PORT && exit 0
	echo "[`/bin/date`] $prog: failed to query 'getstats' from $GLB_CONTROL_HOST:$GLB_CONTROL_PORT"
	exit 1
}

add() {
	if [ "$1" == "" ]; then
		echo $"Usage: $0 add :[:]"
		exit 1
	fi
	if [ "`echo "$1" | /usr/bin/nc $GLB_CONTROL_HOST $GLB_CONTROL_PORT`" == "Ok" ]; then
		echo "[`/bin/date`] $prog: added '$1' successfully"
		#getinfo
		exit 0
	fi
	echo "[`/bin/date`] $prog: failed to add target '$1'."
	exit 1
}

remove() {
	if [ "$1" == "" ]; then
		echo $"Usage: $0 remove :"
		exit 1
	fi
	if [ "`echo "$1:-1" | /usr/bin/nc $GLB_CONTROL_HOST $GLB_CONTROL_PORT`" == "Ok" ]; then
		echo "[`/bin/date`] $prog: removed '$1' successfully"
		#getinfo
		exit 0
	fi
	echo "[`/bin/date`] $prog: failed to remove target '$1'."
	exit 1
}

case $1 in
	start)
		start
	;;
	stop)
		stop
	;;
	restart|reload|force-reload)
		restart
	;;
	condrestart)
		[ -f /var/lock/subsys/$prog ] && restart || :
	;;
	status|getinfo)
		getinfo
	;;
	getstats)
		getstats
	;;
	add)
		add $2
	;;
	remove)
		remove $2
	;;
	*)
		echo $"Usage: $0 {start|stop|restart|status|getstats|getinfo|add|remove}"
		exit 1
	;;
esac

exit $?
