#!/bin/sh
### BEGIN INIT INFO
# Provides: glite-lb-locallogger
# Description: L&B local logger and interlogger deamons
# Short-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: - 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_LOCATION=${GLITE_LOCATION:-'@glite_prefix@'}
GLITE_LOCATION_ETC=${GLITE_LOCATION_ETC:-'@glite_etc@'}
GLITE_USER=${GLITE_USER:-'glite'}
GLITE_HOME=`getent passwd ${GLITE_USER} | cut -d: -f6`
GLITE_HOST_CERT=${GLITE_HOST_CERT:-"$GLITE_HOME/.certs/hostcert.pem"}
GLITE_HOST_KEY=${GLITE_HOST_KEY:-"$GLITE_HOME/.certs/hostkey.pem"}

KRB5_KTNAME="FILE:$GLITE_HOME/krb5kt_lb"
KRB5CCNAME="FILE:$GLITE_HOME/krb5cc_lb"
LOG4C_RCPATH="$GLITE_LOCATION_ETC/glite-lb"

[ -f /etc/profile.d/grid-env.sh ] && . /etc/profile.d/grid-env.sh
[ -f /etc/glite.conf ] && . /etc/glite.conf
[ -f $GLITE_LOCATION_ETC/glite-wms.conf ] && . $GLITE_LOCATION_ETC/glite-wms.conf
[ -f @defaultdir@/glite-lb ] && . @defaultdir@/glite-lb
[ -f $HOME/.glite.conf ] && . $HOME/.glite.conf

LL_PIDFILE=/var/run/glite/glite-lb-logd.pid
IL_PIDFILE=/var/run/glite/glite-lb-interlogd.pid
IL_SOCKFILE=/var/run/glite/glite-lb-interlogger.sock

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

unset creds port env

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 "$env $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 [ -n "$LOG4C_RCPATH" ]; then env="$env LOG4C_RCPATH='$LOG4C_RCPATH'"; fi
	if [ -n "$KRB5_KTNAME" ]; then env="$env KRB5_KTNAME='$KRB5_KTNAME'"; fi
	if [ -n "$KRB5CCNAME" ]; then env="$env KRB5CCNAME='$KRB5CCNAME'"; fi

	if [ -n "$GLITE_HOST_CERT" -a -n "$GLITE_HOST_KEY" ]; then
		env="$env X509_USER_CERT=$GLITE_HOST_CERT X509_USER_KEY=$GLITE_HOST_KEY"
		creds=1
	fi

	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"
			env="$env X509_USER_CERT=$GLITE_HOST_CERT X509_USER_KEY=$GLITE_HOST_KEY"
			creds=1
		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"

        (cd /tmp && ls -f /tmp |grep ^dglogd_sock_ |xargs rm -f)
	start_daemon "glite-lb-logd" "$LL_PIDFILE" "$GLITE_LOCATION/bin/glite-lb-logd \
		-i $LL_PIDFILE $port $sock $fprefix"

	start_daemon "glite-lb-interlogd" "$IL_PIDFILE" "$GLITE_LOCATION/bin/glite-lb-interlogd \
		-i $IL_PIDFILE $sock $fprefix" "${GLITE_LB_IL_SOCK:-$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" "$IL_PIDFILE" || retval=1

	return $retval
}

# upgrade from L&B <= 4.0.1
if [ -f /var/glite/glite-lb-logd.pid -a ! -f $LL_PIDFILE ]; then mv /var/glite/glite-lb-logd.pid /var/run/glite 2>/dev/null; fi
if [ -f /var/glite/glite-lb-interlogd.pid -a ! -f $IL_PIDFILE ]; then mv /var/glite/glite-lb-interlogd.pid /var/run/glite 2>/dev/null; fi

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