#!/bin/sh

#########################################################################################
# Script which is a workaround for a bug found in TXA100 application and due probably
# to the ProSyst framework. It happend, but not always, when the network configuration 
# is changed from dynamic to static and vice-versa. 
#
# When the bug happend, the Prosyst HTTP server is started but doesn't respond anymore.
# The only way we found until now to prevent it is to stop and restart the framework
# at each start of the network layer.
# 
# That's the reason this script is located under /etc/network/if-up.d. 
# Its name beginning with a 'Z' normally guarantee that it will be executed last in the
# network starting sequence order.
#
# To prevent as much as possible any side effect, its execution is conditionned by the 
# kind of HBOX which must be a KNX one and by the presence of a running TXA100 application
#########################################################################################
source /usr/local/bin/update_common
export VM_HOME="/usr/bin/ejre1.7.0_45"

RESTARTFRAMEWORK_SCRIPT_VERSION="1.0.0"

#########################################################################################
# Local variables defined
#########################################################################################

MBSA_DIR="/root/app/mbsa/bin"

MBSA_RUNNING="mbsae.core"
JAVA_RUNTIME="java"

CONSOLE="/dev/console"

LOG_WARNING=1
LOG_NORMAL=2

LOGFILE_AND_CONSOLE="Console"

#########################################################################################
# Global variables 
#########################################################################################

logLevel=$LOG_NORMAL

#########################################################################################
# Fonctions
#########################################################################################
#########################################################################################
Trace() {

	# The second parameter (optional) manage the message severity level
	# If not defined, the message is always written

	if [ -z $2 ] || [ $2 -ge $logLevel ]; then
		echo $1 >> $CONSOLE
	fi
}

#########################################################################################
Is_Framework_Running() {
	
	##### Looking for mbsae.core running
	Trace "Looking for framework running : pidof $MBSA_RUNNING" $LOG_WARNING
	pidfound=$(pidof "$MBSA_RUNNING") 2>&1 >> $CONSOLE
	
	Trace "pidfound = $pidfound" $LOG_WARNING

	if [ ! -z $pidfound ]; then
		return 0
	fi

	##### Mbsae not found, looking for java runtime
	Trace "Looking for java running : pidof $JAVA_RUNTIME" $LOG_WARNING
	pidfound=$(pidof "$JAVA_RUNTIME") 2>&1 >> $CONSOLE
	
	Trace "pidfound = $pidfound" $LOG_WARNING

	if [ ! -z $pidfound ]; then
		return 0
	fi

	##### Nothing found
	return 1
}

#########################################################################################
StopFramework() {

	ActualPwd=$(pwd)

	cd $MBSA_DIR
	./mbsa_stop

	cd $ActualPwd
}

#########################################################################################
StartFramework() {

	ActualPwd=$(pwd)

	cd $MBSA_DIR
	nohup sh ./knxng_mbsa_start knxng > /tmp/mbsa.log 2>&1 &

	cd $ActualPwd
}

#########################################################################################
Is_KNX_Board() {

	board=$(grep KNX /proc/HBox-BoardType)
	
	if [ -z $board ]; then
		return 1
	else
		return 0
	fi
}

#########################################################################################
#########################################################################################
# Script beginning
#########################################################################################
#########################################################################################

if [ "$IFACE" = "br0" ]; then
		
	if Is_KNX_Board; then

		if Is_Framework_Running; then

			Trace "---------- $(basename "$0") $RESTARTFRAMEWORK_SCRIPT_VERSION started"

			SetLed POWER GREEN NORMAL_BLINK

			Trace "Stopping Hager application ..."
			StopFramework

			while Is_Framework_Running; do 

				Trace "Application still running" $LOG_WARNING
				sleep 1

			done	

			Trace "Hager application stopped"
			sleep 1

			Trace "Starting Hager application"
			StartFramework

			SetLed POWER GREEN FIXED

			Trace "---------- $(basename "$0") $RESTARTFRAMEWORK_SCRIPT_VERSION ended"

		else
			Trace "Framework not running" $LOG_WARNING
		fi
	
	else
		Trace "This is not a KNX board" $LOG_WARNING
	fi


fi
