#!/bin/bash

### BEGIN INIT INFO
# Provides:          ServerStatus-Client
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Server status monitoring
# Description:       Start or stop the ServerStatus Client server
### END INIT INFO

NAME="ServerStatus Client"
NAME_BIN="status-client.py"
Info_font_prefix="\033[32m" && Error_font_prefix="\033[31m" && Font_suffix="\033[0m"
if [[ -e "/usr/local/ServerStatus/client/status-client.py" ]]; then
	BIN="/usr/local/ServerStatus/client/status-client.py"
elif [[ -e "/usr/local/ServerStatus/status-client.py" ]]; then
	BIN="/usr/local/ServerStatus/status-client.py"
else
	echo -e "${Error_font_prefix}[错误]${Font_suffix} 客户端文件($NAME_BIN)找不到 !" && exit 1
fi
RETVAL=0

check_running(){
	PID=$(pgrep -f "${NAME_BIN}")
	if [[ -n ${PID} ]]; then
		return 0
	else
		return 1
	fi
}
do_start(){
	if check_running; then
	echo -e "${Info_font_prefix}[信息]${Font_suffix} $NAME (PID ${PID}) 正在运行..." && exit 0
	else
		ulimit -n 51200 >/dev/null 2>&1
		nohup python "$BIN" > /tmp/serverstatus_client.log 2>&1 &
		sleep 2s
		if check_running; then
			echo -e "${Info_font_prefix}[信息]${Font_suffix} $NAME 启动成功 !"
		else
			echo -e "${Error_font_prefix}[错误]${Font_suffix} $NAME 启动失败 !"
		fi
	fi
}
do_stop(){
	if check_running; then
		kill -9 "${PID}"
		RETVAL=$?
		if [[ $RETVAL -eq 0 ]]; then
			echo -e "${Info_font_prefix}[信息]${Font_suffix} $NAME 停止成功 !"
		else
			echo -e "${Error_font_prefix}[错误]${Font_suffix}$NAME 停止失败 !"
		fi
	else
		echo -e "${Info_font_prefix}[信息]${Font_suffix} $NAME 未运行 !"
		RETVAL=1
	fi
}
do_status(){
	if check_running; then
		echo -e "${Info_font_prefix}[信息]${Font_suffix} $NAME (PID ${PID}) 正在运行..."
	else
		echo -e "${Info_font_prefix}[信息]${Font_suffix} $NAME 未运行 !"
		RETVAL=1
	fi
}
do_restart(){
	do_stop
	do_start
}
case "$1" in
	start|stop|restart|status)
	do_"$1"
	;;
	*)
	echo "使用方法: $0 { start | stop | restart | status }"
	RETVAL=1
	;;
esac
exit $RETVAL