#!/bin/sh

set -e

# reduced dhclient-script for the Debian installer
# changes by Joshua Kwan <joshk@triplehelix.org>,
# Bastian Blank <waldi@debian.org>

# dhclient-script for Linux. Dan Halbert, March, 1997.
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
# Modified for Debian.  Matt Zimmerman and Eloy Paris, December 2003

# update /etc/resolv.conf based on received values
make_resolv_conf() {
    local new_resolv_conf

    # DHCPv4
    if [ -n "$new_domain_name" ] || [ -n "$new_domain_name_servers" ]; then
        new_resolv_conf=/etc/resolv.conf.dhclient-new
        rm -f $new_resolv_conf

        if [ -n "$new_domain_name" ]; then
            echo "search $new_domain_name" >>$new_resolv_conf
        fi

        if [ -n "$new_domain_name_servers" ]; then
            for nameserver in $new_domain_name_servers; do
                echo "nameserver $nameserver" >>$new_resolv_conf
            done
        else # keep 'old' nameservers
            grep -i '^nameserver' /etc/resolv.conf >>$new_resolv_conf
        fi

        mv $new_resolv_conf /etc/resolv.conf
    # DHCPv6
    elif [ -n "$new_dhcp6_domain_search" ] || [ -n "$new_dhcp6_name_servers" ]; then
        new_resolv_conf=/etc/resolv.conf.dhclient-new
        rm -f $new_resolv_conf

        if [ -n "$new_dhcp6_domain_search" ]; then
            echo "search $new_dhcp6_domain_search" >> $new_resolv_conf
        fi

        if [ -n "$new_dhcp6_name_servers" ]; then
            for nameserver in $new_dhcp6_name_servers; do
                echo "nameserver $nameserver" >>$new_resolv_conf
            done
        else # keep 'old' nameservers
            grep -i '^nameserver' /etc/resolv.conf >>$new_resolv_conf
        fi

        mv $new_resolv_conf /etc/resolv.conf
    fi
}

set_hostname() {
    local current_hostname
    current_hostname=$(cat /proc/sys/kernel/hostname)

    if [ -z "$current_hostname" ] || [ "$current_hostname" = "(none)" ]; then
        echo "$new_host_name" > /proc/sys/kernel/hostname
    fi
}

# set up some variables for DHCP handlers below
if [ -n "$new_subnet_mask" ]; then
    new_mask="/$(ptom $new_subnet_mask)"
fi
if [ -n "$old_subnet_mask" ]; then
    old_mask="/$(ptom $old_subnet_mask)"
fi

if [ -n "$new_broadcast_address" ]; then
    new_broadcast_arg="broadcast $new_broadcast_address"
fi
if [ -n "$old_broadcast_address" ]; then
    old_broadcast_arg="broadcast $old_broadcast_address"
fi

# Execute the operation
case "$reason" in

    ### DHCPv4 Handlers

    MEDIUM|ARPCHECK|ARPSEND)
        # Do nothing
        ;;
    PREINIT)
        ip link set dev $interface up

        # We need to give the kernel some time to get the interface up.
        sleep 1

        ;;

    BOUND|RENEW|REBIND|REBOOT)
        set_hostname

        if [ -n "$old_ip_address" ] &&
           [ "$old_ip_address" != "$new_ip_address" ]; then
            ip -4 addr flush dev $interface
        fi

        if [ -z "$old_ip_address" ] ||
           [ "$old_ip_address" != "$new_ip_address" ] ||
           [ "$reason" = "BOUND" ] || [ "$reason" = "REBOOT" ]; then
            ip -4 addr add $new_ip_address$new_mask $new_broadcast_arg dev $interface

            if [ -n "$new_interface_mtu" ]; then
                ip link set $interface mtu $new_interface_mtu || true
            fi

            for router in $new_routers; do
                ip -4 route add default via $router dev $interface
            done
        fi

        make_resolv_conf

        # Get the domain name into a file suitable for netcfg to read.
        printf "$new_domain_name" > /tmp/domain_name

        if [ -n "$new_ntp_servers" ]; then
            printf "$new_ntp_servers" > /tmp/dhcp-ntp-servers
        fi

        ;;

    EXPIRE|FAIL|RELEASE|STOP)
        if [ -n "$old_ip_address" ]; then
            ip -4 addr flush dev $interface
        fi

        ;;

    TIMEOUT)
        ip link set $interface down

        ;;

    ### DHCPv6 Handlers
    # TODO handle prefix change: ?based on ${old_ip6_prefix} and ${new_ip6_prefix}?

    PREINIT6)
        # ensure interface is up
        ip link set $interface up

        # flush any stale global permanent IPs from interface
        ip -6 addr flush dev $interface scope global permanent

        ;;

    BOUND6|RENEW6|REBIND6)
        if [ "$new_ip6_address" ] && [ "$new_ip6_prefixlen" ]; then
            ip -6 addr add $new_ip6_address/$new_ip6_prefixlen \
                dev $interface scope global
        fi

        make_resolv_conf

        # Get the domain name into a file suitable for netcfg to read.
        printf "${new_dhcp6_domain_search%% *}" > /tmp/domain_name

        ;;

    DEPREF6)
        if [ "$new_ip6_prefixlen" ]; then
            ip -6 addr change $cur_ip6_address/$cur_ip6_prefixlen \
                dev $interface scope global preferred_lft 0
        fi

        ;;

    EXPIRE6|RELEASE6|STOP6)
        if [ "$old_ip6_address" ] && [ "$old_ip6_prefixlen" ]; then
            ip -6 addr del $old_ip6_address/$old_ip6_prefixlen dev $interface
        fi

        ;;
esac

exit 0
