#!/bin/sh

# Copyright 2004 Jonas Smedegaard <dr@jones.dk>, distributed under the terms of
# the GNU General Public License version 2 or any later version.

# many modifications and abominations made by vagrant@freegeek.org

# udhcpc client script
# - udhcp will have set various env variables according to the new state of
#   the interface
# - NOTE: udhcpc doesn't actually configure the interface
# - NOTE: some options are very similar (cf. bootfile, boot_file !)
#   - udhcpc only requests a select few options: option-{1,3,6,12,51,53,54}
#     = {subnet mask, router, dns, hostname, dhcp msg type, serverid, lease t.}
#       see http://www.freesoft.org/CIE/RFC/2131/8.htm for bootp/dhcp format
#   - ISC DHCP options => udhcp script env variables:
#     - filename => boot_file
#     - dhcp-server-identifier => serverid (i.e. DHCP server address)
#     - next-server => siaddr (NOTE: v0.9.6 has a bug siaddr==yiaddr)

if [ -n "$ip" ]; then
  dhcp_ip="$ip"
fi

if [ -z "$cmdline_opts" ]; then
  cmdline_opts=/tmp/cmdline_opts.conf
fi

if [ -r "$cmdline_opts" ]; then
  . "$cmdline_opts"
fi

if [ -n "$dhcp_ip" ]; then
  ip=$dhcp_ip
fi

case "$1" in

    deconfig)   echo "$0: $interface: deconfig"

                # i/f up, but deconfigured
                ifconfig "$interface" 0.0.0.0
    ;;

    bound)              echo "$0: $interface: bound to $ip"

                ifconfig "$interface" "$ip" \
                    ${subnet:+netmask $subnet} \
                    ${broadcast:+broadcast $broadcast}

                if [ -n "$router" ]; then
                  route add default gw "$router"
                fi

                # set hostname, may be echoed back to the server in the
                # client's dhclient script (so as to get added to lease file)
                [ "$hostname" ] && hostname "$hostname"

                # udhcp 0.9.6 bug: siaddr incorrectly == ip
                if [ "$siaddr" = "$ip" ]; then
                  # assume that the dhcp server is same as next-server
                  echo "WARNING: udhcp bug workaround, setting dhcp server to next-server ($serverid)"
                  siaddr="$serverid"
                fi

                # strip off the filename and directory to get the rootpath
                # i.e. grab /var/lib/lessdisks out of /var/lib/lessdisks/boot/vmlinuz
                if [ -z "$nfspath" ]; then
                  nfspath="${boot_file%/*/*}"
                  if [ -z "$nfspath" ]; then
                    echo "WARNING: assuming default lessdisks NFS root"
                    nfspath="/var/lib/lessdisks"
                  fi
                fi
                if [ -n "$interface" ]; then
                  echo "interface=\"$interface\"" >> $cmdline_opts
                fi
                if [ -n "$siaddr" ]; then
                  echo "siaddr=\"$siaddr\"" >> $cmdline_opts
                fi
                if [ -n "$serverid" ]; then
                  echo "serverid=\"$serverid\"" >>  $cmdline_opts
                fi
                if [ -n "$tftp" ]; then
                  echo "tftp=\"$tftp\"" >> $cmdline_opts
                fi
                if [ -n "$nfspath" ]; then
                  echo "nfspath=\"$nfspath\"" >> $cmdline_opts
                fi
                if [ -n "$boot_file" ]; then
                  echo "boot_file=\"$boot_file\"" >> $cmdline_opts
                fi
    ;;
    
    renew)              echo "$0: $interface: renew $ip";
    ;;

    nak)                echo "$0: $interface: nak: $message"
    ;;

    *)                  echo "$0: huh? unknown udhcpc action: [$1]"
    ;;
esac
