#!/bin/bash
CONF=/etc/config/qpkg.conf
QPKG_NAME="ISB"
Install_Path=$(/sbin/getcfg $QPKG_NAME Install_Path -f $CONF 2>/dev/null)

base=/usr/local/TSPBUS400
SUDO_USR=admin
if test -d /mnt/md1/bus400; then
	#IcyBox
	base=/mnt/md1/bus400
elif test -n "${Install_Path}" -a -d "${Install_Path}"; then
	base=${Install_Path}
else
	SUDO_USR=bus4i
fi
#echo $base
lsofFound=0
if test -f /usr/sbin/lsof; then
	lsofFound=1
elif test -f /opt/sbin/lsof; then
	lsofFound=1
elif test -f /usr/bin/lsof; then
        lsofFound=1
fi
SUDO_CMD=sudo
if test -f /usr/bin/sudo; then
	SUDO_CMD=/usr/bin/sudo #ab QNAP Firmware 4.3.3
elif test -f /opt/bin/sudo; then
	SUDO_CMD=/opt/bin/sudo # Optware IPKG (nicht unter 64-Bit)
fi
isbs=`ls $base`
isrunningAll=1
isrunningSome=0
#echo "              Port Sicherung     Port Ruecksicherung"
echo "              Save Port          Restore Port"

for isb in $isbs; do
	if test -d $base/$isb -a $isb != "tmp" -a $isb != "bin" -a ${isb:0:1} != "@"; then
		tcpport=$(cat $base/$isb/TCPPORT.CFG 2> /dev/null)
		rstport=$(cat $base/$isb/RSTPORT.CFG 2> /dev/null)
		tcppid=$(cat /tmp/isb_${isb}_tgt73c.pid 2> /dev/null)
		rstpid=$(cat /tmp/isb_${isb}_tgt74c.pid 2> /dev/null)
		tcprun=stopped
		rstrun=stopped
		if test -f $base/$isb/IGNORE; then
			rstrun=ignored
			tcprun=ignored
		fi
		if test $tcppid; then
			if test $lsofFound -ne 1; then
				if test -n "$(ps ax | grep "^ *$tcppid " | grep $isb | grep tgt73c)"; then
					tcprun=running
					isrunningSome=1
				elif test -n "$(ps ax | grep "^ *$tcppid ")"; then
					tcprun="???"
					isrunningSome=1; isrunningAll=0;
				else
					isrunningAll=0
				fi
			else
				case "$($SUDO_CMD -u $SUDO_USR $base/bin/isb.sh testConn $tcpport $tcppid)" in 
					LISTEN)			tcprun="running  "; isrunningSome=1;;
					ESTABLISHED)	tcprun="connected"; isrunningSome=1;;
					STOPPED)		tcprun="stopped  "; isrunningAll=0;;
					*)				tcprun="???      "; isrunningSome=1; isrunningAll=0;;
				esac
			fi
		elif test $tcprun != ignored; then
			isrunningAll=0
		fi
		if test $rstpid; then
			if test $lsofFound -ne 1; then
				if test -n "$(ps ax | grep "^ *$rstpid " | grep $isb | grep tgt74c)"; then
					rstrun=running
					isrunningSome=1
				elif test -n "$(ps ax | grep "^ *$rstpid ")"; then
					rstrun="???"
					isrunningSome=1; isrunningAll=0;
				else
					isrunningAll=0
				fi
			else
				case "$($SUDO_CMD -u $SUDO_USR $base/bin/isb.sh testConn $rstport $rstpid)" in 
					LISTEN)			rstrun="running  "; isrunningSome=1;;
					ESTABLISHED)	rstrun="connected"; isrunningSome=1;;
					STOPPED)		rstrun="stopped  "; isrunningAll=0;;
					*)				rstrun="???      "; isrunningSome=1; isrunningAll=0;;
				esac
			fi
		elif test $rstrun != ignored; then
			isrunningAll=0
		fi
		echo "$isb    $tcpport $tcprun     $rstport $rstrun"
	fi
done
locked="";
if test -e /tmp/isbLock; then
	locked="(locked!)";
fi;
if test $isrunningAll -eq 1; then
	echo ISB is started $locked;
	exit 0;
elif test $isrunningSome -eq 0; then
	echo ISB is stopped $locked;
	exit 1;	
else
	echo ISB is not completely started  $locked;
	exit 1;
fi;
