#!/bin/ash
#
# network	stop, restart, start, the networking

if test -f /etc/sysconfig/network_cfg ; then 
	. /etc/sysconfig/network_cfg
else
	echo "/etc/sysconfig/network file not found!!!"
	echo "Major Error, giving up"
	exit -1
fi

if test "$NETWORKING" != "yes" ; then 
	echo "NETWORKING turned off..networking functions not enabled"	
	exit 0 
fi 

ifup_function ()
{
    if test "$BOOTPROTO" = "dhcp"  ; then
        if test -f /sbin/udhcpc  ; then
            # Try to use busy box's dhcp client
            echo -n "Determining IP information for $DEVICE...."
	    insmod af_packet >/dev/null 2>&1
            if /sbin/udhcpc -H $HOSTNAME -b -i $DEVICE ; then
                echo " done...."
            else
                echo "failed...."
            fi
        else
            # We don't have a client to get an ip
            echo "Failed to find a client to obtain ip number...."
        fi

    else

        ifconfig ${DEVICE} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}
    fi
}

ifdown_function () 
{
  pid=`pidof udhcpc`
  if [ "$pid" != "" ]; then
  	kill $pid
  fi 
  ifconfig ${DEVICE} down 
  echo  "Shutting down device ${DEVICE}"
}

case "$1" in 
   start)
   
	# Setting the hostname
	echo "Setting hostname...."
	hostname $HOSTNAME
	
	for i in /etc/sysconfig/ifcfg-*
	do
	 . $i
	 #dhcp can leave stale pid files laying around
	    
	 if test "${ENABLE}" = "yes"  ; then
	   echo "Bringing up interface ${DEVICE}.."
           ifup_function
	 fi
	done
     	echo "setting kernel IP routing tables"
        route add default gateway $GATEWAY dev $GW_DEV
	route add -net 127.0.0.0 netmask 255.0.0.0 dev lo

   ;;
   stop)
    for DEVICE in $(sed -ne'/:/p' /proc/net/dev | cut -d: -f1 )
     do
	ifdown_function
     done
	;;

   restart)
	$0 stop	
	$0 start
	;;
   *)
   	echo "usage: start|stop|restart "
	;;
   esac
   
	
