#!/bin/sh
# udhcpc script to use with ICD

LEASE_PARAMS=/var/run/dhcp-params.conf

case "$1" in

static | bound | renew)
	[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
	[ -n "$subnet" ] && NETMASK="netmask $subnet"

	if [ "$1" = renew ]; then
	    if [ -f $LEASE_PARAMS ]; then
		head -1 $LEASE_PARAMS | grep "^${interface} ${ip} ${router} ${subnet} ${domain} ${dns}$" > /dev/null
		if [ $? -eq 0 ]; then
		    exit 0
		fi
	    fi
	fi

	/sbin/ifconfig $interface $ip $BROADCAST $NETMASK

	if [ -n "$router" ]
	then
	    if netstat -rn | egrep "^0.0.0.0 *${router} .*${interface}$" > /dev/null
	    then
		:
	    else
	        while /sbin/route del default gw 0.0.0.0 dev $interface
		do
		  :
		done

		router_rev=""
		for i in $router
		do
		  router_rev="$i $router_rev"
		done

		succeeded=no
		for i in $router_rev
		do
		  if netstat -rn | egrep "^0.0.0.0 *${i} .*${interface}$" > /dev/null
		  then
		      succeeded=yes
		  else
		      /sbin/route add default gw $i dev $interface
		      if [ "$?" = 0 ]
		      then
			  succeeded=yes
		      fi
		  fi
		done

		if [ $succeeded != yes ]
		then
		    echo -n "Failed to set up default route for " >&2
		    echo "$interface: $ip/$subnet broadcast $broadcast" >&2
		fi

	    fi
	fi

	R=""
	if [ -n "$domain" ]
	then
		R="${R}domain $domain
"
	fi

	D=""
	for i in $dns
	do
		if [ -n "$i" ] && [ "$i" != "0.0.0.0" ]
		then
			D="${D}nameserver $i
"
		fi
	done

	# Let's just pick a default if none were provided... Quad9 seems better
	# than google and cloudflare at least.
	if [ -z "$D" ]
	then
		D="${D}nameserver 9.9.9.9
"
	fi
	R="${R}${D}"

	echo -n "$R" | /sbin/resolvconf -a "${interface}.udhcpc"

	# Clear dnsmasq cache after getting new nameservers
	kill -SIGHUP `pidof dnsmasq`

	/usr/bin/dbus-send \
	    --system \
	    --dest=com.nokia.icd \
	    /com/nokia/icd/autoconf \
	    com.nokia.icd.autoconf.autoconf_changed \
	    string:$interface string:DHCP string:CONNECTED

	if [ "$1" = bound ] || [ "$1" = renew ]; then
	    echo "${interface} ${ip} ${router} ${subnet} ${domain} ${dns}" > $LEASE_PARAMS
	fi
	;;
deconfig)
	/sbin/ifconfig $interface 0.0.0.0
	/sbin/resolvconf -d "$interface.udhcpc" || true
	rm -f $LEASE_PARAMS > /dev/null
	/usr/bin/dbus-send \
	    --system \
	    --dest=com.nokia.icd \
	    /com/nokia/icd/autoconf \
	    com.nokia.icd.autoconf.autoconf_changed \
	    string:$interface string:DHCP string:DISCONNECTED
	;;
zeroconf)
	/sbin/ifconfig $interface:1 \
	    $ip broadcast 169.254.255.255 netmask 255.255.0.0
	/sbin/route add -net 224.0.0.0 netmask 240.0.0.0 dev $interface:1
	/usr/bin/dbus-send \
	    --system \
	    --dest=com.nokia.icd \
	    /com/nokia/icd/autoconf \
	    com.nokia.icd.autoconf.autoconf_changed \
	    string:$interface string:LINKLOCAL string:CONNECTED
	;;
zeroconf.deconfig)
	/sbin/ifconfig $interface:1 $ip 0.0.0.0
	/sbin/route del -net 224.0.0.0 netmask 240.0.0.0 dev $interface:1
	/usr/bin/dbus-send \
	    --system \
	    --dest=com.nokia.icd \
	    /com/nokia/icd/autoconf \
	    com.nokia.icd.autoconf.autoconf_changed \
	    string:$interface string:LINKLOCAL string:DISCONNECTED
	;;
zeroconf.dhcpup)
	;;
esac

