#!/bin/sh
  
# copyright 2004-2005 Vagrant Cascadian <vagrant@freegeek.org>, distributed
# under the terms of the GNU General Public License version 2 or any later
# version.

# attempts to emulate nfsroot behaviour from linux kernel 2.4.26
# Documentation/nfsroot.txt

# TODO support "rarp" protocol
  
case $autoconf_type in
  off|none) echo "not configuring ip address"
    config_ip="false" ;;
  dhcp|bootp|both) config_ip="true" ;;
  rarp) echo "WARNING: rarp protocol is unsupported, trying defaults..."
    config_ip="true" ;;
  # probably manually specified network settings
  *) config_ip="true" ;;
esac
 
if [ "true" = "$config_ip" ]; then
  
  network_script=${network_script-"/etc/initrd-netboot/network_script"}
  
  getUdhcpc() {
    /sbin/udhcpc --foreground --quit --script=$network_script --clientid="$dhcp_client_id"
  }
  
  getCmdline() {
    . "$cmdline_opts"
    test -z "$interface" && interface="eth0"
    export interface
  }
  
  runCmdline() {
    getCmdline
    $network_script bound
  }
  
  # set a default client id
  test -z "$dhcp_client_id" && dhcp_client_id="initrd-netboot"
  
  if [ -n "$ip" ]; then
    runCmdline
  else
    case $autoconf_type in
      dhcp|bootp) 
      if [ -x /sbin/udhcpc ]; then
        echo "attempting to configure DHCP with udhcpc"
        getUdhcpc
      else
        echo "WARNING: no DHCP client found, attempting to get values from /proc/cmdline"
        runCmdline
      fi
      ;;
      *) runCmdline ;;
    esac
  fi
  
  # re-get commandline values, as network_script may add new values
  getCmdline
fi
