#!/bin/sh
### BEGIN INIT INFO
# Provides: glite-lb-harvester
# Description: L&B harvester deamon
# Short-Description: L&B harvester deamon
# 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: - 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_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"}

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

pidfile=${GLITE_LB_HARVESTER_PIDFILE:-'/var/run/glite/glite-lb-harvester.pid'}
level=${GLITE_LB_HARVESTER_DEBUG_LEVEL:-"1"}
lockfile=/var/lock/glite-lb-harvester

# database or configuration file needed
# (using the file as default)
etc=$GLITE_LOCATION_ETC/glite-lb
[ -z "$GLITE_LB_HARVESTER_DBCS" -a -z "$GLITE_LB_HARVESTER_CONFIG" ] && GLITE_LB_HARVESTER_CONFIG=$etc/harvester.conf
if [ -n "$GLITE_LB_HARVESTER_CONFIG" ]; then
	[ ! -d "$etc" ] && mkdir -p "$etc"
	conf="$conf -c $GLITE_LB_HARVESTER_CONFIG"
	if [ ! -f "$GLITE_LB_HARVESTER_CONFIG" ]; then
		hostname -f > $GLITE_LB_HARVESTER_CONFIG
	fi
fi
[ -n "$GLITE_LB_HARVESTER_DBCS" ] && conf="$conf -m $GLITE_LB_HARVESTER_DBCS"

HARVESTER_NOTIFSFILE=/var/tmp/notifs.txt

unset creds port log4c

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

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

	if [ -f "$pidfile" ]; then
		if kill -0 `cat $pidfile`; 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 kill -0 $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 $pidfile
		rm -f $lockfile
	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 kill -0 $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()
{
	log4c="LOG4C_RCPATH='$GLITE_LOCATION_ETC/glite-lb'"

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

	[ -n "$GLITE_LB_HARVESTER_PORT" ] && port="-p $GLITE_LB_HARVESTER_PORT"

	start_daemon glite-lb-harvester "$pidfile" "$GLITE_LOCATION/bin/glite-lb-harvester \
		$creds $conf -i $pidfile -d $level $port --daemon\
		$GLITE_LB_HARVESTER_OTHER_OPTIONS" "$HARVESTER_NOTIFSFILE"
}

stop()
{
	stop_daemon "glite-lb-harvester" $pidfile
}

status()
{
	retval=0
	status_daemon "glite-lb-harvester" "$pidfile" || retval=1

	return $retval
}

cleanup()
{
	stop | grep -v 'glite-lb-harvester not running'
	echo -n "Cleaning up notifications ..."
	$log4c glite-lb-harvester --cleanup $creds $conf -d $level $GLITE_LB_HARVESTER_OTHER_OPTIONS
	if [ $? = 0 ]; then
		echo " done"
	else
		echo " FAILED"
		return 1
	fi
}

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

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