#!/bin/sh

# udhcpc script edited by Tim Riker <Tim@Rikers.org>

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

RESOLV_CONF="/etc/resolv.conf"
[ -n "$subnet" ] && NETMASK="netmask $subnet"

# return 0 if root is mounted on a network filesystem
root_is_nfs() {
	sed -n 's/^[^ ]* \([^ ]*\) \([^ ]*\) .*$/\1 \2/p' /proc/mounts |
	grep -q "^/ \(nfs\|smbfs\|ncp\|coda\)$"
}

link_local() {
	eval $(ipcalc -n ${2}/${3})
	/sbin/ip route add ${NETWORK}/${3} src ${2} dev ${1} scope link proto kernel metric ${4} table ${5}
}

have_bin_ip=0
if [ -x /sbin/ip ]; then
  have_bin_ip=1
  BROADCAST="broadcast +"
fi

[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"

vlanid=${interface:5}
if  [ "$interface" == "eth0" ];then
  metric=12
  ametric=1000
  table=100
elif [ "$interface" == "wlan0" ];then
  metric=52
  ametric=1100
  table=200
elif [ "$interface" == "usb0" ];then
  metric=992
  ametric=1200
  table=400
elif [ "$interface" == "bnep0" ];then
  metric=245
  table=50
elif [ "${interface:0:4}" == "eth0" ] && [ ${vlanid} -gt 0 ] && [ ${vlanid} -lt 4097 ];then
  if [ -e /etc/default/vlan_${vlanid} ];then
    source /etc/default/vlan_${vlanid}
   else
    echo "No VLAN config found for ${interface}" && exit 0
  fi

  table=$(($vlanid + 10000))
  metric=$((101 + (2 * ${METRIC})))
  ametric=$((1200 + ${METRIC}))
fi

case "$1" in
	deconfig)
		if [ -x /sbin/resolvconf ]; then
			/sbin/resolvconf -d "${interface}.udhcpc"
		fi
		if ! root_is_nfs ; then
                        if [ $have_bin_ip -eq 1 ]; then
                                /sbin/ip -4 addr flush dev $interface
                                /sbin/ip link set dev $interface up
				[ ! -z "${ametric}" ] && /sbin/ip route add 169.254.0.0/16 dev $interface scope link metric $ametric
                        else
                                /sbin/ifconfig $interface 0.0.0.0
                        fi
		fi
                if [ "${table}" ];then
                        while /sbin/ip rule del prio ${table} >/dev/null 2>&1;do :;done
                        /sbin/ip route flush scope link dev ${interface} table ${table} >/dev/null 2>&1 | :
                        /sbin/ip rule add prio ${table} oif ${interface} table ${table}
                        /sbin/ip route add 169.254.0.0/16 dev ${interface} scope link metric 1000 table ${table}
                fi
		;;

	renew|bound)
                if [ $have_bin_ip -eq 1 ]; then
                        /sbin/ip addr add dev $interface local $ip/$mask $BROADCAST label $interface:dhcp
                else
                        /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
                fi

		if [ "${table}" ];then
                        (/sbin/ip rule add prio ${table} from ${ip} table ${table} | :
                        /sbin/ip rule add prio ${table} oif ${interface} table ${table} | :
                        /sbin/ip route add 169.254.0.0/16 dev ${interface} scope link metric 1000 table ${table} |:) >/dev/null 2>&1
			(link_local ${interface} ${ip} ${mask} ${metric} ${table})
		fi;

		if [ -n "$router" ] && [ "${metric}" ];then
			if ! root_is_nfs ; then
                                if [ $have_bin_ip -eq 1 ]; then
                                        while /sbin/ip route del default dev $interface metric $metric 2>/dev/null ; do
                                                :
                                        done
                                else
                                        while /sbin/route del default gw 0.0.0.0 dev $interface 2>/dev/null ; do
                                                :
                                        done
                                fi
			fi

			for i in $router ; do
                                if [ $have_bin_ip -eq 1 ]; then
                                        (/sbin/ip route add default via $i metric $metric dev $interface
                                        /sbin/ip route add default via $i metric $metric dev $interface table $table) >/dev/null 2>&1
                                else
                                        /sbin/route add default gw $i dev $interface metric $metric 2>/dev/null
                                fi
                                metric=$(($metric + 1))
			done
		fi

		# Update resolver configuration file
		R=""
		[ -n "$domain" ] && R="domain $domain
"
		for i in $dns; do
			echo "$0: Adding DNS $i"
			R="${R}nameserver $i
"
		done

		if [ -x /sbin/resolvconf ]; then
			echo -n "$R" | /sbin/resolvconf -a "${interface}.udhcpc"
		else
			echo -n "$R" > "$RESOLV_CONF"
		fi
		;;
esac

exit 0
