#!/bin/sh /etc/rc.common

. /lib/functions.sh

START=62
STOP=62
USE_PROCD=1

write_log()
{
	echo $*
	logger -t pgyvpn "$*"
}

start_service()
{
	local res_def_path='/usr/share/pgyvpn'
	local pgyvpn_filename
	local username
	local password
	local check_route_conflict
	local log_path
	local log_mask
	local local_ip
	local local_mask
	local use_encrypt
	local script_p2pinit
	local script_vncinit
	local script_progress
	local oraysl_filename
	local oraysl_logpath
	local oraysl_statusfilename
	local oraysl_pidfilename
	local login_status
	local enable_status

	#read uci config
	config_load pgyvpn
	config_get login_status base login_status
	config_get enable_status base enable_status

	if [ -z $enable_status ] || [ "$enable_status" == "0" ]; then
		exit 0
	fi

	config_get pgyvpn_filename base filename '/usr/sbin/pgyvpnsvr'
	config_get username base user ''
	config_get password base pwd ''
	config_get_bool check_route_conflict base check_route_conflict 0
	config_get log_path log path '/tmp/oray/pgyvpnsvr/log'
	config_get log_mask log mask '0x77'
	config_get_bool use_encrypt secure encrypt 0
	config_get script_p2pinit base script_p2pinit "$res_def_path/p2pinit.sh"
	config_get script_vncinit base script_vncinit "$res_def_path/vncinit.sh"
	config_get script_progress base script_progress "$res_def_path/vpnprogress.sh"
	config_get oraysl_filename oraysl filename '/usr/sbin/pgyvpn_oraysl'
	config_get oraysl_logpath oraysl logpath '/tmp/oray/pgyvpn_oraysl/log'
	config_get oraysl_statusfilename oraysl statusfilename '/tmp/oray/pgyvpn_oraysl/status'
	config_get oraysl_pidfilename oraysl pidfilename '/tmp/oray/pgyvpn_oraysl/pid'
	local_ip=$(uci get network.lan.ipaddr 2> /dev/null)
	local_mask=$(uci get network.lan.netmask 2> /dev/null)

	local host_name=$(uci get system.@system[0].hostname 2> /dev/null)

	#print config
	write_log "pgyvpn app filename is $pgyvpn_filename"
	write_log "username is $username"
	write_log "log path is $log_path"
	write_log "log mask is $log_mask"
	write_log "local ip is $local_ip"
	write_log "local mask is $local_mask"
	write_log "script for p2p init is $script_p2pinit"
	write_log "script for vnc init is $script_vncinit"
	write_log "script for progress is $script_progress"
	write_log "oraysl filename is $oraysl_filename"
	write_log "oraysl log path is $oraysl_logpath"
	write_log "oraysl status filename is $oraysl_statusfilename"
	write_log "oraysl pid filename is $oraysl_pidfilename"
	[ $use_encrypt -eq 1 ] && write_log 'use encrypt' || write_log 'no use encrypt'

	#make app command args
	local cmd_arg=''
	local api_address="pgyapi.oray.net"
	cmd_arg="$cmd_arg --apiaddress $api_address --usehttps --rpc --autologinAsRpc"
	#cmd_arg="$cmd_arg --apiaddress $api_address --usehttps --autologinAsRpc"
	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 -C $script_vncinit"
	# [ -n "$script_progress" ] && cmd_arg="$cmd_arg -n $script_progress"
	[ -n "$script_progress" ] && cmd_arg="$cmd_arg -N $script_progress"
	[ -n "$local_ip" ] && cmd_arg="$cmd_arg --localip $local_ip"
	[ -n "$local_mask" ] && cmd_arg="$cmd_arg --localmask $local_mask"
	[ $use_encrypt -eq 1 ] && cmd_arg="$cmd_arg -r"
	[ $check_route_conflict -ne 0 ] && cmd_arg="$cmd_arg --vipmask 255.255.255.255"
	[ -n "$host_name" ] && cmd_arg="$cmd_arg --hostname $host_name"

	#start app
	procd_open_instance
	procd_set_param command $pgyvpn_filename $cmd_arg
	[ -n "$username" ] && procd_append_param command --sn "$username"
	[ -n "$password" ] && procd_append_param command --pwd "$password"
	procd_set_param respawn
	procd_close_instance

}

stop_service()
{
	#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 oraysl
	killall pgyvpn_oraysl 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
	config_load pgyvpn
	config_get oraysl_statusfilename oraysl statusfilename '/tmp/oray/pgyvpn_oraysl/status'
	rm oraysl_statusfilename 2> /dev/null #delete oraysl status file
}
