#!/bin/sh

#  Copyright (c) Members of the EGEE Collaboration. 2007-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.
#


# chkconfig: 345 94 15

# Source function library
. /etc/rc.d/init.d/functions

if [ "x${BLAHPD_LOCATION}" != "x" -a -d "${BLAHPD_LOCATION}/bin" ]; then
  blparser_bin_directory="${BLAHPD_LOCATION}/bin"
else
  if [ "x${GLITE_LOCATION}" != "x" -a -d "${GLITE_LOCATION}/bin" ]; then
    blparser_bin_directory="${GLITE_LOCATION}/bin"
  else
#   Default when everything else fails.
    blparser_bin_directory="/usr/bin"
  fi
fi

if [ ! -r "$BLPARSER_CONFIG_LOCATION" ]; then
  if [ -r "/etc/blparser.conf" ]; then
    export BLPARSER_CONFIG_LOCATION="/etc/blparser.conf"
  elif [ -r "${GLITE_LOCATION:-/opt/glite}/etc/blparser.conf" ]; then
    export BLPARSER_CONFIG_LOCATION="${GLITE_LOCATION:-/opt/glite}/etc/blparser.conf"
  fi
fi

if [ ! -r "$BLPARSER_CONFIG_LOCATION" ]; then
  echo "$0: Cannot read BLParser config file (\$BLPARSER_CONFIG_LOCATION, or /etc/blparser.conf, or /opt/glite/etc/blparser.conf)"
  exit 1
fi

. $BLPARSER_CONFIG_LOCATION

GLITE_CE_BLPARSERPBS=${blparser_bin_directory}/BLParserPBS
GLITE_CE_BLPARSERLSF=${blparser_bin_directory}/BLParserLSF

[ "x$GLITE_CE_USE_BLPARSERPBS" == "xyes" ] && LRMS1=PBS
[ "x$GLITE_CE_USE_BLPARSERLSF" == "xyes" ] && LRMS2=LSF

        exe=""
        daemon=""
        debuglevel=""
        debuglogfile=""
        binpath=""
        confpath=""
        spooldir=""
        parsernum=""
	
        for lrms in $LRMS1 $LRMS2;do

	      eval parsernum=\$`echo GLITE_CE_BLPARSER${lrms}_NUM`
              eval exe=\$`echo GLITE_CE_BLPARSER${lrms}`

	      
              i=1
	      while (( i <= $parsernum ));do

             	   portval=`echo GLITE_CE_BLPARSER${lrms}_PORT${i}`
             	   eval portnum=\$$portval
		 
                   isup=""
		   isup=`ps -edfaw|grep $exe|grep $portnum`
	     	   if [ "x$isup" == "x" ]; then
                         eval daemon=\$`echo GLITE_CE_BLPARSER${lrms}_DAEMON`
                         eval debuglevel=\$`echo GLITE_CE_BLPARSER${lrms}_DEBUGLEVEL`
                         eval debuglogfile=\$`echo GLITE_CE_BLPARSER${lrms}_DEBUGLOGFILE`
                         eval binpath=\$`echo GLITE_CE_BLPARSER${lrms}_BINPATH`
                         eval confpath=\$`echo GLITE_CE_BLPARSER${lrms}_CONFPATH`
                         eval spooldir=\$`echo GLITE_CE_BLPARSER${lrms}_SPOOLDIR`
                         portval=`echo GLITE_CE_BLPARSER${lrms}_PORT${i}`
                         eval portnum=\$$portval
                         creamportval=`echo GLITE_CE_BLPARSER${lrms}_CREAMPORT${i}`
                         eval creamportnum=\$$creamportval

                         [ "x$daemon" == "xyes" ] && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -D`
                         [ -n "$debuglevel" ]  && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -d $debuglevel`
                         [ -n "$debuglogfile" ]  && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -l $debuglogfile`
                         [ -n "$binpath" ]  && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -b $binpath`
                         [ -n "$confpath" ]  && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -c $confpath`
                         [ -n "$spooldir" ]  && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -s $spooldir`
                         [ ! -n "$parsernum" ] && parsernum=1
  
                         [ -n "$portnum" ] && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -p $portnum`
                         [ -n "$creamportnum" ] && GLITE_CE_BLPARSER_CMDLINE=`echo $GLITE_CE_BLPARSER_CMDLINE -m $creamportnum`
                  	
                         rdate=`date` 
                         echo "$rdate: Restarting $exe $GLITE_CE_BLPARSER_CMDLINE"
                         $exe $GLITE_CE_BLPARSER_CMDLINE
			
             	   fi

	     	   let i=$i+1
	     	  
              done
        done
	      
