#!/bin/sh
#
# manage network interfaces and configure some networking options

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

if ! [ -x /sbin/ifup ]; then
    exit 0
fi

if ! [ -f /conf/vlanid ]; then
    cp -f /etc/defconfig/vlanid /conf/vlanid
fi
VLANINTERFACES_FILE=/conf/vlaninterfaces
VLAN_ID=`awk '{print $2}'  /conf/vlanid | sed -n '1p'`
VLAN_ID1=`awk '{print $2}' /conf/vlanid | sed -n '2p'`

VLAN_STR=`awk '{print $1}' /proc/net/vlan/config | sed -n '3p'`
VLAN_STR1=`awk '{print $1}' /proc/net/vlan/config | sed -n '4p'`

if  [ -f  /proc/sys/ractrends/ncsi/Interface ]; then
NCSIInterface=`cat /proc/sys/ractrends/ncsi/Interface`
else
NCSIInterface="NO"
fi

VLANPriority=`awk '{print $2}'  /conf/vlanpriority | sed -n '1p'`
VLANPriority1=`awk '{print $2}' /conf/vlanpriority | sed -n '2p'`
case "$1" in
    start)
	if [ x"$VLAN_ID" != x ]; then
		if [ $VLAN_ID != 0 ]; then
	        	echo -n "Configuring vlan interfaces.."
			vconfig add eth0 $VLAN_ID 
			echo "done."
			if  [ -f  /var/run/udhcpc.eth0.pid ]; then
				kill -9 `cat /var/run/udhcpc.eth0.pid`
			fi
			if [ $NCSIInterface == "eth0" ]; then
				echo "$VLAN_ID" > /proc/sys/ractrends/ncsi/VlanID
			fi
			if [ x"$VLANPriority" != x ]; then
				if [ $VLANPriority != 0 ]; then
					vconfig set_egress_map eth0.$VLAN_ID 0 $VLANPriority
				fi
			fi
		        ifup eth0.$VLAN_ID -i $VLANINTERFACES_FILE
			ifconfig eth0 0.0.0.0
		else
		    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
		fi
	fi
	if [ x"$VLAN_ID1" != x ]; then
		if [ $VLAN_ID1 != 0 ]; then
	        	echo -n "Configuring vlan interfaces.."
			vconfig add eth1 $VLAN_ID1
			echo "done."
			if  [ -f  /var/run/udhcpc.eth1.pid ]; then
				kill -9 `cat /var/run/udhcpc.eth1.pid`
			fi
			if [ $NCSIInterface == "eth1" ]; then
				echo "$VLAN_ID1" > /proc/sys/ractrends/ncsi/VlanID
			fi
			if [ x"$VLANPriority1" != x ]; then
				if [ $VLANPriority1 != 0 ]; then
					vconfig set_egress_map eth1.$VLAN_ID1 0 $VLANPriority1
				fi
			fi
		        ifup eth1.$VLAN_ID1 -i $VLANINTERFACES_FILE
			ifconfig eth1 0.0.0.0
		else
		    echo -n "VLANID1 is not enabled...So Exit VLAN network interface start up....."
		fi
	fi
	;;
    stop)
	if [ $VLAN_ID != 0 ]; then
        	echo -n "Deconfiguring vlan interfaces..."
	     	echo "0" > /proc/sys/ractrends/ncsi/Enable
	        ifdown eth0.$VLAN_ID -i  $VLANINTERFACES_FILE
        	vconfig rem eth0.$VLAN_ID 
		echo "done."
	else
	    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
	fi

	if [ $VLAN_ID1 != 0 ]; then
        	echo -n "Deconfiguring vlan interfaces..."
	        ifdown eth1.$VLAN_ID1 -i  $VLANINTERFACES_FILE
        	vconfig rem eth1.$VLAN_ID1 
		echo "done."
	else
	    echo -n "VLANID1 is not enabled...So Exit VLAN network interface start up....."
	fi
	;;
    start0)
	if [ $VLAN_ID == 0 ]; then
	    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
	    exit 0
	fi
	VLAN="eth0."
        VLAN=$VLAN$VLAN_ID
        if [ x"$VLAN_STR" == x ];
        then
        	echo -n "Configuring vlan interfaces.."
		vconfig add eth0 $VLAN_ID
		echo "done."
        else if [ $VLAN != $VLAN_STR ];
                then
                        if [ x"$VLAN_STR1" == x ];
                        then
        			echo -n "Configuring vlan interfaces.."
				vconfig add eth0 $VLAN_ID
				echo "done."
                        else if [ $VLAN != $VLAN_STR1 ];
                        then
        			echo -n "Configuring vlan interfaces.."
				vconfig add eth0 $VLAN_ID
				echo "done."
                              fi
                        fi
                fi
        fi
	if  [ -f  /var/run/udhcpc.eth0.pid ]; then
		kill -9 `cat /var/run/udhcpc.eth0.pid`
	fi
	if [ x"$VLANPriority" != x ]; then
		if [ $VLANPriority != 0 ]; then
			vconfig set_egress_map eth0.$VLAN_ID 0 $VLANPriority
		fi
	fi	
	if [ $NCSIInterface == "eth0" ]; then
		echo "$VLAN_ID" > /proc/sys/ractrends/ncsi/VlanID
	fi
        ifup eth0.$VLAN_ID -i $VLANINTERFACES_FILE
	ifconfig eth0 0.0.0.0

	;;
    stop0)
	if [ $VLAN_ID == 0 ]; then
	    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
	    exit 0
	fi
        echo -n "Deconfiguring vlan interfaces..."
        ifdown eth0.$VLAN_ID -i  $VLANINTERFACES_FILE
        vconfig rem eth0.$VLAN_ID 
	echo "done."
	;;
 downvlan0)
	if [ $VLAN_ID == 0 ]; then
	    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
	    exit 0
	fi
        ifdown eth0.$VLAN_ID -i  $VLANINTERFACES_FILE
	;;
    start1)
	if [ $VLAN_ID1 == 0 ]; then
	    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
	    exit 0
	fi

	VLAN="eth1."
        VLAN=$VLAN$VLAN_ID1
        if [ x"$VLAN_STR" == x ];
        then
        	echo -n "Configuring vlan interfaces.."
		vconfig add eth1 $VLAN_ID1
		echo "done."
        else if [ $VLAN != $VLAN_STR ];
                then
                        if [ x"$VLAN_STR1" == x ];
                        then
        			echo -n "Configuring vlan interfaces.."
				vconfig add eth1 $VLAN_ID1
				echo "done."
                        else if [ $VLAN != $VLAN_STR1 ];
                        then
        			echo -n "Configuring vlan interfaces.."
				vconfig add eth1 $VLAN_ID1
				echo "done."
                              fi
                        fi
                fi
        fi
	if [ -f /var/run/udhcpc.eth1.pid ] ; then
		kill -9 `cat /var/run/udhcpc.eth1.pid`
	fi
	if [ x"$VLANPriority1" != x ]; then
		if [ $VLANPriority1 != 0 ]; then
			vconfig set_egress_map eth1.$VLAN_ID1 0 $VLANPriority1
		fi
	fi	
	if [ $NCSIInterface == "eth1" ]; then
		echo "$VLAN_ID1" > /proc/sys/ractrends/ncsi/VlanID
	fi
	ifup eth1.$VLAN_ID1 -i $VLANINTERFACES_FILE
	ifconfig eth1 0.0.0.0
	;;
    stop1)
	if [ $VLAN_ID1 == 0 ]; then
	    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
	    exit 0
	fi
        echo -n "Deconfiguring vlan interfaces..."
        ifdown eth1.$VLAN_ID1 -i  $VLANINTERFACES_FILE
        vconfig rem eth1.$VLAN_ID1 
	echo "done."
	;;
 downvlan1)
	if [ $VLAN_ID1 == 0 ]; then
	    echo -n "VLANID is not enabled...So Exit VLAN network interface start up....."
	    exit 0
	fi
        ifdown eth1.$VLAN_ID1 -i  $VLANINTERFACES_FILE
	;;
    force-reload|restart)
        echo -n "Reconfiguring network interfaces..."
        ifdown eth0.$VLAN_ID -i  $VLANINTERFACES_FILE
        vconfig rem eth0.$VLAN_ID 
	vconfig add eth0 $VLAN_ID 
        ifup eth0.$VLAN_ID -i $VLANINTERFACES_FILE
        
        ifdown eth0.$VLAN_ID1 -i  $VLANINTERFACES_FILE
	vconfig rem eth0.$VLAN_ID1 
	vconfig add eth0 $VLAN_ID1 
	ifup eth0.$VLAN_ID1 -i $VLANINTERFACES_FILE
        
	echo "done."
	;;
#   enableip)
#        echo -n "Configuring IP addr and netmask for vlan interfaces.."
#        ifup eth0.$VLAN_ID -i $VLANINTERFACES_FILE
#	;;
    *)
	echo "Usage: /etc/init.d/vlannetworking {start|stop|restart|force-reload}"
	exit 1
	;;
esac

exit 0

