#!/bin/sh
#
# BESRelay:    Starts the IBM Endpoint Manager Relay
#

### BEGIN INIT INFO
# Provides: BESRelay
# Required-Start: $remote_fs $syslog
# Required-Stop:  $remote_fs $syslog
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Description:    Start BESRelay daemon
### END INIT INFO
# Source function library.


# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
. /etc/rc.status

prog=BESRelay

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

start() {
	SKIP_CLIENT_RESTART="noskip$1"

	/bin/echo -n $"Starting IBM Endpoint Manager: $prog: "
	export BESClientActionMastheadPath=/etc/opt/BESClient/actionsite.afxm
	export BES_LIBPATH=/opt/BESRelay/bin

	clientConfigPath=/var/opt/BESClient/besclient.config
	if [ ! -f $clientConfigPath ]; then
		if [ ! -f $clientConfigPath.default ]; then
			failure "Missing config file $clientConfigPath."
			/bin/echo
			return 1
		fi
	fi

	GATHERURL_CONFIG=""
	relayConfigPath=/var/opt/BESRelay/besrelay.config
	if [ ! -f $relayConfigPath ]; then
		if [ ! -f $relayConfigPath.default ]; then
			failure "Missing config file $relayConfigPath."
			/bin/echo
			return 1
		fi
	else
		GATHERURL_CONFIG=$( perl -ne 'if( m#^__Installer_Masthead_GatherURL\s+=\s+(http://\S+)# ) { print $1; }' $relayConfigPath)
	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
		cat $relayConfigPath.default > $relayConfigPath
		setup_configfile $BESClientActionMastheadPath $relayConfigPath $GATHERURL_MASTHEAD

		CLIENT_RESTART_NEEDED=1
	fi

	startproc /opt/BESRelay/bin/$prog 2> /dev/null
	rc_status -v
	#[ $ret -eq 0 ] && touch /var/lock/subsys/$prog
	/bin/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
		( export BES_LIBPATH=/opt/BESClient/bin; $CLIENT_INIT restart )

	fi

	
}

stop() {
	/bin/echo -n $"Stopping IBM Endpoint Manager: $prog: "
	killproc -t120 -TERM BESRelay
	ret=$?
	#[ $ret -eq 0 ] && rm -f /var/lock/subsys/$prog
	
	rc_status -v
}

restart() {
    $0 stop
	$0 start

	# Remember status and be quiet
	rc_status
}

reload() {
	restart
}

status_BESRelay() {
    /bin/echo -n "Checking for service BESRelay: "
	checkproc /opt/BESRelay/bin/$prog
    rc_status -v
}


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

	/bin/echo >> $configfile

	/bin/echo "[Software\\BigFix\\Enterprise Server]" >> $configfile
	/bin/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
		/bin/echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_Enterprise Server_ClientRegister_ParentRelayURL]" >> $configfile
		/bin/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
		/bin/echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_BESRelay_PostResults_ParentRelayURL]" >> $configfile
		/bin/echo -e "value\t\t= $URL\n" >> $configfile
		/bin/echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_BESRelay_HTTPServer_PortNumber]" >> $configfile
		/bin/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
		/bin/echo "[Software\\BigFix\\Enterprise Server\\GatherService]" >> $configfile
		/bin/echo -e "HttpContentLocation\t\t= $URL/bfmirror/bfsites\n" >> $configfile

		/bin/echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_Enterprise Server_ClientRegister_UDPMessagePort]" >> $configfile
		/bin/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
		/bin/echo "[Software\\BigFix\\EnterpriseClient\\Settings\\Client\\_BESGather_Comm_ParentRelayURL]" >> $configfile
		/bin/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
		/bin/echo "[Software\\BigFix\\Enterprise Server\\GatherService]" >> $configfile
		/bin/echo -e "DownloadMirror URL\t\t= $URL\n" >> $configfile
	fi
}


case "$1" in
	start)
		start
		;;
	start_skipclientrestart)
		start "skipclientrestart"
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	status)
		status_BESRelay
		;;
	*)
		/bin/echo $"Usage: besrelay {start|stop|status|restart|reload}"
		exit 1
esac

exit $?
