#!/bin/bash
#
# Startup script for the QCG comp service
#
# chkconfig: 2345 99 01
# description: This script starts your QCG comp service
# processname: qcg-compd
# pidfile: /var/run/qcg/qcg-comp/qcg-compd.pid

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

# Source qcg-compd configuration.
while read line           
do
    if [[ $line =~ ^[^=\#\ ]+=[^=\#\ ]+ ]]
    then
        eval export $line
    elif [[ $line =~ ^export[\ \t]+[^=\#\ ]+=[^=\#\ ]+ ]]
    then
        eval $line
    fi
done < /etc/sysconfig/qcg-compd

compd=/usr/sbin/qcg-compd
[ -f $compd ] || exit 1

RETVAL=0

INIT_WAIT=${INIT_WAIT:-3}

for qcg_comp_var in SGE_ROOT; do
	if [ -n "$qcg_comp_var" ]; then
		export $qcg_comp_var
	fi
done

start() {
	status qcg-compd >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		echo "qcg-compd is already running."
		exit 0
	fi

	# Source networking configuration.
	. /etc/sysconfig/network
	
	# Check that networking is up.
	[ ${NETWORKING} = "no" ] && exit 1
	
	if [  `cat /proc/uptime | cut -f 1 -d '.'` -lt 3600 ]; then
		echo "Detected cold start. Waiting for 20 s." 
		sleep 20
	fi
	    
	echo -n "Starting qcg-compd: "
	$compd >> /var/log/qcg/qcg-comp/qcg-compd.log 2>&1
	RETVAL=$?
	for i in `seq $INIT_WAIT`; do
		if [ $RETVAL -ne 0 ]; then
			break
		fi
		sleep 1
		/sbin/pidof $compd > /dev/null 2>&1
		RETVAL=$?
	done
	
	if [ $RETVAL -eq 0 ]; then
		touch /var/lock/subsys/qcg-compd
		ps -AF | grep $compd | grep -v "QCG Computing" | grep -v grep | awk '{ print $2 }' > /var/run/qcg/qcg-comp/qcg-compd.pid
		
		# Code for setting the service address for BDII 
		URLS=$(for ((i=1;;++i)) ; do

			LINT_OUTPUT=`/opt/qcg/dependencies/bin/xmllint --shell /etc/qcg/qcg-comp/qcg-compd.xml <<-EOF 2>/dev/null
				setns sm=http://schemas.qoscosgrid.org/core/2011/04/config 
				setns xsi=http://www.w3.org/2001/XMLSchema-instance
				cd //sm:Transport/sm:Module[$i]/sm:Authentication/sm:Module[@xsi:type='sm:atc_transport_gsi.service']/ancestor::sm:Module
				cat sm:Host
				cat sm:Port
			EOF
			`	

			VALID=`echo $LINT_OUTPUT | awk '/Host/ {print "TRUE"}'`	

			if [[ -n $VALID ]]; then
				for o in $(echo $LINT_OUTPUT) ; do
					echo $o | awk -F "[><]" '/Host/{printf("httpg://%s:", $3)}'
					echo $o | awk -F "[><]" '/Port/{printf("%s\n", $3)}'
				done
			else
				break 
			fi
		done)

		for URL in $URLS; do		
			echo $URL > /var/run/qcg/qcg-comp/qcg-compd.addr
		
			# ASSUMPTION: Only the first interface is reported to the file
			break  
		done
				
		success "qcg-compd startup"
	else
		failure "qcg-compd startup"
		tail -n 20 /var/log/qcg/qcg-comp/qcg-compd.log
	fi
	echo
}

stop() {
	rm /var/run/qcg/qcg-comp/qcg-compd.pid
	
    status qcg-compd >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "qcg-compd is not running."
        exit 0
    fi
    
	if test "x`pidof qcg-compd`" != x; then
		echo -n "Shutting down qcg-compd: "
		killproc $compd
	fi
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/qcg-compd /var/run/qcg/qcg-comp/qcg-compd.pid /var/run/qcg/qcg-comp/qcg-compd.addr
}


# See how we were called.
case "$1" in
  start)
	start
	;;
  stop-all)
	stop
	;;
  stop)
	stop
	;;
  status)
	status qcg-compd
	RETVAL=$?
	;;
  reload)
	stop
	start
	;;
  restart-all)
	stop
	start
	;;
  restart)
	stop
	start
	;;
  condrestart)
	if test "x`/sbin/pidof qcg-compd`" != x; then
		stop
		start
    fi
    ;;
  *)
  	echo "Usage: $0 {start|stop|restart|condrestart|status}"
  	exit 1
esac

exit $RETVAL

