#!/usr/bin/bash
#
# drlb		Start up the DR load balancer service
#
# chkconfig: 345 60 30
# description: DR TCP load balancer for software load balancing.
#
# processname: drlb_server
# config: /etc/drlb/active_configs.list

### BEGIN INIT INFO
# Provides: drlb_server
# Required-Start: $local_fs $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the DRLB server daemon
# Description:       DR TCP load balancer for software load balancing.
### END INIT INFO

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

if [ ! -f /etc/drlb/active_configs.list ]; then
   echo "ERROR - No configs found missing: /etc/drlb/active_configs.list"
   exit 1
fi 

error_level=0

LBPROCNAME="drlb_server"
LBPROC="/usr/sbin/${LBPROCNAME}"
ACTIVE_CONFIGS="/etc/drlb/active_configs.list"
SINGLECONFIG="${2}"

# System specific ENV overrides
if [ -f /etc/sysconfig/drlb_server ]; then . /etc/sysconfig/drlb_server; fi

start_with_config() {
    CFGFILE="${1}"
    if [ ! -f ${CFGFILE} ]; then 
	echo "ERROR - Config file not found: ${CFGFILE}"
	error_leve=1
	return
    fi
    pid=$(ps -ef | grep ${LBPROCNAME} | grep -- ${CFGFILE} | grep -v grep | awk '{ print $2 }')
    if [ ! -z $pid ]; then
	echo "${LBPROCNAME} with ${CONFIGFILE} config already running"
    else
	echo "Starting ${LBPROCNAME} with ${CFGFILE} config"
	${LBPROC} --config-file=${CFGFILE} > /dev/null &
	error_level=$?
    fi
}

start() { 
    if [ ! -z ${SINGLECONFIG} ]; then
	start_with_config ${SINGLECONFIG}
	return
    fi

    while read line
    do
	CONFIGFILE=$(echo $line | sed s/' '//g | grep -v "^#")
	if [ "${CONFIGFILE}" == "" ]; then continue; fi
	if [ ! -f ${CONFIGFILE} ]; then echo "ERROR - Config file not found: ${CONFIGFILE}"; continue; fi
	start_with_config ${CONFIGFILE}
    done < ${ACTIVE_CONFIGS}
}

stop_with_config() {
    CFGFILE="${1}"
    pid=$(ps -ef | grep ${LBPROCNAME} | grep -- ${CFGFILE} | grep -v grep | awk '{ print $2 }')
    if [ ! -z $pid ]; then
	echo "Stopping ${LBPROCNAME} with ${CFGFILE} config"
	kill ${pid}
	sleep 15
	pid=$(ps -ef | grep ${LBPROCNAME} | grep -- ${CFGFILE} | grep -v grep | awk '{ print $2 }')
	if [ ! -z $pid ]; then
	    echo "Forcing LB shutdown with ${CFGFILE} config after $count retries"
	    kill -9 ${pid}
	    error_level=1
	    echo "${LBPROCNAME} with ${CFGFILE} stopped"
	    return;
	fi
	echo "${LBPROCNAME} with ${CFGFILE} stopped"
	error_level=0
    else
	echo "${LBPROCNAME} with ${CFGFILE} not running"
	error_level=1
    fi
}

stop() {
    if [ ! -z ${SINGLECONFIG} ]; then
	stop_with_config ${SINGLECONFIG}
	return
    fi

    while read line
    do
	CONFIGFILE=$(echo $line | sed s/' '//g | grep -v "^#")
	if [ "${CONFIGFILE}" == "" ]; then continue; fi
	stop_with_config ${CONFIGFILE}
    done < ${ACTIVE_CONFIGS}
}

case "${1}" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status ${LBPROCNAME}
        ;;
    restart|reload|condrestart)
        stop
        start
        ;;
    *)
        echo $"Usage: ${0} {start|stop|restart|status}"
        exit 1
esac

exit $error_level
