#!/bin/sh
/etc/rc.d/init.d/S20network stop

cat <<-END_DELIMITER > /etc/rc.d/init.d/S20network
#!/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
END_DELIMITER

##############################################
# dhcp option specifies type of dhcp         #
# 0 - dhcp disabled                          #
# 1 - dhcp enabled                           #
# 2 - dhcp enabled, using static IP address  #
##############################################

echo "hostname NEC-DSX" >> /etc/rc.d/init.d/S20network
echo "dhcp=$1" >> /etc/rc.d/init.d/S20network
echo "ip=$2" >> /etc/rc.d/init.d/S20network
echo "subnet=$3" >> /etc/rc.d/init.d/S20network
echo "router=$4" >> /etc/rc.d/init.d/S20network
echo "dns1=$5" >> /etc/rc.d/init.d/S20network
echo "dns2=$6" >> /etc/rc.d/init.d/S20network

cat <<-'END_DELIMITER1'>> /etc/rc.d/init.d/S20network

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"
		if [ "$ip" != "" ]; then
			udhcpc -h NEC-DSX -r $ip -s $DHCP_SCRIPT1 -i $interface &
		else
			udhcpc -h NEC-DSX -s $DHCP_SCRIPT1 -i $interface &
		fi
		
		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
		sleep 1
		
	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
END_DELIMITER1

/etc/rc.d/init.d/S20network start
