#!/bin/sh

# This script exists so that when ifup runs udhcpc, we can control the
# command-line options used.  The options to udhcpc are hard-coded in the
# source code for ifup (busybox).  Fortunately, the path to udhcpc is not, so
# we rely on /bin (where this script lives) coming before /sbin/udhcpc (the
# real one) in the execution path used by ifup, so this is the one that gets
# run.

#echo "$@" > /dev/console
#echo $1 $2 $3 $4 $5 $6 > /dev/console

# get the interface (e.g. usb0) which we assume is the last argument
eval interface=\${$#}
#echo $interface > /dev/console

if [ $interface = usb0 ] ; then
  # usb device port; connecting to PC or main (remote software update)
  exec /sbin/udhcpc -R             -p /var/run/udhcpc.${interface}.pid -i ${interface}
else
  # eth0 or wlan0
  # Some DHCP servers ARP for an address before they OFFER it, with a 1 second timeout,
  # so we should probably should wait a little longer than that before retrying.
  exec /sbin/udhcpc -R -T2 -A3 -t8 -p /var/run/udhcpc.${interface}.pid -i ${interface}
fi

# pre-4.2 version
#exec /sbin/udhcpc -R         -p /var/run/udhcpc.${interface}.pid -i ${interface}
# 4.2 version:
#exec /sbin/udhcpc -R -T1 -A2 -p /var/run/udhcpc.${interface}.pid -i ${interface}

#exec /sbin/udhcpc -R -t8 -T1 -A4 -p /var/run/udhcpc.${interface}.pid -i ${interface}
#exec /sbin/udhcpc -S -R -t8 -T1 -A4 -p /var/run/udhcpc.${interface}.pid -i ${interface}
#        -t,--retries=N          Send up to N request packets
#        -T,--timeout=N          Try to get a lease for N seconds (default 3)
#        -A,--tryagain=N         Wait N seconds (default 20) after failure
#        -O,--request-option=OPT Request DHCP option OPT (cumulative)
#        -o,--no-default-options Do not request any options (unless -O is also given)
#        -n,--now        Exit with failure if lease is not immediately obtained
#        -R,--release    Release IP on quit
#        -a,--arping     Use arping to validate offered address
#        -S,--syslog     Log to syslog too

# hard-coded in busybox's version of ifup:
# udhcpc -R -n -p /var/run/udhcpc.usb0.pid -i usb0

