#!/bin/sh

#
# parameters: start, stop, restart
#

INTF=eth0

stop_intf()
{
  ifconfig ${INTF} down
  killall -TERM udhcpc > /dev/null 2>&1
}

start_intf()
{
  DHCP=$(teste -C dhcp_client)

  if [ ${DHCP} -ne 0 ] ; then
    udhcpc -s /bin/handle_udhcpc.sh &
    return
  fi

  IP=$(teste -C eth_ip)
  MASK=$(teste -C eth_mask)
  BROADCAST=$(ipcalc -b ${IP} ${MASK} | cut -d = -f 2)

  ifconfig ${INTF} ${IP} netmask ${MASK} broadcast ${BROADCAST} up

}

case ${1} in
start)
  start_intf
  ;;
stop)
  stop_intf
  ;;
restart)
  stop_intf
  start_intf
  ;;
*)
  echo "${0}: ERROR - invalid option '${1}'"
  ;;
esac

