#!/bin/sh
#
# besrelay:    Starts the IBM Tivoli Endpoint Manager Relay
#
# chkconfig: 2345 99 99
# description: Starts and stops the IBM Tivoli Endpoint Manager Relay daemon
# processname: BESRelay

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

prog=BESRelay

test -x /opt/BESRelay/bin/$prog || exit 0

start() {
	SKIP_CLIENT_RESTART="noskip$1"

	echo -n $"Starting $prog: "
	export BESClientConfigPath=/var/opt/BESClient/besclient.config
	export BESClientActionMastheadPath=/etc/opt/BESClient/actionsite.afxm
	export BESRelayConfigPath=/var/opt/BESRelay/besrelay.config
	# export BES_LIBPATH=/opt/BESClient/bin
	if [ ! -f $BESClientConfigPath ]; then
		if [ ! -f $BESClientConfigPath.default ]; then
			failure "Missing config file $BESClientConfigPath."
			echo
			return 1
		fi
	fi

	GATHERURL_CONFIG=""
	if [ ! -f $BESRelayConfigPath ]; then
		if [ ! -f $BESRelayConfigPath.default ]; then
			failure "Missing config file $BESRelayConfigPath."
			echo
			return 1
		fi
	else
		GATHERURL_CONFIG=$( perl -ne 'if( m#^__Installer_Masthead_GatherURL\s+=\s+(http://\S+)# ) { print $1; }' $BESRelayConfigPath)
	fi

	CLIENT_RESTART_NEEDED=0
	GATHERURL_MASTHEAD=$( perl -ne 'if( m#^X-Fixlet-Site-Gather-URL:\s+(http://\S+)# ) { print $1; }' $BESClientActionMastheadPath )

	if [ "x$GATHERURL_MASTHEAD" != "x$GATHERURL_CONFIG" ]; then
		# This touch is necesssary to make sure the registration list exists
		touch /var/opt/BESRelay/ClientRegisterData/registrationlist.txt

		cat $BESRelayConfigPath.default > $BESRelayConfigPath
		setup_configfile $BESClientActionMastheadPath $BESRelayConfigPath $GATHERURL_MASTHEAD

		CLIENT_RESTART_NEEDED=1
	fi

	daemon /opt/BESRelay/bin/$prog 2> /dev/null
	ret=$?
	[ $ret -eq 0 ] && touch /var/lock/subsys/$prog
	echo

	if [[ $CLIENT_RESTART_NEEDED -eq 1 && $SKIP_CLIENT_RESTART == "noskip" ]]; then
		# Backwards compatibility: detect name of installed client init script
		CLIENT_INIT=/etc/init.d/besclient
		if [ ! -f $CLIENT_INIT ]; then
			CLIENT_INIT=`ls -d /etc/init.d/BESClient* | head -1`
		fi

		$CLIENT_INIT stop
		$CLIENT_INIT start

	fi

	return $ret
}

stop() {
	echo -n $"Shutting down $prog: "
	killproc BESRelay
	ret=$?
	[ $ret -eq 0 ] && rm -f /var/lock/subsys/$prog
	echo
	return $ret
}

restart() {
	stop
	start
}

reload() {
	restart
}

status_BESRelay() {
	status /opt/BESRelay/bin/$prog
}


setup_configfile() {
	masthead=$1
	configfile=$2
	gatherurl=$3

	echo >> $configfile

	echo "[Software\\BigFix\\Enterprise Server]" >> $configfile
	echo -e "__Installer_Masthead_GatherURL\t\t= $gatherurl\n" >> $configfile


	URL=$( perl -ne 'if( m#^X-Fixlet-Site-Registration-URL:\s+([[:print:]]+)# ) { print $1; }' $masthead )
	if [ -n "$URL" ]; then
		echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_Enterprise Server_ClientRegister_ParentRelayURL]" >> $configfile
		echo -e "value\t\t= $URL\n" >> $configfile
	fi

	URL=$(  perl -ne 'if( m#^X-Fixlet-Site-Report-URL:\s+([[:print:]]+)# ) { print $1; }' $masthead )
	PORT=$( perl -ne 'if( m#^X-Fixlet-Site-Report-URL:\s+http://\S+:(\d+)# ) { print $1; }' $masthead )
	if [ -n "$URL" ]; then
		echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_BESRelay_PostResults_ParentRelayURL]" >> $configfile
		echo -e "value\t\t= $URL\n" >> $configfile
		echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_BESRelay_HTTPServer_PortNumber]" >> $configfile
		echo -e "value\t\t= $PORT\n" >> $configfile
	fi


	URL=$(  perl -ne 'if( m#^X-Fixlet-Site-Gather-URL:\s+(http://\S+:\d+)# ) { print $1; }' $masthead )
	PORT=$( perl -ne 'if( m#^X-Fixlet-Site-Gather-URL:\s+http://\S+:(\d+)# ) { print $1; }' $masthead )
	if [ -n "$URL" -a -n "$PORT" ]; then
		echo "[Software\\BigFix\\Enterprise Server\\GatherService]" >> $configfile
		echo -e "HttpContentLocation\t\t= $URL/bfmirror/bfsites\n" >> $configfile

		echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_Enterprise Server_ClientRegister_UDPMessagePort]" >> $configfile
		echo -e "value\t\t= $PORT\n" >> $configfile
	fi


	URL=$( perl -ne 'if( m#^X-BES-Mirror-Gather-URL:\s+([[:print:]]+)# ) { print $1; }' $masthead )
	if [ -n "$URL" ]; then
		echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_BESGather_Comm_ParentRelayURL]" >> $configfile
		echo -e "value\t\t= $URL\n" >> $configfile
	fi

	URL=$( perl -ne 'if( m#^X-BES-Mirror-Download-URL:\s+([[:print:]]+)# ) { print $1; }' $masthead )
	if [ -n "$URL" ]; then
		echo "[Software\\BigFix\\Enterprise Server\\GatherService]" >> $configfile
		echo -e "DownloadMirror URL\t\t= $URL\n" >> $configfile
	fi
}


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

exit $?
