#!/bin/sh
# Configure network via DHCP before NFS root mount. Polls for an Ethernet
# interface with sleep 1 per iteration to allow the Ethernet driver deferred
# probe chain to settle after udev loads the modules.

netsetup_enabled() {
    [ "${bootparam_root}" = "/dev/nfs" ] || return 1
    return 0
}

netsetup_run() {
    local iface timeout devtype

    # Extract interface from ip= kernel param (format: client:server:gw:mask:host:device:autoconf)
    iface=""
    case "${bootparam_ip}" in
        dhcp|on|any|"")
            ;;
        *)
            iface=$(echo "${bootparam_ip}" | cut -d: -f6)
            ;;
    esac

    udevadm trigger --action=add --subsystem-match=net
    udevadm settle --timeout=10

    # Poll up to 60 seconds for an Ethernet interface (ARPHRD_ETHER = type 1).
    msg "netsetup: waiting for ethernet interface..."
    timeout=60
    while [ "${timeout}" -gt 0 ]; do
        if [ -n "${iface}" ] && [ -d "/sys/class/net/${iface}" ]; then
            break
        fi
        for dev in /sys/class/net/*; do
            [ -f "${dev}/type" ] || continue
            devtype=$(cat "${dev}/type")
            [ "${devtype}" = "1" ] || continue
            iface=$(basename "${dev}")
            break
        done
        [ -n "${iface}" ] && [ -d "/sys/class/net/${iface}" ] && break
        sleep 1
        timeout=$((timeout - 1))
    done

    if [ -z "${iface}" ] || [ ! -d "/sys/class/net/${iface}" ]; then
        msg "netsetup: no ethernet interface found after 60s, skipping DHCP"
        return
    fi

    msg "netsetup: configuring ${iface} via DHCP"

    udhcpc -q -i "${iface}"
}
