#!/bin/sh
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh

setup_interface() {
    ip=$new_ip_address
    mtu=$new_interface_mtu
    mask=$new_subnet_mask
    bcast=$new_broadcast_address
    gw=${new_routers%%,*}
    domain=$new_domain_name
    search=$(printf "$new_domain_search")
    namesrv=$new_domain_name_servers
    hostname=$new_host_name

    [ -f /tmp/net.$netif.override ] && . /tmp/net.$netif.override

    # Taken from debian dhclient-script:
    # The 576 MTU is only used for X.25 and dialup connections
    # where the admin wants low latency.  Such a low MTU can cause
    # problems with UDP traffic, among other things.  As such,
    # disallow MTUs from 576 and below by default, so that broken
    # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
    if [ -n "$mtu" ] && [ $mtu -gt 576 ] ; then
        echo "if ! ip link set $netif mtu $mtu ; then"
        echo "ip link set $netif down"
        echo "ip link set $netif mtu $mtu"
        echo "ip link set $netif up"
        echo wait_for_if_up $netif
        echo "fi"
    fi > /tmp/net.$netif.up

    echo ip addr add $ip${mask:+/$mask} ${bcast:+broadcast $bcast} dev $netif >> /tmp/net.$netif.up

    [ -n "$gw" ] && echo ip route add default via $gw dev $netif > /tmp/net.$netif.gw

    [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net.$netif.resolv.conf
    if  [ -n "$namesrv" ] ; then
        for s in $namesrv; do
            echo nameserver $s
        done
    fi >> /tmp/net.$netif.resolv.conf

    # Note: hostname can be fqdn OR short hostname, so chop off any
    # trailing domain name and explicity add any domain if set.
    [ -n "$hostname" ] && echo "echo ${hostname%.$domain}${domain+.$domain} > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
}

PATH=/usr/sbin:/usr/bin:/sbin:/bin

export PS4="dhclient.$interface.$$ + "
exec >>/run/initramfs/loginit.pipe 2>>/run/initramfs/loginit.pipe
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh

# We already need a set netif here
netif=$interface

# Huh? Interface configured?
[ -f "/tmp/net.$netif.up" ] && exit 0

case $reason in
    PREINIT)
        echo "dhcp: PREINIT $netif up"
        ip link set $netif up
        wait_for_if_up $netif
        ;;
    BOUND)
        echo "dhcp: BOND setting $netif"
        if ! arping -q -D -c 2 -I $netif $new_ip_address ; then
            warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying"
            exit 1
        fi
        setup_interface
        set | while read line; do
            [ "${line#new_}" = "$line" ] && continue
            echo "$line"
        done >/tmp/dhclient.$netif.dhcpopts
        echo online > /sys/class/net/$netif/uevent

        if [ -e /tmp/net.$netif.manualup ]; then
            /sbin/netroot $netif -m
            rm -f /tmp/net.$netif.manualup
        else
            initqueue --onetime --name netroot-$netif netroot $netif
        fi
        ;;
    *) echo "dhcp: $reason";;
esac

exit 0
