#!/bin/sh
### BEGIN INIT INFO
# Provides: glite-lb-bkserverd
# Description: L&B server daemon
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
# chkconfig: 345 75 25
#
# Copyright (c) Members of the EGEE Collaboration. 2004-2010.
# See http://www.eu-egee.org/partners/ for details on the copyright holders.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

GLITE_LB_LOCATION=${GLITE_LB_LOCATION:-'@glite_prefix@'}
GLITE_LB_LOCATION_VAR=${GLITE_LB_LOCATION_VAR:-'@glite_var@'}
GLITE_LB_LOCATION_ETC=${GLITE_LB_LOCATION_ETC:-'@glite_etc@'}
GLITE_JP_LOCATION=${GLITE_JP_LOCATION:-$GLITE_LB_LOCATION}

[ -f /etc/profile.d/grid-env.sh ] && . /etc/profile.d/grid-env.sh
[ -f /etc/glite.conf ] && . /etc/glite.conf
[ -f $GLITE_LB_LOCATION_ETC/glite-wms.conf ] && . $GLITE_LB_LOCATION_ETC/glite-wms.conf

[ -f $GLITE_LB_LOCATION_ETC/lb.conf ] && . $GLITE_LB_LOCATION_ETC/lb.conf
[ -f $GLITE_LB_LOCATION_VAR/etc/lb.conf ] && . $GLITE_LB_LOCATION_VAR/etc/lb.conf

[ -f $HOME/.glite.conf ] && . $HOME/.glite.conf

[ -n "$GLITE_LB_SERVER_PIDFILE" ] && BK_PIDFILE=$GLITE_LB_SERVER_PIDFILE ||
	BK_PIDFILE=$GLITE_LB_LOCATION_VAR/glite-lb-bkserverd.pid
[ -z "$GLITE_LB_NOTIF_SOCK" ] && GLITE_LB_NOTIF_SOCK="/tmp/glite-lb-notif.sock"
[ -n "$GLITE_JP_IMPORTER_PIDFILE" ] && jp_importer_pidfile=$GLITE_JP_IMPORTER_PIDFILE ||
	jp_importer_pidfile=$GLITE_LB_LOCATION_VAR/glite-jp-importer.pid

GLITE_LB_TYPE=${GLITE_LB_TYPE:-'server'} # server, proxy, both
GLITE_LB_PROXY_SOCK=${GLITE_LB_PROXY_SOCK:-'/tmp/glite-lbproxy-ilog.sock'}
GLITE_LB_PROXY_FPREFIX=${GLITE_LB_PROXY_FPREFIX:-'/tmp/glite-lbproxy-ilog_events'}

PROXY_IL_PIDFILE=$GLITE_LB_LOCATION_VAR/glite-lb-proxy-interlogd.pid
NOTIF_IL_PIDFILE=$GLITE_LB_LOCATION_VAR/glite-lb-notif-interlogd.pid

msgconf="$GLITE_LB_LOCATION_ETC/glite-lb/msg.conf"
if [ -f "$msgconf" ]; then
	GLITE_LB_NOTIF_IL_OTHER_OPTIONS="$GLITE_LB_NOTIF_IL_OTHER_OPTIONS -F $msgconf"
	GLITE_LB_SERVER_OTHER_OPTIONS="$GLITE_LB_SERVER_OTHER_OPTIONS -F $msgconf"
fi

lockfile=/var/lock/glite-lb-bkserverd

unset creds port log4c

start_daemon()
{
	local name="$1"
	local pidfile="$2"
	local cmd="$3"
	local stale="$4"

	if [ -f "$pidfile" ]; then
		if ps p `cat $pidfile` >/dev/null 2>&1; then
			return 0
		fi
		echo "Warning: stale $pidfile for $name"
		rm -f "$pidfile"
		if [ -n "$stale" ]; then
			for file in $stale; do
				echo "Warning: stale $file for $name"
				rm -f "$file"
			done
		fi
	fi
	echo -n "Starting $name ..."
	su - $GLITE_USER -c "$log4c $cmd"

	if [ $? -eq 0 ]; then
		echo " done"
		touch $lockfile
	else
		echo " FAILED"
	fi
}

stop_daemon()
{
	name="$1"
	local pidfile="$2"

	if [ -f "$pidfile" ]; then
		pid=`cat "$pidfile"`
		echo -n Stopping $name \($pid\) ...
		kill $pid
		try=0
		while ps p $pid >/dev/null 2>&1; do 
			sleep 1;
			try=`expr $try + 1`
			if [ $try = 20 ]; then
				kill -9 $pid
				echo " force quit after $try retries"
				rm -f $lockfile
				return 1
			fi
	 	done
		echo " done"
		rm -f $lockfile
		rm -f $pidfile
	else
		echo $name not running
		return 1
	fi
}

status_daemon()
{
	local name="$1"
	local pidfile="$2"
	local enabled="$3"

	if [ -f "$pidfile" ]; then
		pid=`cat "$pidfile"`
		if ps p $pid >/dev/null 2>&1; then
			echo "$name running as $pid"
		else
			echo "$name not running (stale pidfile)"
			return 1
		fi
	else
		if test -n "$enabled" -a x"$enabled" = x"0"; then
			echo "$name not running (disabled)"
			return 0
		else
			echo "$name not running"
			return 1
		fi
	fi

	return 0
}

start()
{
	if test -z "$GLITE_USER" ;then
		echo 'Error: GLITE_USER is not set'
		echo FAILED
		return 1
	fi

	[ -n "$GLITE_HOST_CERT" -a -n "$GLITE_HOST_KEY" ] &&
		creds="-c $GLITE_HOST_CERT -k $GLITE_HOST_KEY"

	if test -z "$creds"; then
		if su - $GLITE_USER -c "test -r /etc/grid-security/hostkey.pem -a -r /etc/grid-security/hostcert.pem"; then
			echo "$0: WARNING: /etc/grid-security/hostkey.pem readable by $GLITE_USER"
			creds="-c /etc/grid-security/hostcert.pem -k /etc/grid-security/hostkey.pem"
		fi
	fi

	log4c="LOG4C_RCPATH='$GLITE_LB_LOCATION_ETC/glite-lb'"

	policy="$GLITE_LB_LOCATION_ETC/glite-lb/glite-lb-authz.conf"
	lcas_log="LCAS_LOG_FILE='/var/log/glite/glite-lb-server-lcas.log' LCAS_ETC_DIR='$GLITE_LB_LOCATION_ETC/glite-lb'"
	if test -f "$policy"; then
		# lcas not enabled by default yet
		policy="--policy '$policy'"
	else
		unset policy
		unset lcas_log
	fi

	[ -z "$GLITE_LB_EXPORT_DUMPDIR" ] && GLITE_LB_EXPORT_DUMPDIR=$GLITE_LB_LOCATION_VAR/dump
	dumpdir="--dump-prefix $GLITE_LB_EXPORT_DUMPDIR"
	[ -d "$GLITE_LB_EXPORT_DUMPDIR" ] || mkdir -p "$GLITE_LB_EXPORT_DUMPDIR" && chown $GLITE_USER:$GLITE_GROUP -R "$GLITE_LB_EXPORT_DUMPDIR"

	[ -z "$GLITE_LB_EXPORT_PURGEDIR" ] && GLITE_LB_EXPORT_PURGEDIR=$GLITE_LB_LOCATION_VAR/purge
	purgedir="--purge-prefix $GLITE_LB_EXPORT_PURGEDIR"
	[ -d "$GLITE_LB_EXPORT_PURGEDIR" ] || mkdir -p "$GLITE_LB_EXPORT_PURGEDIR" && chown $GLITE_USER:$GLITE_GROUP -R "$GLITE_LB_EXPORT_PURGEDIR"

	if [ x"$GLITE_LB_EXPORT_ENABLED" = x"true" ]; then
		if [ ! -x $GLITE_JP_LOCATION/bin/glite-jp-importer ]; then
			echo "glite-jp-importer needed for export to Job Provenace, install glite-jp-client package too."
			exit 1
		fi

		# registrations
		[ -z "$GLITE_LB_EXPORT_JPREG_MAILDIR" ] && GLITE_LB_EXPORT_JPREG_MAILDIR=$GLITE_LB_LOCATION_VAR/jpreg
		jpreg_maildir="--reg-mdir $GLITE_LB_EXPORT_JPREG_MAILDIR"
		lbreg_maildir="--jpreg-dir $GLITE_LB_EXPORT_JPREG_MAILDIR"
		[ -d "$GLITE_LB_EXPORT_JPREG_MAILDIR" ] || mkdir -p "$GLITE_LB_EXPORT_JPREG_MAILDIR" && chown $GLITE_USER:$GLITE_GROUP -R "$GLITE_LB_EXPORT_JPREG_MAILDIR"

		# dumps
		[ -z "$GLITE_LB_EXPORT_JPDUMP_MAILDIR" ] && GLITE_LB_EXPORT_JPDUMP_MAILDIR=$GLITE_LB_LOCATION_VAR/jpdump
		jpdump_maildir="--dump-mdir $GLITE_LB_EXPORT_JPDUMP_MAILDIR"
		[ -d "$GLITE_LB_EXPORT_JPDUMP_MAILDIR" ] || mkdir -p "$GLITE_LB_EXPORT_JPDUMP_MAILDIR" && chown $GLITE_USER:$GLITE_GROUP -R "$GLITE_LB_EXPORT_JPDUMP_MAILDIR"

		# sandboxes
		[ -z "$GLITE_LB_EXPORT_SANDBOX_MAILDIR" ] && GLITE_LB_EXPORT_SANDBOX_MAILDIR=$GLITE_LB_LOCATION_VAR/sandbox
		sandbox_maildir="--sandbox-mdir $GLITE_LB_EXPORT_SANDBOX_MAILDIR"
		[ -d "$GLITE_LB_EXPORT_SANDBOX_MAILDIR" ] || mkdir -p "$GLITE_LB_EXPORT_SANDBOX_MAILDIR" && chown $GLITE_USER:$GLITE_GROUP -R "$GLITE_LB_EXPORT_SANDBOX_MAILDIR"

		# target Job Provenance
		[ -n "$GLITE_LB_EXPORT_JPPS" ] && jpps="--jpps $GLITE_LB_EXPORT_JPPS"
	fi

	[ -z "$creds" ] && echo $0: WARNING: No credentials specified. Using default lookup which is dangerous. >&2

	[ -n "$GLITE_LB_SERVER_PORT" ] && port="-p $GLITE_LB_SERVER_PORT"
	[ -n "$GLITE_LB_SERVER_WPORT" ] && wport="-w $GLITE_LB_SERVER_WPORT"
	[ -z "$GLITE_LB_NOTIF_FPREFIX" ] && GLITE_LB_NOTIF_FPREFIX="/var/tmp/glite-lb-notif"
	if [ -n "$GLITE_LB_SERVER_DEBUG" ] ; then
		if which tscat >/dev/null; then
			GLITE_LB_SERVER_OTHER_OPTIONS="$GLITE_LB_SERVER_OTHER_OPTIONS -d -s 1 2>&1 | tscat >> $GLITE_LB_LOCATION_VAR/lb.log &"
			GLITE_LB_NOTIF_OTHER_OPTIONS="$GLITE_LB_NOTIF_OTHER_OPTIONS -v -d 2>&1 | tscat >> $GLITE_LB_LOCATION_VAR/notif-il.log &"
		else
			GLITE_LB_SERVER_OTHER_OPTIONS="$GLITE_LB_SERVER_OTHER_OPTIONS -d -s 1 >> $GLITE_LB_LOCATION_VAR/lb.log 2>&1 &"
			GLITE_LB_NOTIF_OTHER_OPTIONS="$GLITE_LB_NOTIF_OTHER_OPTIONS -v -d >> $GLITE_LB_LOCATION_VAR/notif-il.log 2>&1 &"
		fi
	fi
	case "$GLITE_LB_TYPE" in
	proxy)
		proxy="-P --proxy-il-sock $GLITE_LB_PROXY_SOCK --proxy-il-fprefix $GLITE_LB_PROXY_FPREFIX"
		;;
	both)
		proxy="-B --proxy-il-sock $GLITE_LB_PROXY_SOCK --proxy-il-fprefix $GLITE_LB_PROXY_FPREFIX"
		;;
	*)
		proxy=''
		;;
	esac

	start_daemon glite-lb-bkserver "$BK_PIDFILE" "$lcas_log $GLITE_LB_LOCATION/bin/glite-lb-bkserverd \
		--notif-il-sock=$GLITE_LB_NOTIF_SOCK \
		--notif-il-fprefix=$GLITE_LB_NOTIF_FPREFIX \
		$super $creds -i $BK_PIDFILE $port $wport $dumpdir $purgedir $lbreg_maildir $proxy $policy\
		$GLITE_LB_SERVER_OTHER_OPTIONS" "/tmp/lb_proxy_serve.sock /tmp/lb_proxy_store.sock"

	if test -x $GLITE_LB_LOCATION/bin/glite-lb-notif-interlogd; then
		start_daemon glite-lb-notif-interlogd "$NOTIF_IL_PIDFILE" "$GLITE_LB_LOCATION/bin/glite-lb-notif-interlogd \
			-f $GLITE_LB_NOTIF_FPREFIX -s $GLITE_LB_NOTIF_SOCK \
			-i $NOTIF_IL_PIDFILE -M 10485760 \
			$creds $GLITE_LB_NOTIF_IL_OTHER_OPTIONS" "$GLITE_LB_NOTIF_SOCK"
	else
		echo Warning: glite-lb-notif-interlogd not installed, LB notifications will not work
	fi

	if test x"$GLITE_LB_EXPORT_ENABLED" = x"true" ; then
		start_daemon glite-jp-importer "$jp_importer_pidfile" "\
		LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GLITE_JP_LOCATION/lib \
		$GLITE_JP_LOCATION/bin/glite-jp-importer \
			-i $jp_importer_pidfile $jpreg_maildir $jpdump_maildir $sandbox_maildir \
			$jpps $creds $GLITE_JP_IMPORTER_ARGS"
	fi

	if test x"$GLITE_LB_TYPE" = x"proxy" -o x"$GLITE_LB_TYPE" = x"both" ; then
		if test -x $GLITE_LB_LOCATION/bin/glite-lb-interlogd; then
			start_daemon "glite-lb-interlog for proxy" "$PROXY_IL_PIDFILE" "$GLITE_LB_LOCATION/bin/glite-lb-interlogd \
				-f $GLITE_LB_PROXY_FPREFIX -s $GLITE_LB_PROXY_SOCK \
				-i $PROXY_IL_PIDFILE \
				$creds $GLITE_LB_PROXY_IL_OTHER_OPTIONS" "$GLITE_LB_PROXY_SOCK"
		else
			echo Warning: glite-lb-interlogd not installed, logging to LB proxy will not work
		fi
	fi
}

stop()
{
	stop_daemon "glite-lb-notif-interlogd" $NOTIF_IL_PIDFILE

	stop_daemon "glite-lb-bkserverd" $BK_PIDFILE
	stop_daemon "glite-jp-importer" $jp_importer_pidfile

	stop_daemon "glite-lb-interlogd" $PROXY_IL_PIDFILE

	# for L&B <= 2.0
	LC_ALL=C
	if netstat -an --unix | grep "^unix .* LISTEN.* $GLITE_LB_NOTIF_SOCK" >/dev/null 2>&1 ;then
		killall -9 glite-lb-notif-interlogd
	fi
	if netstat -an --unix | grep "^unix .* LISTEN.* $GLITE_LB_PROXY_SOCK" >/dev/null 2>&1 ;then
		killall -9 glite-lb-interlogd
	fi
}

status()
{
	retval=0
	proxy=0
	jp=0
	test x"$GLITE_LB_TYPE" = x"proxy" -o x"$GLITE_LB_TYPE" = x"both" \
		&& proxy=1
	test x"$GLITE_LB_EXPORT_ENABLED" = x"true" && jp=1

	status_daemon "glite-lb-notif-interlogd" "$NOTIF_IL_PIDFILE" \
		|| retval=1
	status_daemon "glite-lb-interlogd for proxy" "$PROXY_IL_PIDFILE" \
		$proxy || retval=1
	status_daemon "glite-lb-bkserverd" "$BK_PIDFILE" \
		|| retval=1
	status_daemon "glite-jp-importer" "$jp_importer_pidfile" $jp \
		|| retval=1

	return $retval
}

case x$1 in
	xstart)	start;;
	xstop)	stop;;
	xrestart|xforce-reload) stop; start;;
	xstatus) status;;
	xcondrestart|xtry-restart)
		status >/dev/null 2>&1 || exit 0
		stop; start
		;;
	x*)	echo usage: $0 start,stop,restart,status >&2
		exit 1;;
esac
