#!/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

dsxwdog -1 &

#######################################
##### 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"

END_DELIMITER

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

echo "interface=eth0" >> /etc/rc.d/init.d/S20network
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

/usr/bin/check_voipdb
voipdb=$?

case "$1" in

    start)
    
	echo -n > $IP_CONF
	echo $dhcp >> $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
	
	/usr/bin/dsxsubnet $subnet
	cidr=$?

	/sbin/ifconfig eth0 0.0.0.0
	/sbin/ifconfig eth1 0.0.0.0
	
	if [ "$voipdb" -eq 1 ]
	then
		echo "Setting up bridge br0"
		
		interface=br0
		/sbin/brctl addbr br0
		/sbin/brctl addif br0 eth0
		/sbin/brctl addif br0 eth1
		sleep 1
		/bin/ip link set br0 up
		sleep 1
	fi

	echo "Setting up link for eth1"
	/sbin/ifconfig eth1 10.254.254.1 netmask 255.255.255.0
	
	echo "Setting up link for loopback"
	/sbin/ifconfig lo 127.0.0.1
	
	if [ "$voipdb" -eq 1 ]
	then
		/sbin/rules_table.sh
	fi
	
	if [ "$dhcp" = "1" ]; then
	
		echo "Setting up DHCP1 for eth0"
		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 eth0"
		udhcpc -h NEC-DSX -s $DHCP_SCRIPT2 -i $interface &
		exit 0
		
	fi

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

	if [ "$voipdb" -eq 1 ]
	then
		/bin/ip addr add $ip/$cidr brd + dev br0
	fi
	
	echo "Setting up link for eth0"
	/sbin/ifconfig eth0 $ip $BROADCAST $NETMASK
		
	if [ "$router" != "" ]; then
		while route del default gw 0.0.0.0 ; do
			:
		done

		route add default gw $router
	fi

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

    stop)
    
	if [ "$dhcp" != "0" ]; then
		killall -12 udhcpc
		sleep 1
		
	else
		echo "Shutting down link for eth0 and eth1"
		/sbin/ifconfig eth0 0.0.0.0
		/sbin/ifconfig eth1 0.0.0.0
	fi
	
	killall udhcpc
		
	echo "Shutting down br0"
	/bin/ip link set br0 down
	/sbin/brctl delif br0 eth0
	/sbin/brctl delif br0 eth1
	/sbin/brctl delbr br0
	
	ebtables -F
	ebtables -t nat -F
	ebtables -t broute -F
	ebtables --init-table


	;;

    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
