#!/bin/sh
### BEGIN INIT INFO
# Provides:          360safe
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: 360safe
# Description:       360safe
### END INIT INFO

# Author: sly

# PATH should only include /usr/* if it runs after the mountnfs.sh script
# eg PATH=/sbin:/usr/sbin:/bin:/usr/bin
# eg DESC="Description of the service"
# eg NAME=daemonexecutablename
# eg DAEMON=/usr/sbin/$NAME
# eg DAEMON_ARGS="--options args"
# eg PIDFILE=/var/run/$NAME.pid
# eg SCRIPTNAME=/etc/init.d/$NAME

# var
RETVAL=0
pid=""

install_dir="/opt/apps/com.qianxin.qaxsafe/files"
main_process_name="360safed"

# scan program
scanner=$install_dir"/$main_process_name"
scanner_pid=$install_dir"/.$main_process_name.pid"
scanner_ppid=$install_dir"/.$main_process_name.ppid"

group_option=

# Exit if the package is not installed
# eg [ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present

# Load the VERBOSE setting and other rcS variables
# eg . /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
# eg . /lib/lsb/init-functions

#
# Function that starts the daemon/service
#

get_group_option()
{
	# is esxi 5 or 6 ?
    local osname="`uname -a | awk '{print $1}' | head -n 1`"
    if [ "$osname" = "VMkernel" ]; then
        local version_info="`uname -a | awk '{print $3}' | head -n 1`"
        local version="${version_info:0:1}"
        if [ "$version" = "5" -o "$version" = "6" ]; then
            group_option="++group=host/vim/vmvisor/"
        fi
    fi
}

#檢查該PID的進程是否存在，輸入參數pid文件路徑
check_alive_pid()
{
	if [ ! -f "$1" ]; then
		return 0
	fi
	pid=`cat $1 2>/dev/null`
	if [ ! -f "$1" ]; then
		return 0
	fi
	ret1=`ps -CPc 2>/dev/null | grep "^$pid" | grep "$main_process_name" | wc -l`
	ret2=`ps --no-headers -p $pid 2>/dev/null | grep "$main_process_name" | wc -l`
	ret=`expr $ret1 + $ret2`
	#echo "$1 pid = $pid, ret = $ret"
	if [ $ret -eq 1 ]; then
		return 1
	else
		#刪除pid文件
		#if [ -f "$1" ]; then
		#	rm $1
		#fi
		return 0
	fi
}

#检查進程状态,輸入參數執行文件路徑+pid文件路徑
query_process_status()
{
	check_alive_pid $2 && check_alive_pid $3
	if [ $? -eq 0 ];then
		echo "stopped    $1 "
		return 1
	else
		echo "running    $1"
		return 0
	fi
}

status()
{
	echo "=================status===================="
	query_process_status $scanner $scanner_pid $scanner_ppid
	return $?
}

loop_query_start()
{
	query_times=10
	while [ $query_times -gt 0 ]; do
		check_alive_pid $2
		if [ $? -eq 1 ];then
			query_times=0
		else
			echo "waiting start $1 ..."
			query_times=`expr $query_times - 1`
			sleep 1
		fi
	done

	check_alive_pid $2
	if [ $? -eq 0 ]; then
		echo "start $1 failed."
	fi
}

#開啟進程,輸入參數執行文件路徑+pid文件路徑
#開啟py脚本,輸入參數執行文件路徑+pid文件路徑+py
start_process()
{
	check_alive_pid $2
	if [ $? -eq 0 ]; then
		if [ $# -eq 2 ]; then
			`$1 $group_option >/dev/null 2>&1 &`
			loop_query_start $1 $2
		elif [ $# -eq 3 ]; then
			`nohup $python360 $1 $3 >/dev/null 2>&1 &`
			loop_query_start $1 $2
		elif [ $# -eq 4 ]; then
			`$1 $3 >/dev/null 2>&1 &`
			loop_query_start $1 $2
		else
			echo "start $1 param error."
		fi
	else
		echo "$1 is already started"
	fi
}

stop_oldprocess()
{
	`pkill -9 python360 2>/dev/null`
	`pkill -9 360scanner 2>/dev/null`
	`pkill -9 360taskbus 2>/dev/null`
}

set_thirdlib_path()
{
	FULLPATH=$install_dir"/thirdlib"
	if [ "$LD_LIBRARY_PATH"  ] ; then 
		if [[ $LD_LIBRARY_PATH != ${FULLPATH}* ]]; then
			export LD_LIBRARY_PATH=${FULLPATH}:$LD_LIBRARY_PATH
		fi
	else
		export LD_LIBRARY_PATH=${FULLPATH}
	fi
}

start()
{
	stop_oldprocess

	echo "=================start====================="
	echo "..."

	set_thirdlib_path
	start_process $scanner $scanner_pid
	status
	return $?
}

loop_query_stop()
{
	query_times=10
	while [ $query_times -gt 0 ]; do
		check_alive_pid $2
		if [ $? -eq 1 ];then
			echo "waiting stop $1 ..."
		query_times=`expr $query_times - 1`
		sleep 1
		else
		query_times=0
		fi
	done
}

#停止進程,輸入參數執行文件路徑+pid文件路徑
#停止py脚本,輸入參數執行文件路徑+pid文件路徑
stop_process()
{
	check_alive_pid $2
	if [ $? -eq 1 ]; then
		pid=`cat $2 2>/dev/null`
		ppid=`cat $3 2>/dev/null`
		if [ $# -eq 3 ]; then
			kill -INT $pid    #结束redisserver,计划后续都这样改，程序内接收SIGTERM信号，自动退出
		else
			kill -9 $pid
		fi
		loop_query_stop $1 $2
		check_alive_pid $2
		if [ $? -eq 1 ];then
			kill -9 $ppid
			kill -9 $pid
		fi
	else
		echo "process $1 already stopped"
	fi

	#删除pid文件
	# if [ -f $2 ]; then
	# 	rm -f $2
	# fi
}

#停止监控
stop_monitor()
{
	sleep 2
	status
}

stop()
{
	echo "==================stop====================="
	echo "..."

	#stop_monitor

	stop_process $scanner $scanner_pid $scanner_ppid

	status
	if [ $? -eq 0 ];then
		return 1
	else
		return 0
	fi
}

get_group_option

case "$1" in
	start)
		start && exit 0 || exit $?
		;;
	stop)
		stop && exit 0 || exit $?
		;;
	restart)
		stop || exit $?
		start && exit 0 || exit $?
		;;
	status)
		status && exit 0 || exit $?
		;;
	stopmonitor)
		stop_monitor
		;;
	*)
		echo $"Usage : %0 {start|stop|restart|status}"
		RETVAL=1
esac
exit $RETVAL
