#!/bin/bash
#***************************************************************************#
#                                                                           #
#  TRUMPF Medizinsysteme GmbH Saalfeld                                      #
#  www.trumpf-med.com                                                       #
#                                                                           #
#  File:         /home/startup                                              #
#  Description:  autostart file for ksps                                    #
#                                                                           #
#  -----------------------------------------------------------------------  #
#                                                                           #
#  Revision History:                                                        #
#                                                                           #
#  2010/08/26 schulzema1:  created this file while porting to new kr        #
#****************************************************************************

echo
echo "***  TMS KSPS running here...  ***"
echo

BINDIR=/bin
ETCDIR=/etc

#######
#     Init I2C EEPROM of base board
#######
echo eeprom 0x50 > /sys/bus/i2c/devices/i2c-2/new_device


#######
#     Handle serial number for KR
#######
TMP_EE=`/bin/TmsBrdCfgTool -r SERIAL_NUMBER`
if [[ -n $TMP_EE ]]; then
	TMP_PL=`grep -oe '^export SERIAL_NUMBER=[[:alnum:]_\-]\+' /etc/profile.local`
	if [[ -n $TMP_PL ]]; then
		# update
		[[ "$TMP_PL" != "export $TMP_EE" ]] &&  sed -i "s/$TMP_PL/$TMP_EE/" /etc/profile.local
	else
		# create
		echo "export $TMP_EE" >> /etc/profile.local
	fi
fi

#######
#     Handle product number for KR
#######
TMP_EE=`/bin/TmsBrdCfgTool -r ITEM_NUMBER`
if [[ -n $TMP_EE ]]; then
	TMP_PL=`grep -oe '^export ITEM_NUMBER=[[:alnum:]_\-]\+' /etc/profile.local`
	if [[ -n $TMP_PL ]]; then
		# update
		[[ "$TMP_PL" != "export $TMP_EE" ]] && sed -i "s/$TMP_PL/$TMP_EE/" /etc/profile.local
	else
		# create
		echo "export $TMP_EE" >> /etc/profile.local
	fi
fi

#######
#     Handle revision number for KR
#######
TMP_EE=`/bin/TmsBrdCfgTool -r REVISION_NUMBER`
if [[ -n $TMP_EE ]]; then
	TMP_PL=`grep -oe '^export REVISION_NUMBER=[[:alnum:]_\-]\+' /etc/profile.local`
	if [[ -n $TMP_PL ]]; then
		# update
		[[ "$TMP_PL" != "export $TMP_EE" ]] && sed -i "s/$TMP_PL/$TMP_EE/" /etc/profile.local 
	else
		# create
		echo "export $TMP_EE" >> /etc/profile.local
	fi
fi

#######
# export version informations
#######
. /etc/profile.local


#######
#     Install packages from /install
#     check for post installation packages and install them
#######
INSTALL_PACKAGES=`find /install/ -type f -perm -100`
for i in $INSTALL_PACKAGES;
do
  echo "install package ${i}"
  $i
  if (( $? == 0 )); then
    # delete file
    rm $i
  fi
done


#######
#     KrApp
#######
${BINDIR}/pidof KrApp > /dev/null 2>&1
if ( [ $? -ne 0 ] && [ -f ${BINDIR}/KrApp ] ); then
    echo "Starting KrApp in background..."
	${BINDIR}/respawn ${BINDIR}/KrApp -f ${ETCDIR}/KrApp.par &
fi   

#######
#     Webserver
#######
${BINDIR}/pidof webservereventhandler > /dev/null 2>&1
if ( [ $? -ne 0 ] && [ -f ${BINDIR}/webservereventhandler ] ); then
	${BINDIR}/webservereventhandler &
fi

if [ -d /httpd ]; then
  # luft keine instanz des boa wird diese gestartet
  # 1.Instanz referenziert auf /etc/boa.conf fr das Trumpf Netzwerk
  pidof boa > /dev/null || boa -c $ETCDIR/
  # 2.Instanz referenziert auf /etc/boa/boa.conf fr Siemens
  boa -f $ETCDIR/boa_siemens.conf &
fi

#######
#     Handle OP-TYPE from MR without service-mode request
#######
TSI_TYPESF="\"22\"";
TSI_TYPESM="\"23\"";
SFA_TYPE="\"18\"";
OP_TYPE=`TmsSysMsgTool -m client -f /httpd/html/cgi-bin/includes/parameters/MSGPARAM_ParamVersionsOpt.par -o /tmp/startup.txt | grep Type | awk '{print $12}'`
RESULT=$?;
#echo $OP_TYPE > /tmp/op_type.txt
#echo $TSI_TYPE > /tmp/tsi_type.txt
if [ $RESULT -eq 0 ]; then
	    #echo "b"> /tmp/result0
	if [ -n "$OP_TYPE" ]; then
		#echo "a"> /tmp/result1
		# Ist der OP-Type 22 (TSI) dann fhre die IFApplication aus
		# bei ungleich wird die TransApp ausgefhrt
		if [[ "$OP_TYPE" == "$TSI_TYPESF" || "$OP_TYPE" == "$TSI_TYPESM" ]]; then
			#######
			#     IFApplication
			#######
		    #echo "a"> /tmp/result1
			${BINDIR}/pidof IFApplication > /dev/null 2>&1
			if ( [ $? -ne 0 ] && [ -f ${BINDIR}/IFApplication ] ); then
			    echo "Starting IFApplication in background..."
			    ${BINDIR}/respawn ${BINDIR}/IFApplication &
			fi
		fi
		if [ "$OP_TYPE" == "$SFA_TYPE" ]; then
			#######
			#     TransApp
			#######
			${BINDIR}/pidof TransApp > /dev/null 2>&1
			if ( [ $? -ne 0 ] && [ -f ${BINDIR}/TransApp ] ); then
			    echo "Starting TransApp in background (connecting to 10.1.1.2)..."
			    ${BINDIR}/respawn ${BINDIR}/TransApp -f /etc/TransApp.par &
			fi
	 	fi
	fi
fi

#######
#     DEV: Bootchart
#######
PID_BC=`pidof bootchartd`
if [ -n "${PID_BC}" ]; then
        PID_COL=`pidof collector`
        if [ -n "${PID_COL}" ]; then
                sleep 10
                kill -INT ${PID_BC}
                kill -INT ${PID_COL}
        fi;
fi;
