#!/bin/bash
#
#       /etc/rc.d/init.d/eosapmond
#
# Starts xrootd 
#
# chkconfig: 345 95 5
# description: Starts apmon to monitor xrootd on EOS
# processname: ApMon

# Source function library.
EOSLOCATION="/usr/"
EOSCONFIG="/etc/xrd.cf.fst"
EOSLOGDIR="/var/log/eos/"
EOSROLE="apmon"
EOSUSER="daemon"
. /etc/init.d/functions
test -e /etc/sysconfig/eos && . /etc/sysconfig/eos
test -e /etc/sysconfig/eos_env && . /etc/sysconfig/eos_env


mkdir -p ${EOSLOGDIR}/${EOSROLE}
chown -R $EOSUSER ${EOSLOGDIR}

prog="eos_apmond"

if test ! -x ${EOSLOCATION}/sbin/$prog ; then
  echo Error: ${EOSLOCATION}/sbin/$prog does not exist
  exit 0
fi



RETVAL=0

#
#       See how we were called.
#


host=`hostname -f`
xrdpid=`pgrep -u ${EOSUSER} xrootd | tail -1`
cmspid=`pgrep -u ${EOSUSER} cmsd | tail -1`

if [ $xrdpid="" ]; then
    xrdpid=999999
fi

if [ $cmspid="" ]; then
    cmspid=999999
fi
    
MONALISA_HOST=${MONALISAHOST:-"undef"}
APMON_DEBUG_LEVEL=${APMON_DEBUG_LEVEL:-"WARNING"}
APMON_INSTANCE_NAME=${APMON_INSTANCE_NAME:-"unconfigured"}

start() {
        # Check if eosapmond is already running
        avoid=$$;
	if [ $MONALISA_HOST = "undef" ]; then
	    echo "error: please configure the MONALISAHOST variable in /etc/sysconfig/eos first!"
	    return 1
	fi
	
        if [ ! `pgrep -f -u ${EOSUSER} $prog `  ]; then
            echo -n "Starting $prog: "
            daemon --user ${EOSUSER} "test -e /etc/sysconfig/eos && source /etc/sysconfig/eos; cd ${EOSCOREDIR}; ulimit -c unlimited; export PERL5LIB=`perl -V:installsitearch | cut -d "'" -f 2`/ApMon ;${EOSLOCATION}/sbin/$prog /usr/sbin/eos_apmonpl ${EOSLOGDIR}/${EOSROLE}/apmon.log ${MONALISA_HOST} ${APMON_DEBUG_LEVEL} ${APMON_INSTANCE_NAME} $host $xrdpid >& /dev/null < /dev/null" 
            RETVAL=$?
	    sleep 1
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
            echo
	else 
            echo $prog already running.  
            echo Stop it first with 'service $prog stop'
            echo and try again or use directly 'service $prog restart'.
            RETVAL=1
        fi
        return $RETVAL
}

stop() {
        echo -n "Stopping $prog: "
        killproc /${EOSLOCATION}/sbin/$prog
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
        echo
        return $RETVAL
}


restart() {
        stop
        start
}       

reload() {
        restart
}       

status_at() {
    pid=`pidofproc $prog`
    if [ -n "$pid" ]; then
	echo $"$prog (pid $pid) is running..."
    else
	if [ -f /var/lock/subsys/$prog ]; then
	    echo $"$prog dead but subsys locked"
	else
	    echo $"$prog is stopped"
	fi
	RETVAL=1
    fi
    return $RETVAL
}


case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload|restart)
        restart
        ;;
condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
            restart
        fi
        ;;
status)
        status_at
        ;;
*)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
esac

exit $?
exit $RETVAL
