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

FW_SCRIPT="/usr/syno/etc/rc.d/S01iptables.sh"
RELAYDAEMON="/usr/syno/sbin/synorelayd"
UPNP_TOOL="/usr/syno/bin/miniupnp-tool.sh"

if [ -x "${FW_SCRIPT}" ]; then
	${FW_SCRIPT} restart /etc/firewall
fi
${RELAYDAEMON} -U

check_is_pppoe ${PPPD_PID}
if [ 0 -eq $? ]; then
	if [ -d /initrd ]; then
		if test -n "$DNS1" ; then
			echo "nameserver $DNS1" > /etc/ppp/resolv.conf.pppoe
		fi
		if test -n "$DNS2" ; then
			echo "nameserver $DNS2" >> /etc/ppp/resolv.conf.pppoe
		fi
		if [ -n "$DNS1" ]; then
			/bin/chmod 644 /etc/ppp/resolv.conf.pppoe 2>&1
			/usr/syno/sbin/synonet --dns_update > /dev/null 2>&1
		fi
		if test -n "$IPREMOTE" ; then
			echo "$IPREMOTE" > /tmp/pppoe_gw_backup
		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
fi

check_is_usbmodem "$6"
if [ 1 -eq $? ]; then
	local dns_mode=`/bin/get_key_value /etc/synoinfo.conf dns_mode`
	if [ "static" = "${dns_mode}" ]; then
		# skip dns set when dns_mode is static
		exit 0;
	fi
	local SZF_RESOLV_FILE_TMP='/tmp/resolv.conf.tmp'
	local SZF_RESOLV_FILE='/etc/resolv.conf'

	check_usbmodem_is_defaultroute
	if [ 1 -eq $? ]; then
		cp $SZF_RESOLV_FILE $SZF_RESOLV_FILE_TMP
		echo "nameserver $DNS1" > $SZF_RESOLV_FILE
		echo "nameserver $DNS2" >> $SZF_RESOLV_FILE
		cat $SZF_RESOLV_FILE_TMP >> $SZF_RESOLV_FILE

		rm -f $SZF_RESOLV_FILE_TMP
	else
		echo "nameserver $DNS1" >> $SZF_RESOLV_FILE
		echo "nameserver $DNS2" >> $SZF_RESOLV_FILE
	fi
fi
