#!/bin/sh

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

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

RESOLV_CONF="/etc/resolv.conf"

if [ -n "$broadcast" ] ; then
    BROADCAST="broadcast $broadcast"
else
    BROADCAST="broadcast +"
fi

[ -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\)$"
}

have_bin_ip=0
if command -v ip; then
    have_bin_ip=1
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
                ip addr flush dev $interface
                ip link set dev $interface up
            else
                /sbin/ifconfig $interface 0.0.0.0
            fi
        fi
        ;;

    renew|bound)
        if [ $have_bin_ip -eq 1 ]; then
            ip addr add dev $interface local $ip/$mask $BROADCAST
            [ "$interface" = "wwan0" ] && ip link set dev $interface mtu ${MTU_WWAN}
            [ "$interface" = "wwan1" ] && ip link set dev $interface mtu ${MTU_WWAN}
            [ "$interface" = "ppp0" ] && ip link set dev $interface mtu ${MTU_WWAN}
        else
            /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
            [ "$interface" = "wwan0" ] && /sbin/ifconfig $interface mtu ${MTU_WWAN}
            [ "$interface" = "wwan1" ] && /sbin/ifconfig $interface mtu ${MTU_WWAN}
            [ "$interface" = "ppp0" ] && /sbin/ifconfig $interface mtu ${MTU_WWAN}
        fi

        if [ -n "$router" ] ;
	metric=0
	then
		if [ $have_bin_ip -eq 1 ];
		then
			ip route | grep "^default"
	                default_route=$?
	        else
        	        route -n | grep "^0.0.0.0"
                	default_route=$?
	        fi

        	# Update the default route only if there is no already existing default route.
	        if [ "default_route" != 0 ]
	        then
        		for i in $router ;
			do
	                if [ $have_bin_ip -eq 1 ];
			then
                		ip route add default via $i dev $interface metric $metric
				Log "##### ip route add default via $i dev $interface metric $metric"
	                else
    		    	        route add default gw $i dev $interface metric $metric 2>/dev/null
				Log "##### route add default gw $i dev $interface metric $metric 2>/dev/null"
	                fi
        	        metric=$(($metric + 1))
        		done
	        fi
            	# Create a temp gwip file for a specific interface
        	echo $router > "/run/gwip."$interface
        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
		# Update the DNS configuration only if the resolv.conf file is not present or empty
                if [ ! -s /etc/resolv.conf ] || [ ! -e /etc/resolv.conf ]
                then
                        echo -n "$R" > "$RESOLV_CONF"
                fi
                # Create a temp resolv.conf file for a specific interface
                echo -n "$R" > "/run/resolv.conf".$interface
        fi
        ;;
esac

exit 0
