#!/bin/bash

LOG_DIR=/var/log

DUMPDROP=${LOG_DIR}
CONF=/usr/conf
ISGW=/usr/local/bin/isgw
ARGS="-e $CONF -f${LOG_DIR}/isgw.log.1,${LOG_DIR}/isgw.log.2 -z500000"
GENERATE_DEBUG=1

export PATH=$PATH:/bin:/sbin/:/usr/bin:/usr/sbin

#
# Let's  dump core
#
ulimit -c unlimited
echo 'core%' > /proc/sys/kernel/core_pattern

# Add NTPTP infos to $ARGS
NTPTP=""
oldifs=$IFS
IFS=$'\n'
for i in $(/usr/local/bin/beroconf query ari "\"select port from isdn where ntteSW=\\\"nt\\\" and ptp=1 \"")
do
	val=$(echo $i | sed -n 's/^[^0-9]*\([0-9]*\)[^0-9]*$/\1/p')
	NTPTP="${NTPTP:+"${NTPTP},"}$val"
done
if [ -n "$NTPTP" ]
then
	ARGS="$ARGS -P$NTPTP"
fi
IFS=$oldifs

safe_debug_data()
{
	#disabe generate_debug until we are finished with packing
	GENERATE_DEBUG=0
	if [ -f ${LOG_DIR}/core ]; then
# 		mv ${LOG_DIR}/core ${DUMPDROP}/core.`hostname`-`date -Iseconds`
		#make a hard link, so core file is protected from being overwritten with a new core file
		ln ${LOG_DIR}/core ${LOG_DIR}/corelink
	fi

	if [ ! -f ${LOG_DIR}/debug-info.tar.gz ] ; then
		/usr/local/sbin/generate_info.sh ${LOG_DIR}/info.txt
		sync
		tar czf ${LOG_DIR}/debug-info.tar.gz /usr/conf ${LOG_DIR}/isgw.log.* ${LOG_DIR}/info.txt ${LOG_DIR}/isgw.err ${LOG_DIR}/sofia.log ${LOG_DIR}/debug.txt /usr/local/repo_info.txt
		rm ${LOG_DIR}/info.txt
	fi
# 	rm ${LOG_DIR}/core*
	GENERATE_DEBUG=1
}


check_and_cut_file()
{
    FILENAME=$1
    FILESIZE=$2

    TMPSIZE=$(ls -l $FILENAME)
    ISGWERRSIZE=$(echo $TMPSIZE | cut -d " " -f 5)

    if [ $ISGWERRSIZE -ge $FILESIZE ]  ; then
        cp $FILENAME $FILENAME.1
        sync
        echo > $FILENAME
    fi
}

generate_debug()
{
	if [ $GENERATE_DEBUG == 1 ]
	then
		rm -rf ${LOG_DIR}/debug.txt
		/usr/local/sbin/generate_info.sh ${LOG_DIR}/debug.txt
	fi
}

run_tasks()
{
    while :; do
        sleep 120;
        check_and_cut_file ${LOG_DIR}/isgw.err 10000
        generate_debug
    done
}

run_isgw()
{
	run_tasks &
	
	while :; do

		cd ${LOG_DIR}

		echo "=========================================" >> ${LOG_DIR}/isgw.err
		date >> ${LOG_DIR}/isgw.err
		/sbin/ifconfig eth1 up

		safeguard_enabled=0
		safeguard_enabled_db=`beroconf get root safeguard`
		if [ x$safeguard_enabled_db == x1 ];
		then
			safeguard_enabled=1
		fi
		
		echo 300 > /sys/class/berofix/safeguard_enabled
		echo  $safeguard_enabled > /sys/class/berofix/safeguard_enabled
		
		echo Starting ISGW \(`cat /usr/local/FILENAME`\) >> ${LOG_DIR}/isgw.err
		/usr/bin/time $ISGW $ARGS > /dev/null 2>> ${LOG_DIR}/isgw.err
		EXITSTATUS=$?
		
		date >> ${LOG_DIR}/isgw.err
		echo "ISGW ended with exit status $EXITSTATUS" >> ${LOG_DIR}/isgw.err

		echo "ISGW ended with exit status $EXITSTATUS"
		if [ "$EXITSTATUS" = "0" ]; then
			# Properly shutdown....
			echo "ISGW shutdown normally."
			#disable safeguard 
			echo 0 > /sys/class/berofix/safeguard_enabled
			killall -9 safe_isgw
			exit 0
		elif [ $EXITSTATUS = "1" ]; then
			echo "Waiting 20 second until next start" >> ${LOG_DIR}/isgw.err
			sleep 20
		elif [ $EXITSTATUS -gt 128 ]; then
			let EXITSIGNAL=EXITSTATUS-128
			echo "ISGW exited on signal $EXITSIGNAL."
			safe_debug_data
		else
			echo "ISGW died with code $EXITSTATUS."
			safe_debug_data
		fi
		
		#disable safeguard
		echo 0 > /sys/class/berofix/safeguard_enabled
		
		#uptime check
		ut=`cat /proc/uptime |sed 's/\..*//'`
		echo uptime $ut -- exit $EXITSTATUS -- mspcheck $safeguard_enabled >> ${LOG_DIR}/isgw.err
		
		#additional safeguard
		if [ x$safeguard_enabled = x1 -a $ut -gt 300 ];
		then
			isgw -e $CONF -A
			ret=$?
			if [ $ret = 116 -o $ret = 110 ]
			then
				echo mspcheck '=>' rebooting isgw because of exit cause $ret on msp test >> ${LOG_DIR}/isgw.err
				reboot
			else
				echo mspcheck '=>' not rebooting isgw: exit cause $ret on msp test >> ${LOG_DIR}/isgw.err
			fi
		fi
		
		echo "Automatically restarting ISGW."
		sleep 4
	done
}

run_isgw &
