#!/bin/sh


vlan_configure() {
	if [ -e /etc/default/vlan_${2} ];then
		source /etc/default/vlan_${2}
	else
		echo "No static config found" && exit 0
	fi;

	table=$((${2} + 10000))
	metric=$((100 + (${METRIC-0} * 2)))
	ametric=$((1200 + ${METRIC-0}))

	/sbin/ip link set dev ${interface} up
	/sbin/ip rule add prio ${table} oif ${1}.${2} table ${table} >/dev/null 2>&1 |:

	if [ "${IP}" ] && [ "${SN}" ];then
		/sbin/ip rule add prio ${table} from ${IP} table ${table}
		/sbin/ip addr add dev ${1}.${2} local ${IP}/${SN} broadcast + label ${1}.${2}:static
		/sbin/ip route show table main scope link proto kernel |awk -v IFACE=${1}.${2} -v TABLE=${table} '$3 == IFACE {printf "ip route add %s scope link proto kernel table %s\n",$0,TABLE}' |sh 2>/dev/null | :
	fi;

	if [ "${GW}" ];then
		/sbin/ip route add default via ${GW} metric ${metric} dev ${1}.${2}
		/sbin/ip route add default via ${GW} metric ${metric} dev ${1}.${2} table ${table}
	fi;

	if [ "${VPNGW}" ] && [ "${VPNROUTE}" ];then
		for newrt in ${VPNROUTE};do
			ip route add ${newrt} via ${VPNGW} dev ${1}.${2} metric ${metric}  | :
			ip route add ${newrt} via ${VPNGW} dev ${1}.${2} metric ${metric} table ${table} | :
		done;
	fi;

	if [ "${DNS1}" ];then
		echo "nameserver ${DNS1}" > /tmp/static_resolv_${2}
	elif [ -e /tmp/static_resolv_${2} ];then
		rm /tmp/static_resolv_${2}
	fi;
	if [ "${DNS2}" ];then
		echo "nameserver ${DNS2}" >> /tmp/static_resolv_${2}
	fi;

	if [ -e /tmp/static_resolv_${2} ];then
		cat /tmp/static_resolv_${2} |/sbin/resolvconf -a ${1}.${2}.static
		rm /tmp/static_resolv_${2}
		/sbin/resolvconf -d ${1}.${2}.udhcpc
	else
		/sbin/resolvconf -d ${1}.${2}.static
	fi;
}

ethernet_configure() {
	if [ -e /etc/default/static ];then
        	source /etc/default/static
	else
		echo "No static config found" && exit 0
	fi;

	if [ "${IP}" ] && [ "${SN}" ];then
		/sbin/ip addr add ${IP}/${SN} brd + dev ${1} label ${1}:static | :
	fi;

	if [ "${GW}" ];then
		/sbin/ip route add default via ${GW} dev ${1} metric 11 | :
		/sbin/ip route add default via ${GW} dev ${1} metric 11 table 100 | :
	fi;

	if [ "${VPNGW}" ] && [ "${VPNROUTE}" ];then
		for newrt in ${VPNROUTE};do
			ip route add ${newrt} via ${VPNGW} dev ${1} metric 11 | :
			ip route add ${newrt} via ${VPNGW} dev ${1} metric 11 table 100 | :
		done;
	fi;

	if [ "${DNS1}" ];then
		echo "nameserver ${DNS1}" > /tmp/static_resolv
	elif [ -e /tmp/static_resolv ];then
		rm /tmp/static_resolv
	fi;
	if [ "${DNS2}" ];then
		echo "nameserver ${DNS2}" >> /tmp/static_resolv
	fi;

	if [ -e /tmp/static_resolv ];then
		cat /tmp/static_resolv |/sbin/resolvconf -a ${1}.static
		rm /tmp/static_resolv
		/sbin/resolvconf -d ${1}.udhcpc
	else
		/sbin/resolvconf -d ${1}.static
	fi;
}

[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1

case "$1" in
	deconfig|renew|bound)
		if [ "${interface}" == "eth0" ];then
			ethernet_configure ${interface} >/dev/null 2>&1 | :
		elif [ "${interface:0:4}" == "eth0" ];then
			vlanid=${interface:5}
			if [ ${vlanid} -gt 0 ] && [ ${vlanid} -lt 4097 ];then
				vlan_configure eth0 ${vlanid} >/dev/null 2>&1 | :
			fi;
		fi
		;;
esac
