#!/bin/sh
. /etc/ppp/ip-function

TC_SCRIPT="/usr/syno/etc/rc.d/S01tc.sh"
RELAYDAEMON="/usr/syno/sbin/synorelayd"
UPNP_TOOL="/usr/syno/bin/miniupnp-tool.sh"
CMS_UPDATE="/var/packages/CMS/target/tools/synocmsserver"
SYNONETD_TOOL="/usr/syno/sbin/synonetdtool"
VPNC_CURRENT="/tmp/vpnc_current"
PPTPCLIENT_CONF="/usr/syno/etc/synovpnclient/pptp/pptpclient.conf"
L2TPCLIENT_CONF="/usr/syno/etc/synovpnclient/l2tp/l2tpclient.conf"
IPV6_PPPOE_CONF="/etc/sysconfig/networking/ipv6-pppoe$6"
if [ "$#" = "6" ]; then
	PPPOE_CONF="/etc/ppp/pppoe_$6.conf"
else
	PPPOE_CONF="/etc/ppp/pppoe.conf"
fi

# Don't do anything when this ppp is vpnclient which connect to our server
if check_is_client_of_vpnserver "${IFNAME}"; then
	logger -p user.err -t "/etc/ppp/ip-up" "skip ${IFNAME}"
	exit 0
fi

set_gateway_info()
{
	local priority=$1
	local class=$2
	local user_dns=$3
	local ifname="${IFNAME}"
	local gateway="NULL"
	local dns="NULL"
	local ip="${IPLOCAL}"

	logger -p user.err -t "/etc/ppp/ip-up" "${class} ${ifname} is up"

	if test -n "$IPREMOTE" ; then
		gateway="${IPREMOTE}"
	fi

	if [ -n "${user_dns}" ]; then
		dns="${user_dns}"
	elif [ -n "$DNS1" ]; then
		dns="${DNS1}"
	fi

	${SYNONETD_TOOL} --add-gateway-info -4 ${priority} ${ifname} ${gateway} ${dns} ${class}
	${SYNONETD_TOOL} --refresh-gateway -4
	${SYNONETD_TOOL} --refresh-policy-route-rules

	local enable_multi_gateway=`/bin/get_key_value /etc/synoinfo.conf multi_gateway`
	if [ "xyes" = "x${enable_multi_gateway}" ]; then
		${SYNONETD_TOOL} --add-policy-route-rule -4 multi-gateway ${ifname} NULL NULL ${gateway}
		${SYNONETD_TOOL} --refresh-route-table -4 ${ifname} ${ip}
	fi

}

if [ -x "${TC_SCRIPT}" ]; then
	${TC_SCRIPT} restart /etc/tc
fi
${RELAYDAEMON} -U
if [ -x "${CMS_UPDATE}" ]; then
	# We have to wait for relay service, but time is unknown...
	/bin/sh -c "sleep 30; ${CMS_UPDATE} --update_cred" &
fi

check_is_pppoe ${PPPD_PID}
if [ 0 -eq $? ]; then
	if [ -d /initrd ]; then
		local default_route=`/usr/syno/bin/synogetkeyvalue ${PPPOE_CONF} DEFAULTROUTE`
		local peer_dns=`/usr/syno/bin/synogetkeyvalue ${PPPOE_CONF} PEERDNS`
		local dns1=`/usr/syno/bin/synogetkeyvalue ${PPPOE_CONF} DNS1`
		local class="pppoe"
		local priority=""

		if [ "xyes" = "x${default_route}" ]; then
			priority="0"
		else
			priority="-2"
		fi

		if [ "xyes" = "x${peer_dns}" ]; then
			set_gateway_info ${priority} ${class}
		else
			set_gateway_info ${priority} ${class} ${dns1}
		fi
	else
		if test -n "$DNS1" ; then
			echo "nameserver $DNS1" > /etc/resolv.conf
		fi
		if test -n "$DNS2" ; then
			echo "nameserver $DNS2" >> /etc/resolv.conf
		fi
		/bin/chmod 644 /etc/resolv.conf 2>&1
	fi
	${UPNP_TOOL} sync-ppp

	if [ -f "${IPV6_PPPOE_CONF}" ]; then
		${SYNONETD_TOOL} --init-ipv6-router "pppoe${6}" ${IPLOCAL}
	fi
fi

check_is_usbmodem "$6"
if [ 1 -eq $? ]; then
	# add gateway info for 3glte, with origional priority
	set_gateway_info -2 "3g"
fi

check_is_vpnclient_pptp ${PPPD_PID}
if [ 0 -eq $? ]; then
	while [ ! -f ${VPNC_CURRENT} ]; do
		sleep 1
	done
	local con_id=`/usr/syno/bin/get_section_key_value ${VPNC_CURRENT} curr_info conf_id`
	local default_route=`/usr/syno/bin/get_section_key_value ${PPTPCLIENT_CONF} ${con_id} def_gateway`
	local class="vpn-client"
	local priority=""

	if [ "xyes" = "x${default_route}" ]; then
		priority="0"
	else
		priority="-2"
	fi

	set_gateway_info ${priority} ${class}
fi

check_is_vpnclient_l2tp ${PPPD_PID}
if [ 0 -eq $? ]; then
	while [ ! -f ${VPNC_CURRENT} ]; do
		sleep 1
	done
	local con_id=`/usr/syno/bin/get_section_key_value ${VPNC_CURRENT} curr_info conf_id`
	local default_route=`/usr/syno/bin/get_section_key_value ${L2TPCLIENT_CONF} ${con_id} def_gateway`
	local class="vpn-client"
	local priority=""

	if [ "xyes" = "x${default_route}" ]; then
		priority="0"
	else
		priority="-2"
	fi

	set_gateway_info ${priority} ${class}
fi

/etc/rc.network update-nat
