#!/bin/bash

# Bring up/down networking

. /etc/image_features
. /etc/sysconfig/rc.conf
. /etc/sysconfig/network.conf
. /bin/network-functions

CONF_PATH=/etc/fiad-conf/sysconfig/network-scripts

set_proc_values ()
{
	if [ "$FEATURE_NAT_ROUTER" = "1" ] ; then
	  # enable IP forwarding
	  echo "1" > /proc/sys/net/ipv4/ip_forward
	fi
}


do_start ()
{
	/bin/wan-detect
	. /var/run/interfaces
	DSPMG_IFACE_NAME=`grep DEVICE= $CONF_PATH/ifcfg-lan0_0 | cut -f 2 -d =`
	DSPSW_IFACE_NAME=`grep DEVICE= $CONF_PATH/ifcfg-dsp | cut -f 2 -d =`


	if [ ! -e $CONF_PATH/ifcfg-external ] ; then
		/bin/ln -s ifcfg-wan0 $CONF_PATH/ifcfg-external
	fi

	/etc/init.d/softdogd wait
	set_proc_values
	echo "  done"

	echo ""
	echo -n " interfaces: "
	if [ "$handle_lan0" = "true" ] ; then
		IPV6ADDR=""
		mac=`cat /sys/class/net/lan0/address`
		AUTOIPV6ADDR_LAN=fe80::$(printf %02x $((0x${mac%%:*} ^ 2)))
		mac=${mac#*:}
		AUTOIPV6ADDR_LAN=$AUTOIPV6ADDR_LAN${mac%:*:*:*}ff:fe
		mac=${mac#*:*:}
		AUTOIPV6ADDR_LAN=$AUTOIPV6ADDR_LAN${mac%:*}${mac##*:}
		AUTOIPV6ADDR_LAN=`echo $AUTOIPV6ADDR_LAN | sed -e 's/:0/:/g'`

		isremoveIPv6Address=false
		if [ "$IPV6_AUTO_CONFIG_LAN" = "false" ]; then
			. $CONF_PATH/ifcfg-lan0
			if [ "$IPV6ADDR" != "$AUTOIPV6ADDR_LAN/64" ] ; then
				isremoveIPv6Address=true
			fi
		fi
		echo -n "lan interface, "
		/bin/ifup $CONF_PATH/ifcfg-lan0
		echo -n "$DSPMG_IFACE_NAME, "
		/bin/ifup $CONF_PATH/ifcfg-lan0_0
		if [ "$FEATURE_MULTIDSP" = "1" ] ; then
			NUMBER_OF_CPU_CORES=`cat /proc/cpuinfo | grep processor | wc -l`
			for i in `seq 0 $(($NUMBER_OF_CPU_CORES-1))`; do
				echo -n "dsp$i, "
				/bin/ifup $CONF_PATH/ifcfg-dsp$i
			done    
		else
			echo -n "$DSPSW_IFACE_NAME, "
			/bin/ifup $CONF_PATH/ifcfg-dsp
		fi
		if [ "$IPV6_AUTO_CONFIG_LAN" = "false" ]; then
			if [ "$isremoveIPv6Address" = "true" ] ; then
				/bin/sleep 3
				/bin/ip add del "$AUTOIPV6ADDR_LAN/64" dev lan0
			fi
		fi

	fi

	if [ "$handle_wan0" = "true" ] ; then
		echo -n "wan interface, "
		/bin/ifup $CONF_PATH/ifcfg-wan0
		IPV6ADDR=""
		mac=`cat /sys/class/net/wan0/address`
		AUTOIPV6ADDR_WAN=fe80::$(printf %02x $((0x${mac%%:*} ^ 2)))
		mac=${mac#*:}
		AUTOIPV6ADDR_WAN=$AUTOIPV6ADDR_WAN${mac%:*:*:*}ff:fe
		mac=${mac#*:*:}
		AUTOIPV6ADDR_WAN=$AUTOIPV6ADDR_WAN${mac%:*}${mac##*:}
		AUTOIPV6ADDR_WAN=`echo $AUTOIPV6ADDR_WAN | sed -e 's/:0/:/g'`
		if [ "$IPV6_AUTO_CONFIG_WAN" = "false" ]; then
			. $CONF_PATH/ifcfg-wan0
			if [ "$IPV6ADDR" != "$AUTOIPV6ADDR_WAN/64" ]; then
				/bin/sleep 3
				/bin/ip add del "$AUTOIPV6ADDR_WAN/64" dev wan0
			fi
		fi
	fi

	tempip=$(/bin/addr -i $PHYS_WAN 2> /dev/null)
	if [ "$tempip" != "" ] ; then
		/bin/fw_ci --add changeip --ip $tempip
	fi
	echo "  done"


# configure vlan-interfaces
echo -n " vlan: "
if [ "$FEATURE_VLAN" = "1" ] ; then 
	/bin/vlan-helper -l > /dev/null 2>&1 && echo "done" || echo "failed"
	if [ -s /etc/fiad-conf/vlan.cfg ] ; then
		/bin/sleep 1
# remove default Auto configured IPV6 address from LAN/WAN interfaces
		if [ "$handle_lan0" = "true" ] ; then
			for entry in $CONF_PATH/ifcfg-lan0.*
			do
				if [ -f $entry ] ; then
					. /"$entry"
					/bin/sleep 3
					/bin/ip add del "$AUTOIPV6ADDR_LAN/64" dev $DEVICE
				fi
			done
		fi
		if [ "$handle_wan0" = "true" ] ; then
			for entry in $CONF_PATH/ifcfg-wan0.*
			do
				if [ -f $entry ] ; then
					. /"$entry"
					/bin/sleep 3
					/bin/ip add del "$AUTOIPV6ADDR_WAN/64" dev $DEVICE
				fi
			done
		fi
	fi

	if [ "$START_VLANGW" = "true" ] ; then
		echo "0" > /proc/sys/net/ipv4/ip_dynaddr
		/bin/ifup $CONF_PATH/ifcfg-$VLAN_INTERFACE
		tempip=$(cat $CONF_PATH/ifcfg-$VLAN_INTERFACE | grep IPADDR | /bin/cut -f 2 -d '=')
		if [ "$tempip" != "" ] ; then
			/bin/fw_ci --add changeip --ip $tempip
		fi
		if ! /bin/ip route add default dev $VLAN_INTERFACE via $GATEWAY; then
			echo "Error: failed to add default route via $GATEWAY on $VLAN_INTERFACE"
		fi
	fi
else
	echo "skipped"
fi

	echo -n " routing: "
	if [ "$START_VLANGW" != "true" ] ; then
		if [ "x$GATEWAY" != "x" ] ; then
			echo -n "default, "
			echo "0" > /proc/sys/net/ipv4/ip_dynaddr
			if ! /bin/ip route add default via $GATEWAY; then
				echo "Error: failed to add default route via $GATEWAY"
			fi
		fi
		# IPv6 routing
		if [ "x$IPV6_DEFAULTGW" != "x" ] ; then
			echo -n "IPv6 default, "
			if ! /sbin/route -A inet6 add $IPV6_DEFAULTGW $PHYS_WAN; then
				echo "Error: failed to add default route via $IPV6_DEFAULTGW"
			fi
		fi
	fi
	echo -n "static, "
	/bin/addroute -b -s all > /dev/null 2>&1
	echo -n "policy - "
	/bin/addroute -b -p all > /dev/null 2>&1
	echo "  done"

	if [ "$DISABLE_IPV6_LAN" = "true" ]; then
		echo 1 >/proc/sys/net/ipv6/conf/lan0/disable_ipv6
	else
		echo 0 >/proc/sys/net/ipv6/conf/lan0/disable_ipv6
	fi
	if [ "$DISABLE_IPV6_WAN" = "true" ]; then
		echo 1 >/proc/sys/net/ipv6/conf/wan0/disable_ipv6
	else
		echo 0 >/proc/sys/net/ipv6/conf/wan0/disable_ipv6
	fi

	echo 0 > /proc/sys/net/ipv6/conf/lan0/dad_transmits
	echo 0 > /proc/sys/net/ipv6/conf/lan0/accept_dad

	echo 0 > /proc/sys/net/ipv6/conf/wan0/dad_transmits
	echo 0 > /proc/sys/net/ipv6/conf/wan0/accept_dad

	return
}

do_stop ()
{
# remove all routes
	/etc/init.d/softdogd wait
	ip route del all  > /dev/null 2>&1
	return
}

do_status ()
{
	echo "Sorry.. not yet implemented"
	exit 1
}

. /var/run/interfaces

case "$1" in
	start)
		handle_lan0=true
		handle_wan0=true
		do_start
		;;
	stop)
		handle_lan0=true
		handle_wan0=true
		do_stop
		;;
	status)
		do_status
		;;
	restart)
		handle_lan0=true
		handle_wan0=true
		do_stop
		/bin/touch /tmp/skipMtoBReboot
		do_start
		;;
	restart_lan0)
		handle_lan0=true
		handle_wan0=false
		do_stop
		/bin/touch /tmp/skipMtoBReboot
		do_start
		;;
        restart_wan0)
		handle_lan0=false
		handle_wan0=true
		do_stop
		/bin/touch /tmp/skipMtoBReboot
		do_start
		;;
	*)
		echo "Usage: $0 {start|stop|restart|restart_lan0|restart_wan0|status}"
		exit 1
esac

exit $?
