#! /bin/sh

echo "Starting /etc/dhclient-exit-hooks ($reason) at `date`"

if [ -x /usr/bin/logger ]; then
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
else
	LOGGER=echo
fi

$LOGGER "$0 invoked with reason $reason"
# $LOGGER "Displaying ipfw rules at $0 invocation:"
# /sbin/ipfw list | ${LOGGER}
  
case ${reason} in
BOUND|REBIND|REBOOT)
  fw_args="-i $new_ip_address -d $new_dhcp_server_identifier -m $new_subnet_mask -g $new_routers -b $new_broadcast_address"
  if [ -r "/etc/rc.firewall-post-dhcp" ]; then
    $LOGGER "$0 invoking /etc/rc.firewall-post-dhcp $fw_args"
    /bin/sh /etc/rc.firewall-post-dhcp $fw_args
  else
    $LOGGER "$0 failed to invoke /etc/rc.firewall-post-dhcp $fw_args"
  fi
  current_hostname=`/bin/hostname`
  $LOGGER "Current Hostname: $current_hostname"
  if [ -z "${current_hostname}" ]; then
    current_domain=""
  else
    current_domain=`echo "$current_hostname" | /usr/bin/sed -e 's/^[^\.]*\.//'`
    if ! /usr/bin/egrep '^[ 	]*(search|domain)[ 	]' /etc/resolv.conf >/dev/null; then
      echo "search ${current_domain}" >>/etc/resolv.conf
    fi
    if ! grep "$current_domain" /etc/resolv.conf >/dev/null; then
      current_hostname=""
    fi
  fi
  if [ -z "${current_hostname} ] || [ "${current_hostname} = "localhost" ]; then
    new_host_name=`host $new_ip_address | /usr/bin/sed -e 's/^.*domain name pointer //'`
    $LOGGER "New Hostname: $new_host_name"
    /bin/hostname $new_host_name
  else
    $LOGGER "Did not set new_host_name; using $current_hostname"
  fi
#  $LOGGER "New time-server: $new_time_server"
#  $LOGGER "New dhcp-server-identifier: $new_dhcp_server_identifier"
  ntp_server="${new_time_server:=${new_dhcp_server_identifier}}"
  [ -f /var/run/ntpd.pid ] && \
  $LOGGER "ntpd appears to be running; skipping ntp startup" || \
  (($LOGGER "Using ntp_server: $ntp_server" && echo "server ${ntp_server}" >/etc/ntp.conf && ntpd -g -p /var/run/ntpd.pid) || \
  $LOGGER "Failed to use ntp_server: $ntp_server")
  ;;

EXPIRE|FAIL|TIMEOUT)
  new_host_name="localhost"
  $LOGGER "New Hostname: $new_host_name"
  /bin/hostname $new_host_name
  if [ -r "/etc/rc.firewall-pre-dhcp" ]; then
    . /etc/rc.firewall-pre-dhcp
  fi
  ;;

*)
  $LOGGER "reason was $reason; no action taken"
  ;;
esac
#echo "Ending /etc/dhclient-exit-hooks ($reason) at `date`"
