#!/bin/sh

echo 1 > /proc/sys/net/ipv6/conf/${IFNAME}/accept_ra

kill_dns ()
{
	if [ -z $1 ]; then
		return 1
	fi

	# We can't use `dhclient -x` because ppp interface is gone this time
	# So we kill dhclient manually and delete gateway info here
	if [ -f /tmp/dhclient6-$1.pid ]; then
		pid=`/bin/cat /tmp/dhclient6-$1.pid`

		if [ -n ${pid} ]; then
			process=`/bin/echo \`/bin/cat /proc/${pid}/cmdline\` | /usr/bin/cut -d- -f 1`
			if [ ${process} = "/usr/sbin/dhclient" ]; then
				kill ${pid}
			fi
		fi
	fi

	/usr/syno/sbin/synonetdtool --del-gateway-info -6 $1
	/usr/syno/sbin/synonetdtool --refresh-gateway -6

	return 0
}

FW_SCRIPT="/usr/syno/etc/rc.d/S01iptables.sh"
TC_SCRIPT="/usr/syno/etc/rc.d/S01tc.sh"
RELAYDAEMON="/usr/syno/sbin/synorelayd"
CMS_UPDATE="/var/packages/CMS/target/tools/synocmsserver"

kill_dns ${IFNAME}

if [ -x "${FW_SCRIPT}" ]; then
	${FW_SCRIPT} restart /etc/firewall
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
