#!/bin/sh

#
# /etc/rc.d/init.d/S20network - Start/Stop the network interface(s).
#

# Comment out the following exit line to enable this script.
# Before doing so, you need to edit the network address information below.
#exit 0

#######################################
##### network address information #####
#######################################

IP_CONF="/usr/share/udhcpc/ip.conf"
RESOLV_CONF="/etc/resolv.conf"
DHCP_SCRIPT1="/usr/share/udhcpc/dhcp1.script"
DHCP_SCRIPT2="/usr/share/udhcpc/dhcp2.script"

interface=eth0
hostname NEC-DSX
dhcp=0
ip=192.168.1.250
subnet=255.255.255.0
router=192.168.1.1
dns1=
dns2=

if [ ! -x /sbin/ifconfig ]; then
echo "$0: Missing /sbin/ifconfig"
exit 1
fi

case "$1" in

    start)
    
echo "Setting up link for loopback"
/sbin/ifconfig lo 127.0.0.1

echo -n > $IP_CONF
echo $ip >> $IP_CONF
echo $subnet >> $IP_CONF
echo $broadcast >> $IP_CONF
echo $router >> $IP_CONF
echo $domain >> $IP_CONF
echo $dns1 $dns2 >> $IP_CONF

if [ "$dhcp" = "1" ]; then
echo "Setting up DHCP1 for $interface"
udhcpc -h NEC-DSX -s $DHCP_SCRIPT1 -i $interface &
exit 0
elif [ "$dhcp" = "2" ]; then
echo "Setting up DHCP2 for $interface"
udhcpc -h NEC-DSX -s $DHCP_SCRIPT2 -i $interface &
exit 0
fi

echo "Setting up link for $interface"

[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"

/sbin/ifconfig $interface $ip $BROADCAST $NETMASK

if [ "$router" != "" ]; then
while route del default gw 0.0.0.0 dev $interface ; do
:
done

route add default gw $router dev $interface
fi

echo -n > $RESOLV_CONF
[ "$dns1" != "" ] && echo nameserver $dns1>> $RESOLV_CONF
[ "$dns2" != "" ] && echo nameserver $dns2>> $RESOLV_CONF

;;

    stop)
    
if [ "$dhcp" = "1" ]; then
killall -12 udhcpc

else
echo "Shutting down link for $interface"
/sbin/ifconfig $interface 0.0.0.0
fi

killall udhcpc

;;

    restart)
$0 stop
sleep 1
$0 start
;;

    *)
echo "Usage: $0 (start|stop|restart)"
exit 1
;;

esac

exit 0
