#!/bin/sh 

NAME=pgyvpn
config_file="/home/sysuser/project/pgysvr.ini"

# __readINI [配置文件路径+名称] [节点名] [键值]
__readINI()
{
	INIFILE=$1; SECTION=$2; ITEM=$3; DEFAULT=$4
	_readIni=`awk -F '=' '/\['$SECTION'\]/{a=1}a==1&&$1~/'$ITEM'/{print $2;exit}' $INIFILE`
	if [ -n "${_readIni}" ] ;then
		echo ${_readIni}
	else
		echo ${DEFAULT}
	fi
}

start_pgyvpnsvr()
{
	local res_def_path='/home/sysuser/bin/pgyvpn'
	local pgyvpn_filename=$(__readINI $config_file base filename '/home/sysuser/bin/pgyvpn/pgyvpnsvr')
	local username=$(__readINI $config_file base user '')
	local password=$(__readINI $config_file base pwd '')
	local check_route_conflict=$(__readINI $config_file base check_route_conflict 1)
	local log_path=$(__readINI $config_file log path '/tmp/oray/pgyvpnsvr/log')
	local log_mask=$(__readINI $config_file log mask '0x77')
	local use_encrypt=$(__readINI $config_file secure encrypt '0')
	local script_p2pinit=$(__readINI $config_file base script_p2pinit "$res_def_path/p2pinit.sh")
	local script_vncinit=$(__readINI $config_file base script_vncinit "$res_def_path/vncinit.sh")
	local script_progress=$(__readINI $config_file base script_progress "$res_def_path/vpnprogress.sh")
	local oraysl_filename=$(__readINI $config_file oraysl filename '/home/sysuser/bin/pgyvpn/pgyvpn_oraysl')
	local oraysl_logpath=$(__readINI $config_file oraysl logpath '/tmp/oray/pgyvpn_oraysl/log')
	local oraysl_statusfilename=$(__readINI $config_file oraysl statusfilename '/tmp/oray/pgyvpn_oraysl/status')
	local oraysl_pidfilename=$(__readINI $config_file oraysl pidfilename '/tmp/oray/pgyvpn_oraysl/pid')

	#make app command args	
	local cmd_arg=''
	local api_address="pgyapi.oray.net"
	cmd_arg="$cmd_arg --apiaddress $api_address --usehttps --rpc --autologinAsRpc -d" 
	cmd_arg="$cmd_arg --logsize 4194304 --logpath $log_path --logmask $log_mask"
	cmd_arg="$cmd_arg --orayslpath $oraysl_filename --oraysllogpath $oraysl_logpath --orayslstatusfilename $oraysl_statusfilename --orayslpidfilename $oraysl_pidfilename"
	[ -n "$script_p2pinit" ] && cmd_arg="$cmd_arg -n $script_p2pinit"
	[ -n "$script_vncinit" ] && cmd_arg="$cmd_arg -n $script_vncinit"
	[ -n "$script_progress" ] && cmd_arg="$cmd_arg -N $script_progress"
	[ $use_encrypt -eq 1 ] && cmd_arg="$cmd_arg -r"
	[ -n "$username" -a -n "$password" ] && cmd_arg="$cmd_arg --sn $username --pwd $password"
	[ $check_route_conflict -ne 0 ] && cmd_arg="$cmd_arg --vipmask 255.255.255.255"

	#$pgyvpn_filename $cmd_arg 2>&1 > /dev/null
	/home/sysuser/bin/pgyvpn/pgyvpnsvr $cmd_arg 2>&1 > /dev/null
	p_daemon=$(pidof pgydaemon)
	[ -z "$p_daemon" ] && /home/sysuser/bin/pgyvpn/pgydaemon > /dev/null 2>&1

}

stop_pgyvpnsvr()
{
	#remove p2p firewall
	iptables -w -t filter -F oray_vpn_p2p 2> /dev/null
	while true;
	do
		iptables -w -t filter -D INPUT -j oray_vpn_p2p 2>/dev/null
		[ $? -ne 0 ] && break
	done
	iptables -w -t filter -X oray_vpn_p2p 2> /dev/null

	#remove forward firewall
	iptables -w -t filter -F oray_vpn_vnc 2> /dev/null
	while true;
	do
		iptables -w -t filter -D INPUT -j oray_vpn_vnc 2>/dev/null
		[ $? -ne 0 ] && break
	done

	while true;
	do
		iptables -w -t filter -D FORWARD -j oray_vpn_vnc 2>/dev/null
		[ $? -ne 0 ] && break
	done

	while true;
	do
		iptables -w -t filter -D OUTPUT -j oray_vpn_vnc 2>/dev/null
		[ $? -ne 0 ] && break
	done
	iptables -w -t filter -X oray_vpn_vnc 2> /dev/null

	#stop daemon
	killall -9 pgydaemon > /dev/null 2>&1

	#stop vpn
	killall pgyvpnsvr 2> /dev/null
	local vpnpid
	for i in $(seq 1 10)
	do
		vpnpid=$(pidof pgyvpnsvr)
		[ -z "$vpnpid" ] && {
			echo 'pgyvpnsvr processor is stopped'
			break
		}
		echo "pgyvpnsvr processor is still alive(pid is $vpnpid), wait 1 second"
		sleep 1
	done
	vpnpid=$(pidof pgyvpnsvr)
	[ -z "$vpnpid" ] || {
		echo "pgyvpnsvr processor is still alive(pid is $vpnpid), kill it"
		killall -9 pgyvpnsvr 2> /dev/null

		#kill oraysl
		local oraysl_filename=$(__readINI $config_file oraysl filename '/home/sysuser/bin/pgyvpn/pgyvpn_oraysl')
		killall -9 ${oraysl_filename##*/} 2>/dev/null
	}

	rm /tmp/pgyvpnsvr_mutex 2> /dev/null #delete mutex file
	rm /tmp/pgyvpnsvr_rpc_mutex 2> /dev/null #delete rpc mutex file

	local oraysl_statusfilename=$(__readINI $config_file oraysl statusfilename '/tmp/oray/pgyvpn_oraysl/status')
	rm $oraysl_statusfilename 2> /dev/null #delete oraysl status file
}

case "$1" in
  start)
    start_pgyvpnsvr
  ;;
  stop)
    stop_pgyvpnsvr
  ;;
  restart)
    stop_pgyvpnsvr
    start_pgyvpnsvr
  ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart}" >&2
    exit 2
  ;;
esac

