#!/bin/sh

#root       829   828  2 10:28 ?        00:00:00 ./amy4Server -config=amy4Server.json
#chkconfig: 2345 99 99
### BEGIN INIT INFO
# Provides:          amy4Server
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start amy4Server daemon at boot time
# Description:       Start amy4Server daemon at boot time
### END INIT INFO
START=99
EXTRA_COMMANDS="status"

usage()
{
	if type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ]; then
		us="systemctl [start|stop|status|restart] amy4Server.service"
	else
		if [ "$0" = '/etc/rc.common' ]; then
			us="/etc/init.d/amy4Server [start|stop|status|restart]"\
		else
			us="$0 [start|stop|status|restart]"
		fi
	fi &>/dev/null
	echo "Usage:
	$us
	Config file is [amy4Server_install_dir]/amy4Server.json"
}

status()
{
	grep -q amy4Server /proc/`cat "[amy4Server_install_dir]/run.pid" 2>/dev/null`/comm 2>/dev/null && \
		echo "amy4Server is running..." || \
		echo "amy4Server is stopped..."
} 2>/dev/null


start()
{
	status | grep running && return 0
	echo -n "Starting amy4Server:"
	for server_port in [amy4Server_tcp_port_list]; do
		iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
		ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
	done
	cd "[amy4Server_install_dir]"
	rm -f "[amy4Server_install_dir]/run.pid"
	(while :; do
		./amy4Server -config=amy4Server.json
		[ "$?" = '143' -o "$?" = '0' ] && break
		sleep 3
	done >/dev/null &)
	count=0
	while [ ! -f "[amy4Server_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do
		sleep 1
		count=$((count + 1))
	done
	grep -q amy4Server /proc/`cat [amy4Server_install_dir]/run.pid 2>/dev/null`/comm && \
		echo -e "\033[60G[\033[32m  OK  \033[0m]" || \
		echo -e "\033[60G[\033[31mFAILED\033[0m]"
}

stop()
{
	for server_port in [amy4Server_tcp_port_list]; do
		while iptables -S INPUT | grep -qE "\-\-dport $server_port \-j ACCEPT"; do
			iptables -D INPUT -p 6 --dport $server_port -j ACCEPT
		done
		while ip6tables -S INPUT | grep -qE "\-\-dport $server_port \-j ACCEPT"; do
			ip6tables -D INPUT -p 6 --dport $server_port -j ACCEPT
		done
	done
	status | grep stopped && return 0
	echo -n "Stopping amy4Server:"
	kill `cat [amy4Server_install_dir]/drun.pid` `cat [amy4Server_install_dir]/run.pid`
	sleep 1
	grep -q amy4Server /proc/`cat [amy4Server_install_dir]/run.pid`/comm && \
		echo -e "\033[60G[\033[31mFAILED\033[0m]" || \
		echo -e "\033[60G[\033[32m  OK  \033[0m]"
} 2>/dev/null

restart()
{
	stop
	start
} 2>/dev/null

systemdStart()
{
	status | grep running && return 0
	cd "[amy4Server_install_dir]"
	rm -f "[amy4Server_install_dir]/run.pid"
	for server_port in [amy4Server_tcp_port_list]; do
		iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
		ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
	done
	(while :; do
		./amy4Server -config=amy4Server.json
		[ "$?" = '143' -o "$?" = '137' -o "$?" = '0' ] && break
		sleep 3
	done >/dev/null &)
	count=0
	while [ ! -f "[amy4Server_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do
		sleep 1
		count=$((count + 1))
	done
} 2>/dev/null

systemdRestart()
{
	stop
	systemdStart
} 2>/dev/null

if [ -z "$initscript" ]; then
	${1:-usage}
fi
