#!/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
sysISB=Linux
if test -d /mnt/md1/bus400; then
	#IcyBox
	base=/mnt/md1/bus400
	sysISB=IcyBox
elif test -n "${Install_Path}" -a -d "${Install_Path}"; then
	base=${Install_Path}
	sysISB=QNAP
fi
#echo $base

RETVAL=0
case "$1" in
  start)
		ENABLED=$(/sbin/getcfg $QPKG_NAME Enable -u -d FALSE -f $CONF 2>/dev/null)
		if [ -f /sbin/getcfg -a "$ENABLED" != "TRUE" ]; then
			echo "$QPKG_NAME is disabled and can not start."
			exit 1
		fi
		echo "Starting ISB services $base"
		(kill `cat /tmp/isbWatchDog.pid` 2>&1) > /dev/null
		($base/bin/isbWatchDog.sh 2>&1) > $base/isbWatchDog.log &
		echo $! > /tmp/isbWatchDog.pid

		if test $sysISB == QNAP; then
			uCONF=/etc/default_config/uLinux.conf
			isbVersion=$(/sbin/getcfg $QPKG_NAME Version -f $CONF 2>/dev/null)
			model=$(/sbin/getcfg System Model -f $uCONF 2>/dev/null)
			build=$(/sbin/getcfg System "Build Number" -f $uCONF 2>/dev/null)
			version=$(/sbin/getcfg System Version -f $uCONF 2>/dev/null)
			echo -n "ISB $isbVersion" > $base/isb.version
			echo -n "QNAP $version $build $model" > $base/lin.version
		else
			#$base/lin.version vorgegeben, nicht Ã¤ndern!
			echo -n "Linux $(uname -rv)" > $base/lin.version
		fi
        ;;
  stop)
        echo "Shutting down ISB services $base"
		kill `cat /tmp/isbWatchDog.pid`
		RETVAL=$?
		$base/bin/isbStop.sh
        ;;
  enable)
        echo "Enable ISB app"
		/sbin/setcfg $QPKG_NAME Enable TRUE -f $CONF
        ;;
  unlock)
        echo "unlocking ISB services $base"
		rm -f /tmp/isbLock
		RETVAL=$?
        ;;
  lock)
        echo "locking ISB services $base"
		touch /tmp/isbLock
		RETVAL=$?
		sleep 2; #give aktive watchdog time to end his work
		$base/bin/isbStop.sh
        ;;
  restart|force-reload)
        $0 stop
        $0 start
        RETVAL=$?
        echo "stopped."
        ;;
  try-restart)
		($base/bin/isbStatus.sh 2>&1) > /dev/null
		RETVAL=$?
		if test $RETVAL -eq 0; then
			echo "Already started"
		else
			$0 restart
			RETVAL=$?
		fi
        ;;
  shortStatus)
		$base/bin/isbShortStatus.sh
		RETVAL=$?
        ;;
  status)
		df $base
		echo
		if test $sysISB == QNAP; then
			uCONF=/etc/default_config/uLinux.conf
			isbVersion=$(/sbin/getcfg $QPKG_NAME Version -f $CONF 2>/dev/null)
			model=$(/sbin/getcfg System Model -f $uCONF 2>/dev/null)
			build=$(/sbin/getcfg System "Build Number" -f $uCONF 2>/dev/null)
			version=$(/sbin/getcfg System Version -f $uCONF 2>/dev/null)
			echo "Version: ISB $isbVersion, QNAP $model $version $build"
		else
			isbVersion=$(cat $base/isb.version)
			echo "Version $isbVersion, Linux $(uname -rv)"
		fi
		$base/bin/isbFullStatus.sh
		RETVAL=$?
        ;;
  testConn)
		tcpport=$2
		tcppid=$3
		LSOF_CMD=/usr/sbin/lsof
		if test -f /usr/sbin/lsof; then
			LSOF_CMD=/usr/sbin/lsof
		elif test -f /opt/sbin/lsof; then
			LSOF_CMD=/opt/sbin/lsof
                elif test -f /usr/bin/lsof; then
                        LSOF_CMD=/usr/bin/lsof
 		fi

		result=x`$LSOF_CMD -nPRp $tcppid 2>/dev/null`x
		REGEX=".*:$tcpport->.[0-9:\.a-f]*.:[0-9]*[[:blank:]]+\(ESTABLISHED\).*" #Problem mit \] daher . nach IP6-Addesse (vor :[0-9]*...)
		if [[ $result =~ $REGEX ]]; then 
			echo -n "ESTABLISHED"
		else
			REGEX=".*:$tcpport[[:blank:]]+\(LISTEN\).*"
			if [[ $result =~ $REGEX ]]; then 
				echo -n "LISTEN"
			else
				if test "$result" == "xx"; then
					echo -n "STOPPED"
				else
					echo -n "UNKNOWN"
				fi
			fi
		fi
        ;;
  chgPort)
		add=$2
		anwendung=$3
		tcpportNeu=$4
		rstportNeu=$5
		if test $add -eq 1; then
			mkdir $base/$anwendung
			mkdir $base/$anwendung/open
			mkdir $base/$anwendung/closed
		fi
		echo -n $tcpportNeu > $base/$anwendung/TCPPORT.CFG 2> /dev/null
		echo -n $rstportNeu > $base/$anwendung/RSTPORT.CFG 2> /dev/null
        ;;
  rename)
		anwendungAlt=$2
		anwendungNeu=$3
		if test -d $base/$anwendungAlt; then
			mv $base/$anwendungAlt $base/$anwendungNeu
		fi
		$base/bin/isb.sh status
        ;;
  delete)
		if test $# -eq 3; then
			anwendung=$2
			let alter=365
			let alter=$((10#$3)) 
			if test $alter -le 0; then 
				let alter=1
				#-mtime +0 funktioniert leider nicht!
				if test $anwendung = 'ALL' -o $anwendung = 'all'; then
					echo "find $base/ -mtime -1 -name \"OSB*.BUS\" | xargs -r rm"
					find $base/ -mtime -1 -name "OSB*.BUS" | xargs -r rm
				else 
					echo "find $base/$anwendung/closed/ -mtime -1 -name \"OSB*.BUS\" | xargs -r rm"
					find $base/$anwendung/closed/ -mtime -1 -name "OSB*.BUS" | xargs -r rm
				fi
			fi
			if test $anwendung = 'ALL' -o $anwendung = 'all'; then
				echo "find $base/ -mtime +$alter -name \"OSB*.BUS\" | xargs -r rm"
				find $base/ -mtime +$alter -name "OSB*.BUS" | xargs -r rm
			else 
				echo "find $base/$anwendung/closed/ -mtime +$alter -name \"OSB*.BUS\" | xargs -r rm"
				find $base/$anwendung/closed/ -mtime +$alter -name "OSB*.BUS" | xargs -r rm
			fi
			df $base
		else 
			echo "usage: $0 delete Name|all|ALL numberDays"
		fi
        ;;
  config)
		startStop = $2			#Zukünftiger Parameter, derzeit ignoriert
		shift 2					#Parameter verschieben: $1="config" und startStop entfernen 
		$base/bin/isb.sh stop
		while [ $# -gt 2 ]		#Solange die Anzahl der Parameter ($#) größer 2
		do
			anwendung=$1
			tcpportNeu=$((10#$2)) 
			rstportNeu=$((10#$3))
			isbs=`ls $base`
			for isb in $isbs; do	#Test ob Ports schon in Verwendung
				if test -d $base/$isb -a ! -f $base/$isb/IGNORE -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)
					if test $tcpportNeu == $tcpport -o $rstportNeu == $rstport -o $tcpportNeu == $rstport -o $rstportNeu == $tcpport; then
						echo -n "Port conflict" > $base/$isb/IGNORE 2> /dev/null 
					fi
				fi
			done
			if test ! -d $base/$anwendung; then
				mkdir $base/$anwendung
				mkdir $base/$anwendung/open
				mkdir $base/$anwendung/closed
			fi
			echo -n $tcpportNeu > $base/$anwendung/TCPPORT.CFG 2> /dev/null
			echo -n $rstportNeu > $base/$anwendung/RSTPORT.CFG 2> /dev/null
			if test -f $base/$anwendung/IGNORE; then
				rm $base/$anwendung/IGNORE		#Bei Konflikt mit anderer Spiegelgruppe wurde bereits die andere deaktiviert!
			fi
			if test $tcpportNeu = $rstportNeu; then
				echo -n "Port conflict: save port=restore port" > $base/$anwendung/IGNORE 2> /dev/null 
			fi
			shift 3                #Parameter verschieben $4->$1, $5->$2, $6->$3,...
		done
		sleep 2;
		$base/bin/isb.sh start
		sleep 5;
		$base/bin/isb.sh status
        ;;
  *)
        echo "Usage: $0 {start|stop|enable|restart|try-restart|force-reload|lock|unlock|shortStatus|status}"
        exit 1
esac

exit $RETVAL


