#!/bin/sh

# copyright 2004 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

. /etc/lessdisks/mkinitrd/initrd-netboot.conf
if [ -r "$cmdline_opts" ]; then
  . "$cmdline_opts"
fi

case $autoconf_type in
  off|none) echo "not configuring ip address"
  exit 0 ;;
  dhcp|bootp|both) ;;
  *) ;;
esac

if [ -z "$(/sbin/ifconfig eth0)" ]; then
  echo "not configuring DHCP, no ethernet card found..."
  exit 10
fi

network_script=/etc/lessdisks/mkinitrd/network_script

getDhclient() {
  /sbin/dhclient

  # kill dhclient so the initrd can get un-mounted
  killall dhclient 2> /dev/null || killall dhclient-2.2.x 2> /dev/null
}

getUdhcpc() {
  /sbin/udhcpc --foreground --quit --script=$network_script
}

getCmdline() {
  . "$cmdline_opts"
  test -z "$interface" && interface="eth0"
  export interface
  if [ "$1" = "runscript" ]; then
    $network_script bound
  fi
}

getCmdline

if [ -n "$ip" ]; then
  getCmdline runscript
else
  case $autoconf_type in
    dhcp|bootp) 
    if [ -x /sbin/udhcpc ]; then
      echo "attempting to configure DHCP with udhcpc"
      getUdhcpc
    elif [ -x /sbin/dhclient ]; then
      echo "attempting to configure DHCP with dhclient"
      getDhclient
    else
      echo "no DHCP client found, attempting to get values from /proc/cmdline"
      getCmdline runscript
    fi
    ;;
    *) getCmdline runscript ;;
  esac
fi
