#/bin/sh
#set -x

if test "$FITDIR" != "" 
	then echo "FITDIR found"
	CP=${PWD}/install/ant-launcher.jar

	if test "$JAVA_HOME" != ""
		then 
			echo "JAVA found"
		else
			echo "No JAVA_HOME found, using build in version if available!"
			JAVA_HOME=${FITDIR}/run/bin/java/j2sdk1.4.2_02
	fi
	
	${JAVA_HOME}/bin/java -cp "${CP}" -DINST_OS=unix -DINST_SYS=fit -DINST_DIR=${PWD} -DDST_DIR=${FITDIR}/run/bin/FIT-MI_Server -DBASE_DIR=${FITDIR} -Dant.home=${PWD}/install org.apache.tools.ant.launch.Launcher -buildfile install/ant_update.xml update

elif test "$TCFITDIR" != ""
	then echo "TCFITDIR found"
	CP=${PWD}/install/ant-launcher.jar
	
	if test "$JAVA_HOME" != ""
		then 
			echo "JAVA found"
		else
			echo "No JAVA_HOME found, using build in version if available!"
			JAVA_HOME=${TCFITDIR}/java/j2sdk
	fi

	# Stop running service before update...
	case `uname` in
		Linux)	echo "Stopping SFC services..."
				if [ -d /etc/systemd/system ]; then
					${TCFITDIR}/etc/systemd/SFC_MTCServer.sh stop
				else
					${TCFITDIR}/etc/init.d/SFC_MTCServer.sh stop
				fi
				
				echo "SFC MTC service stopped."
				read -p "Press [Enter] to start update or [CTRL-C] to abort..."

				;;
			*)	echo "Please stop running SFC MTC service before update is executed!"
				read -p "Press [Enter] to start update or [CTRL-C] to abort..."
				;;
		
	esac

	${JAVA_HOME}/bin/java -cp "${CP}" -DINST_SYS=fit4tc -DINST_OS=unix -DINST_DIR=${PWD} -DDST_DIR=${TCFITDIR}/FIT-MI_Server -DBASE_DIR=${TCFITDIR} -Dant.home=${PWD}/install org.apache.tools.ant.launch.Launcher -buildfile install/ant_update.xml update
	
else
	echo "FITDIR nor TCFITDIR found!"

	CP=${PWD}/install/ant-launcher.jar
	
	if test "$JAVA_HOME" != ""
		then 
			echo "JAVA found"
		else
			echo "No JAVA_HOME found, using build in version if available!"
			JAVA_HOME=${TCFITDIR}/java/j2sdk
	fi
	
	${JAVA_HOME}/bin/java -cp "${CP}" "$@" -DINST_SYS=fit4tc -DINST_OS=unix -DINST_DIR=${PWD} -Dant.home=${PWD}/install org.apache.tools.ant.launch.Launcher -buildfile install/ant_update.xml doUpdateStandAlone
fi
