#!/bin/sh
# 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)

leases_file=/var/lib/dhcp/dhclient.leases

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" 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 "udhcp bug workaround: setting dhcp server to next-server ($serverid)"
                  siaddr="$serverid"
                fi

                # ok, write env to file for others to source...
                #set > $UDHCPENV

                # 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
                    nfspath="/var/lib/lessdisks"
                  fi
                fi
                # create a leases file similar to what dhclient expects...
                echo "lease {" >> $leases_file
                if [ -n "$interface" ]; then
                  echo "  interface \"$interface\";" >> $leases_file
                fi
                if [ -n "$siaddr" ]; then
                  echo "  option next-server $siaddr;" >> $leases_file
                fi
                if [ -n "$serverid" ]; then
                  echo "  option dhcp-server-identifier $serverid;" >> $leases_file
                fi
                if [ -n "$tftp" ]; then
                  echo "  option tftp-server-name $tftp;" >> $leases_file
                fi
                if [ -n "$nfspath" ]; then
                  echo "  option root-path \"$nfspath\";" >> $leases_file
                fi
                if [ -n "$boot_file" ]; then
                  echo "  filename \"$boot_file\";" >> $leases_file
                fi
                echo "}" >> $leases_file
    ;;
    
    renew)              echo "$0: $interface: renew $ip";
    ;;

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

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