#!/bin/sh

PATH=/bin:/usr/bin:/sbin:/usr/sbin

RESOLV_CONF="/etc/resolv.conf"

UDHCPC_INFO="/etc/udhcpc.info"

update_interface()
{
  [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
  [ -n "$subnet" ] && NETMASK="netmask $subnet"
  /bin/ifconfig $interface $ip $BROADCAST $NETMASK
}

update_routes()
{
  if [ -n "$router" ]
  then
    echo "deleting routes"
    while /bin/route del default gw 0.0.0.0 dev $interface > /dev/null 2>&1
    do :
    done

    for i in $router
    do
      /bin/route add default gw $i dev $interface
    done
  fi
}

update_dns()
{
  echo -n > $RESOLV_CONF
  [ -n "$domain" ] && echo domain $domain >> $RESOLV_CONF
  for i in $dns
  do
    echo adding dns $i
    echo nameserver $i >> $RESOLV_CONF
  done
}

deconfig()
{
  /bin/ifconfig $interface 0.0.0.0
}

update_udhcpc_info()
{
    cat > $UDHCPC_INFO <<EOF
HOSTNAME=$hostname
INTERFACE=$interface
IP=$ip
BROADCAST=$broadcast
NETMASK=$netmask
ROOTPATH=$rootpath
EOF
}

case "$1" in
  bound)
    update_interface;
    update_routes;
    update_dns;
    update_udhcpc_info;
  ;;

  renew)
    update_interface;
    update_routes;
    update_dns;
    update_udhcpc_info;
  ;;

  deconfig)
    deconfig;
  ;;

  *)
    echo "Usage: $0 {bound|renew|deconfig}"
    exit 1
    ;;
esac

exit 0
