#!/bin/sh
[ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1

. /etc/functions.sh
include /lib/network
RESOLV_CONF="/tmp/resolv.conf.auto"

change_state () {
	[ -n "$ifc" ] || return
	uci_revert_state "$1" "$2" "$3" "$4"
	uci_set_state "$1" "$2" "$3" "$4"
}

set_classless_routes() {
	local max=128
	local type
	while [ -n "$1" -a -n "$2" -a $max -gt 0 ]; do
		[ ${1##*/} -eq 32 ] && type=host || type=net
		echo "udhcpc: adding route for $type $1 via $2"
		route add -$type "$1" gw "$2" dev "$interface"
		max=$(($max-1))
		shift 2
	done
}

setup_interface () {
	local old_ip
	local old_broadcast
	local old_subnet
	local old_router
	local old_dns
	local user_dns
	local user_router
	local user_metric

	[ -n "$ifc" ] && {
		old_ip="$(uci_get_state network "$ifc" ipaddr)"
		old_broadcast="$(uci_get_state network "$ifc" broadcast)"
		old_subnet="$(uci_get_state network "$ifc" netmask)"
	}

	[ "$ip" != "$old_ip" ] \
	|| [ "${broadcast:-+}" != "$old_broadcast" ] \
	|| [ "${subnet:-255.255.255.0}" != "$old_subnet" ] && {
		echo "udhcpc: ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}"
		ifconfig $interface $ip netmask ${subnet:-255.255.255.0} broadcast ${broadcast:-+}

		change_state network "$ifc" ipaddr "$ip"
		change_state network "$ifc" broadcast "${broadcast:-+}"
		change_state network "$ifc" netmask "${subnet:-255.255.255.0}"
	}


	# Default Route
	[ -n "$ifc" ] && {
		change_state network "$ifc" lease_gateway "$router"
		old_router="$(uci_get_state network "$ifc" gateway)"
		user_router="$(uci_get network "$ifc" gateway)"
		user_metric="$(uci_get network "$ifc" metric)"
		[ -n "$user_router" ] && router="$user_router"
	}

	[ -n "$router" ] && [ "$router" != "0.0.0.0" ] && [ "$router" != "255.255.255.255" ] && [ "$router" != "$old_router" ] && {
		echo "udhcpc: setting default routers: $router"

		local valid_gw=""
		for i in $router ; do
			route add default gw $i ${user_metric:+metric $user_metric} dev $interface
			valid_gw="${valid_gw:+$valid_gw|}$i"
		done
		
		eval $(route -n | awk '
			/^0.0.0.0\W{9}('$valid_gw')\W/ {next}
			/^0.0.0.0/ {print "route del -net "$1" gw "$2";"}
		')

		change_state network "$ifc" gateway "$router"
	}

	# CIDR STATIC ROUTES (rfc3442)
	[ -n "$staticroutes" ] && set_classless_routes $staticroutes
	[ -n "$msstaticroutes" ] && set_classless_routes $msstaticroutes

	# DNS
	old_dns=$(uci_get_state network "$ifc" dns)
	old_domain=$(uci_get_state network "$ifc" dnsdomain)
	user_dns=$(uci_get "network.$ifc.dns")
	[ -n "$user_dns" ] && dns="$user_dns"

	[ -n "$dns" ] && [ "$dns" != "$old_dns" -o -n "$user_dns" ] && {
		echo "udhcpc: setting dns servers: $dns"
		add_dns "$ifc" $dns

		[ -n "$domain" ] && [ "$domain" != "$old_domain" ] && {
			echo "udhcpc: setting dns domain: $domain"
			sed -i -e "${old_domain:+/^search $old_domain$/d; }/^search $domain$/d" "${RESOLV_CONF}"
			echo "search $domain" >> "${RESOLV_CONF}"
			change_state network "$ifc" dnsdomain "$domain"
		}
	}

	[ -n "$ifc" ] || return

	# UCI State
	change_state network "$ifc" lease_server "$serverid"
	change_state network "$ifc" lease_acquired "$(sed -ne 's![^0-9].*$!!p' /proc/uptime)"
	change_state network "$ifc" lease_lifetime "$lease"
	[ -n "$ntpsrv" ] && 	change_state network "$ifc" lease_ntpsrv "$ntpsrv"
	[ -n "$timesvr" ] && 	change_state network "$ifc" lease_timesrv "$timesvr"
	[ -n "$hostname" ] &&	change_state network "$ifc" lease_hostname "$hostname"
	[ -n "$timezone" ] && 	change_state network "$ifc" lease_timezone "$timezone"


	# Hotplug
	env -i ACTION="$1" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
}


scan_interfaces
applied=
for ifc in $interfaces __default; do
	if [ "$ifc" = __default ]; then
		ifc=""
		[ -n "$applied" ] && continue
	else
		config_get ifname "$ifc" ifname
		[ "$ifname" = "$interface" ] || continue

		config_get proto "$ifc" proto
		[ "$proto" = "dhcp" ] || continue
		applied=true
	fi

	case "$1" in
		deconfig)
			ifconfig "$interface" 0.0.0.0
			[ -n "$ifc" ] && {
				env -i ACTION="ifdown" INTERFACE="$ifc" DEVICE="$ifname" PROTO=dhcp /sbin/hotplug-call iface
			
				config_get device "$ifc" device
				config_get ifname "$ifc" ifname
				config_get aliases "$ifc" aliases
				uci_revert_state network "$ifc"
				[ -n "$device" ] && uci_set_state network "$ifc" device "$device"
				[ -n "$ifname" ] && uci_set_state network "$ifc" ifname "$ifname"
				[ -n "$aliases" ] && uci_set_state network "$ifc" aliases "$aliases"
			}
		;;
		renew)
			setup_interface update
		;;
		bound)
			setup_interface ifup
		;;
	esac
done

# user rules
[ -f /etc/udhcpc.user ] && . /etc/udhcpc.user

exit 0