#!/bin/bash
#
# vlan   Bring up/down vlan
#
# chkconfig: 2345 11 89
# description: Activates/Deactivates all vlan interfaces configured to \
#              start at boot time.
# Version    : HP_VLAN_1_0_1

# The following lines are needed to make chkconfig work properly
# In an United Linux and SuSE Environment
### BEGIN INIT INFO
# Provides:     vlan
# Required-Start:	$network
# Should-Start: $network
# Required-Stop:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      1 6
### END INIT INFO

IFS='
'
VLANPROCDIR=/proc/net/vlan
VLANCONF=/etc/vlan.conf
VCONFIG=/sbin/vconfig
if [ ! -f $VCONFIG ]
then
	echo "Error!! vconfig $VCONFIG not found"
	exit 1
fi
if [ ! -f $VLANCONF ]
then
	echo "Error!! vlan config file $VLANCONF not found"
	exit 1
fi
# See how we were called.
case "$1" in
  start)
	# First get the name type of the vlan interface
	nt=`cat $VLANCONF | egrep '^[ 	]*nametype'| awk '{print $2}'`
	if [ "$nt" = "" ]
	then
        	nt=DEV_PLUS_VID_NO_PAD
	fi
	$VCONFIG set_name_type $nt
	for i in `cat $VLANCONF | egrep -v '^[ 	]*#'|egrep -v nametype`
	do
		iface=`echo $i | awk '{print $1}'`
		vlan_id=`echo $i | awk '{print $2}'`
		if [ $vlan_id -lt 0 -o $vlan_id -gt 4094 ]
		then
		  echo "Error!! vlan_id out of range for $iface $vlan_id"
		  continue
		fi
		ip_addr=`echo $i | awk '{print $3}'`
		netmask=`echo $i | awk '{print $4}'`
		$VCONFIG add $iface  $vlan_id
		#
		# Use the proc method to get the virtual device
		#
		vif=`cat $VLANPROCDIR/config | egrep "$iface" | egrep $vlan_id | awk -F'|' '{print $1}' | sed 's/ //g'`
		/sbin/ifconfig $vif $ip_addr netmask $netmask up
		/sbin/ifconfig $iface 0.0.0.0
	done
	touch /var/lock/subsys/vlan
        ;;
  stop)
		#User might just have added and trying to remove package
		# On UL and SLES7 some proc entries could be missing
		# and hence need to change the logic to remove VLANs
		# use the config file to determine VLAN devices
		if [ -d $VLANPROCDIR ]
		then
			for i in `cat $VLANPROCDIR/config | egrep -v "VLAN|Name-Type"| awk '{print $1}'`
			do
				/sbin/ifconfig $i down
				$VCONFIG rem $i
			done
		fi
	rm -f /var/lock/subsys/vlan
        ;;
  status)
	avlan=""
	tvlan=""
	ivlan=""
	acount=0
	icount=0
	tcount=0
	if [ ! -d /proc/net/vlan ]
	then
		tvlan="None"
		avlan="None"
		ivlan="None"
	else
		ls $VLANPROCDIR | egrep -v config > /dev/null 2>&1
		if [ $? != 0 ]
		then
		tvlan="None"
		avlan="None"
		ivlan="None"
		else
	        for i in `cat $VLANPROCDIR/config | egrep -v "VLAN|Name-Type"| awk '{print $1}'`
	   	do
   	   	/sbin/ifconfig $i | egrep RUNNING > /dev/null 2>&1
   	   	if [ $? = 0 ]
   	   	then 
        	   	avlan="$avlan $i"
			if [ $acount -eq 5 ]
			then
				avlan="$avlan \n"
				acount=0
			else
				acount=`expr $acount + 1`
			fi
   	   	else
   		   	ivlan="$ivlan $i"
			if [ $icount -eq 5 ]
			then
				ivlan="$ivlan \n"
				icount=0
			else
				icount=`expr $icount + 1`
			fi
   		fi
   	   	tvlan="$tvlan $i"
		if [ $tcount -eq 5 ]
		then
			tvlan="$tvlan \n"
			tcount=0
		else
			tcount=`expr $tcount + 1`
		fi
	   	done
		fi
	fi
		echo "Configured VLANs: (All VLAN devices)"
		echo ""
		echo -e "$tvlan"
		echo ""
		echo "Active VLANs: (VLAN Interfaces Up and Running)"
		echo ""
		echo -e "$avlan"
		echo ""
		echo "Inactive VLANs: (VLAN Interfaces down)"
		echo ""
		echo -e "$ivlan"
		echo ""
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0
