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

### BEGIN INIT INFO
# Provides:          JYNSAFED
# 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/cn.vsecure.chenxinsd/files
verstr=`cat $jyn_install_path/Config.ini|grep -v "Mon"|grep "Type"`
versub=`echo ${verstr#*.}`
verflag=`echo ${versub:1:1}`

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

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

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
}

get_svr_name() {
	svr_name="JYNSAFED"
	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
	get_svr_name
	kill -15 `ps aux | grep "$svr_name" | grep -v "grep" | awk '{print $2}'` > /dev/null 2>&1
	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  
