#!/bin/bash  
#  
# JYNGLTX
# chkconfig: 2345 99 01
# description: jingyunsd service, for communicating 

### BEGIN INIT INFO
# Provides:          JYNGLTX
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: jingyunsd service
# Description:      jingyunsd service, for communicating 
### END INIT INFO  

svr_name=""
jyn_install_path=/opt/apps/vrvsd-tl
verstr=`cat $jyn_install_path/Config.ini|grep -v "Mon"|grep "Type"`
versub=`echo ${verstr#*.}`
verflag=`echo ${versub:1:1}`
ZDFY=$jyn_install_path/filemonitor

versubsdk=`echo ${verstr##*.}`
verflagsdk=`echo ${versubsdk:2:1}`

is_protect()
{
	selfProtect=`cat $jyn_install_path/Config.ini|grep "SelfProtect"`
    echo ${selfProtect:14:1}
}

stop_self_protect()
{
	if [ -f $ZDFY ]; then
		$ZDFY stopSelfProtect
	fi
}

clean_iptables()
{
	if `iptables -nL JYNIN > /dev/null 2>&1`; then
		echo "clear iptables JYNIN"
		iptables -F JYNIN
		iptables -D INPUT -j JYNIN
		iptables -X JYNIN
	fi
	
	if `iptables -nL JYNOUT > /dev/null 2>&1`; then
		echo "clear iptables JYNOUT"
		iptables -F JYNOUT
		iptables -D OUTPUT -j JYNOUT
		iptables -X JYNOUT
	fi

	if `ip6tables -nL JYNIN > /dev/null 2>&1`; then
		echo "clear ip6tables JYNIN"
		ip6tables -F JYNIN
		ip6tables -D INPUT -j JYNIN
		ip6tables -X JYNIN
	fi
	
	if `ip6tables -nL JYNOUT > /dev/null 2>&1`; then
		echo "clear ip6tables JYNOUT"
		ip6tables -F JYNOUT
		ip6tables -D OUTPUT -j JYNOUT
		ip6tables -X JYNOUT
	fi
}

start_self_protect()
{
	if [ "$(is_protect)"e != "0"e ] ; then
		if [ -f $ZDFY ]; then
			$ZDFY startSelfProtect
		fi
	fi
}

get_svr_name() {
	if [ -f $jyn_install_path/JingyunsdIPCServer ]; then
		svr_name="JingyunsdIPCServer"
	elif [ -f $jyn_install_path/JingyunSdCtrlCenter ]; then
		svr_name="JingyunSdCtrlCenter"
	else
		svr_name="JYNGLTX1"
	fi
	echo "service name:[$svr_name]"
}

start() {  
	get_svr_name
	PID=`pidof $svr_name`
	if [ "$PID" = "" ] ; then
		nohup $jyn_install_path/$svr_name >/dev/null 2>&1  &
		echo "start $svr_name"
	else
		echo "$svr_name already running"
	fi
}  

stop() {  
	clean_iptables
	stop_self_protect
	get_svr_name
	PID=`pidof $svr_name`
	if [ "$PID" = "" ] ; then
		echo "$svr_name not running"
	else
		kill -9 `pidof $svr_name`
	fi
	start_self_protect
}  

status(){
	get_svr_name
	PID=`pidof $svr_name`
	if [ "$PID" = "" ] ; then
		echo "$svr_name not running"
	else
		echo "$svr_name is running"
	fi
}

case "$1" in  
start)  
start  
;;  
stop)  
stop  
;;  
reload|restart)  
stop  
start  
;;  
status)  
status
;;  
*)  
echo $"Usage: $0 {start|stop|restart|status}"  
exit 1  
esac  
