#!/bin/sh
### BEGIN INIT INFO
# Provides:          yhkydefender
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: yhkydefender
# Description:       yhkydefender
### 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="/usr/sbin/defender"
main_process_name="yhkydefenderd"

# scan program
yhkydefenderd=$install_dir"/$main_process_name"
yhkydefenderd_pid="/var/run/$main_process_name.pid"

# 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
#

#檢查該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
	if [ $? -eq 0 ];then
		echo "stopped    $1 "
		return 1
	else
		echo "running    $1"
		return 0
	fi
}

status()
{
	echo "=================status===================="
	query_process_status $yhkydefenderd $yhkydefenderd_pid
	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 >/dev/null 2>&1 &`
			loop_query_start $1 $2
		else
			echo "start $1 param error."
		fi
	else
		echo "$1 is already started"
	fi
}

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()
{
	echo "=================start====================="
	echo "..."

	set_thirdlib_path
	start_process $yhkydefenderd $yhkydefenderd_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`
		if [ $# -eq 2 ]; 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 $pid
		fi
	else
		echo "process $1 already stopped"
	fi
}

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

	#stop_monitor

	stop_process $yhkydefenderd $yhkydefenderd_pid

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


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