#!/bin/sh
### BEGIN INIT INFO
# Provides: glite-lb-locallogger
# Description: L&B local logger and interlogger deamons
# 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 76 24
#
# 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_ETC=${GLITE_LB_LOCATION_ETC:-'@glite_etc@'}
GLITE_LB_LOCATION_VAR=${GLITE_LB_LOCATION_VAR:-'@glite_var@'}

[ -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

LL_PIDFILE=${LL_PIDFILE:-$GLITE_LB_LOCATION_VAR/glite-lb-logd.pid}
IL_PIDFILE=${IL_PIDFILE:-$GLITE_LB_LOCATION_VAR/glite-lb-interlogd.pid}
IL_SOCKFILE=/tmp/interlogger.sock

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

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
}

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

	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
		case "$GLITE_LB_TYPE" in
		proxy)
			echo "$name not running (disabled)"
			return 0
			;;
		*)
			echo "$name not running"
			return 1
			;;
		esac
	fi

	return 0
}

start()
{
	case "$GLITE_LB_TYPE" in
	proxy)
		echo 'Not starting logd and interlogger, proxy only instance (GLITE_LB_TYPE=proxy).'
		return 0
	esac

	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

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

	[ -n "$GLITE_LB_LOGGER_PORT" ] && port="--port $GLITE_LB_LOGGER_PORT"
	[ -n "$GLITE_LB_IL_SOCK" ] && sock="--socket $GLITE_LB_IL_SOCK"
	[ -n "$GLITE_LB_IL_FPREFIX" ] && fprefix="--file-prefix $GLITE_LB_IL_FPREFIX"

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

	mkdir -p /var/glite/log 
	chown $GLITE_USER /var/glite/log
        (cd /tmp && ls -f /tmp |grep ^dglogd_sock_ |xargs rm -f)
	start_daemon "glite-lb-logd" "$LL_PIDFILE" "$GLITE_LB_LOCATION/bin/glite-lb-logd \
		-i $LL_PIDFILE $creds $port $sock $fprefix"

	start_daemon "glite-lb-interlogd" "$IL_PIDFILE" "$GLITE_LB_LOCATION/bin/glite-lb-interlogd \
		-i $IL_PIDFILE $creds $sock $fprefix" "$IL_SOCKFILE"
}

killwait()
{
	pidfile=$1
	if [ -f $pidfile ] && pid=`cat $pidfile` && kill $pid 2>/dev/null; then
		cnt=0
		while ps p $pid 2>/dev/null >/dev/null; do
			sleep 1;
			cnt=`expr $cnt + 1`
			if [ $cnt = 120 ]; then break; fi
		done
		if [ $cnt = 100 ]; then echo " can't stop"
		else echo " done"; fi
	else
		echo " not running"
	fi
}

stop()
{
	echo -n Stopping glite-lb-logd ...
	killwait $LL_PIDFILE
	echo -n Stopping glite-lb-interlogd ...
	killwait $IL_PIDFILE

	# for L&B <= 2.0
	LC_ALL=C
	if netstat -an --inet --inet6 | grep "^tcp .* \(::\|0.0.0.0\):${GLITE_LB_LOGGER_PORT:-9002} .*LISTEN" >/dev/null 2>&1 ;then
		killall -9 glite-lb-logd
	fi
	if netstat -an --unix | grep "^unix .* LISTEN.* ${GLITE_LB_IL_SOCK:-$IL_SOCKFILE}$" >/dev/null 2>&1 ;then
		killall -9 glite-lb-interlogd
	fi

	rm -f $lockfile
}

status()
{
	retval=0

	status_daemon "glite-lb-logd" "$LL_PIDFILE" || retval=1
	status_daemon "glite-lb-interlogd" "$LL_PIDFILE" || 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
