#!/bin/sh

# Stop network interface(s) using previously configured init script
/etc/rc.d/init.d/S20network stop

# Begin creating new network init script reflecting configured parameters
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

# Disable DSX watchdog during this network interface configuration
dsxwdog -1 &

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

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

END_DELIMITER

##############################################
# record the configured network parameters   #
# as variables in the network init script    #
#                                            #
# 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 "ip_if=eth0" >> /etc/rc.d/init.d/S20network
echo "voip_if=eth1" >> /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

# Finish the creation of the network init script
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)

	# If no IP or Subnet specified, make sure that defaults are used
	if [ "$ip" = "0.0.0.0" ]; then
		ip=""
	fi
	if [ "$subnet" = "0.0.0.0" ]; then
		subnet="255.255.255.0"
	fi

	# If we're set for DHCP with no IP specified, but we have a previous configuration, get the IP and Subnet from that config
	if [ "$ip" = "" -a "$dhcp" != "0" ]; then
		if [ -e "$IP_CONF" ]; then
			read dummy
			read ip
			read subnet
		fi < $IP_CONF
	fi

	# Determine the IP network specified by IP and SUBNET
	# Break out IP address octets
	ipbin1=`echo $ip | awk 'BEGIN{FS="."}{print $1}'`
	ipbin2=`echo $ip | awk 'BEGIN{FS="."}{print $2}'`
	ipbin3=`echo $ip | awk 'BEGIN{FS="."}{print $3}'`
	ipbin4=`echo $ip | awk 'BEGIN{FS="."}{print $4}'`
	# Break out subnet octets
	nmbin1=`echo $subnet | awk 'BEGIN{FS="."}{print $1}'`
	nmbin2=`echo $subnet | awk 'BEGIN{FS="."}{print $2}'`
	nmbin3=`echo $subnet | awk 'BEGIN{FS="."}{print $3}'`
	nmbin4=`echo $subnet | awk 'BEGIN{FS="."}{print $4}'`
	# Network is determined by masking IP address with net mask
	ip_network="$((ipbin1 & nmbin1)).$((ipbin2 & nmbin2)).$((ipbin3 & nmbin3)).$((ipbin4 & nmbin4))"

	# Choose VOIP gateway IP address making sure to avoid the IP network
	voip_ip="10.0.0.1"
	if [ "$ip_network" = "10.0.0.0" ]; then
		voip_ip="10.254.254.1"
	fi
    
	# Network interface start up
	# Record IP configuration
	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

	# Calculate cidr representation of subnet i.e. 255.255.255.0 = /24
	/usr/bin/dsxsubnet $subnet
	cidr=$?

	# Deconfigure the network interface(s)
	/sbin/ifconfig $ip_if 0.0.0.0
	/sbin/ifconfig $voip_if 0.0.0.0
	
	# If VOIP DB is installed, create the ethernet bridge (between the two interfaces)
	# and specify that the interface to be used by DHCP is br0 (instead of eth0)
	if [ "$voipdb" -eq 1 ]
	then
		echo "Setting up bridge br0"
		
		interface=br0
		/sbin/brctl addbr br0
		/sbin/brctl addif br0 $ip_if
		/sbin/brctl addif br0 $voip_if
		sleep 1
		/bin/ip link set br0 up
		sleep 1
	fi

	# Configure the loop back interface
	echo "Setting up link for loopback"
	/sbin/ifconfig lo 127.0.0.1

	# Configure the VOIP Interface using the chosen VOIP gateway IP address
	echo "Setting up link for $voip_if"
	/sbin/ifconfig $voip_if $voip_ip netmask 255.255.255.0

	# Initiailize the ethernet bridging table
	if [ "$voipdb" -eq 1 ]
	then
		/sbin/rules_table.sh
	fi
	
	# Standard DHCP mode
	if [ "$dhcp" = "1" ]; then
	
		# Startup DHCP service specifying the preferred IP address (if provided)
		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
		
	# DHCP with manual IP
	elif [ "$dhcp" = "2" ]; then
	
		# Startup DHCP service - we will discard whatever IP address is assigned and use the manually configured onec
		echo "Setting up DHCP2 for $interface"
		udhcpc -R -h NEC-DSX -s $DHCP_SCRIPT2 -i $interface &
		exit 0
		
	fi

	# Manually configure the interface
	# Build new DNS resolv.conf
	echo -n > $RESOLV_TEMP
	[ -n "$domain" ] && echo search $domain >> $RESOLV_TEMP
	[ -n "$dns1" ] && echo nameserver $dns1>> $RESOLV_TEMP
	[ -n "$dns2" ] && echo nameserver $dns2>> $RESOLV_TEMP
	mv -f $RESOLV_TEMP $RESOLV_CONF
	
	# If VOIP DB is installed, configure the ethernet bridge
	if [ "$voipdb" -eq 1 ]
	then
		/bin/ip addr add $ip/$cidr brd + dev br0
	fi
	
	# Configure the IP Interface
	echo "Setting up link for $ip_if"
	[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
	[ -n "$subnet" ] && NETMASK="netmask $subnet"
	/sbin/ifconfig $ip_if $ip $BROADCAST $NETMASK
		
	# Configure routing table with default gw
	if [ "$router" != "" ]; then
		# Remove any previous default gw
		while route del default gw 0.0.0.0 ; do
			:
		done

		# Install new default gw
		route add default gw $router
	fi

	;;

    stop)
    
	# Network interface shut down
	# If dhcp is enabled, send signal to release IP lease
	if [ "$dhcp" != "0" ]; then
		killall -12 udhcpc
		sleep 1
	fi
	
	# Terminate the DHCP service
	killall udhcpc
	
	# Deconfigure the network interface(s)
	echo "Shutting down link for $ip_if and $voip_if"
	/sbin/ifconfig $ip_if 0.0.0.0
	/sbin/ifconfig $voip_if 0.0.0.0

	# If VOIP DB installed, shut down the ethernet bridge
	if [ "$voipdb" -eq 1 ]
	then
		# Deconfigure the ethernet bridge
		echo "Shutting down br0"
		/bin/ip link set br0 down
		/sbin/brctl delif br0 $ip_if
		/sbin/brctl delif br0 $voip_if
		/sbin/brctl delbr br0

		# Flush any bridging rules
		echo "Flushing all bridging rules"
		ebtables -F
		ebtables -t nat -F
		ebtables -t broute -F
		ebtables --init-table
	fi

	;;

    restart)
	
	# Network interface restart
	# Shut down interface(s), wait a second and start interface(s) back up
	$0 stop
	sleep 1
	$0 start
	;;

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

esac

exit 0
END_DELIMITER1

# Start up the network interface(s) using the newly created init script
/etc/rc.d/init.d/S20network start
