#!/bin/sh

PPP_IFACE=${PPP_IFACE-ppp0}

gen_dhcpcd() {
	cat <<__EOF__
duid
noipv6rs
ipv6only
slaac private
logfile /var/log/dhcpcd_ppp0.log
ipv6ra_noautoconf
#persistent

background
waitip 6
timeout 15
#debug

allowinterfaces ${PPP_IFACE}

interface ${PPP_IFACE}
#	ipv6rs
__EOF__
	printf "\tia_pd ${PPP_IFACE}";

	PRE=0

	source /etc/default/ppp
	EXT_VLAN=${VLAN}
	if [ "${IPV6}" == "1" ];then
		echo -n " eth0/${PRE}/64/0";
		PRE=$((${PRE} + 1));
	fi;

	IPV6=0;
	source /etc/default/wifi
	if [ "${IPV6}" == "1" ];then
		if [ "${WIFI_MODE}" == "STA" ];then
			echo -n " wlan0/${PRE}/64/0";
		else
			echo -n " wlan1/${PRE}/64/0";
		fi
		PRE=$((${PRE} + 1));
	fi

	source /etc/default/vlans;
	for vlanid in ${VLANS};do
		if [ ! -e /etc/default/vlan_${vlanid} ];then
			continue;
		fi;
		IPV6=0;
		source /etc/default/vlan_${vlanid}
		if [ "${IPV6}" != "1" ];then
			continue;
		fi;
		if [ "${EXT_VLAN}" == "${vlanid}" ];then
			continue
		fi;
		echo -n " eth0.${vlanid}/${PRE}/64/0";
		PRE=$((${PRE} + 1));
	done

	if [ "${EXT_VLAN}" ] && [ -e /etc/default/vlan_${EXT_VLAN} ];then
		IPV6=0
		source /etc/default/vlan_${EXT_VLAN}
		if [ "${IPV6}" == "1" ];then
			echo -n " eth0.${EXT_VLAN}/${PRE}/64/1";
			PRE=$((${PRE} + 1));
		fi;
	fi;
	echo
}

if [ ! -d /run/netconf ];then
  mkdir -p /run/netconf
fi;


gen_dhcpcd > /run/netconf/dhcpcd_${PPP_IFACE}.conf
if [ -e /var/run/dhcpcd-${PPP_IFACE}.pid ];then
	/bin/kill $(cat /var/run/dhcpcd-${PPP_IFACE}.pid)
	sleep 2
	if [ -e /var/run/dhcpcd-${PPP_IFACE}.pid ];then
		/bin/kill -9 $(cat /var/run/dhcpcd-${PPP_IFACE}.pid)
		sleep 2
	fi;
fi;

/etc/init.d/radvd stop
/usr/sbin/dhcpcd -f /run/netconf/dhcpcd_${PPP_IFACE}.conf ${PPP_IFACE}
