#!/bin/sh
#
# Copyright 2003 Meru Networks
# All rights reserved.
#
# Expects MMODS to be set in the calling process.
#
tmp=/tmp/mmods_$$
# insmod errors
tmp2=/tmp/mmods2_$$
KDIR=/opt/meru/kernel

if [ -f /etc/init.d/meru-functions ]; then
	. /etc/init.d/meru-functions
else
	echo "/etc/init.d/meru-functions must exist to use $0"; exit 1
fi


set_params
install_mods()
{
	> $tmp
	/sbin/lsmod > $tmp
	for mod in $MMODS
	do
	    # Skip modules already loaded
	    egrep "^$mod" $tmp >/dev/null 2>&1 && continue

	    if [ "$mod" = "ip_tables" ]; then
		    modprobe ip_tables >/dev/null 2> $tmp2 || {
			echo "insmod error" | tee -a $LOG
			cat $tmp2 | tee -a $LOG
			fail "modprobe ip_tables failed"
		    }
		    continue
	    fi

	    ARGS=
	    IF_NAME=
	    [ "$mod" = "wncreg" ] && {
		    ARGS="g_wireless_subnet_addr_str=$WIRELESS_SUBNET_ADDR g_wireless_subnet_mask_str=$WIRELESS_SUBNET_MASK g_dhcp_server_addr_str=$DHCP_SERVER_IP"
	    }

		if [ "$SECOND_INTERFACE_MODE" = "redundant" -a "$mod" = "rebridging" ];then
			{
				ARGS="real_int=$REAL_IF name=$UDP_IF one_meru_mac=$MERU_MAC rebrid_if_mbx_flag=0"
			}
			insmod -m $KDIR/${mod}.mod $ARGS 1> $KDIR/${mod}.map 2> $tmp2 || 
			{
				echo "insmod error" | tee -a $LOG
				cat $tmp2 | tee -a $LOG
				fail "Could not load kernel module $mod"
			}
			continue
		fi

		#we insert one instance of rebridging module only if :
		#	1: the second interface mode is redundant
		#	2: HA is not configured.
		if [ $HIGH_AVAILABILITY_STATE != "1" ]; then
			if [ "$SECOND_INTERFACE_MODE" = "none" -a "$mod" = "rebridging" ];then
				{
					ARGS="real_int=$REAL_IF name=$UDP_IF one_meru_mac=$MERU_MAC rebrid_if_mbx_flag=0"
				}
				insmod -m $KDIR/${mod}.mod $ARGS 1> $KDIR/${mod}.map 2> $tmp2 || 
				{
					echo "insmod error" | tee -a $LOG
					cat $tmp2 | tee -a $LOG
					fail "Could not load kernel module $mod"
				}
				continue
			fi
		fi

	    if [ "$mod" = "rebridging" ]; then	      
			ARGS="-o rebridging1 real_int=$REAL_IF name=$UDP_IF one_meru_mac=$MERU_MAC rebrid_if_mbx_flag=0"				
			insmod -m $KDIR/${mod}.mod $ARGS 1> $KDIR/${mod}.map 2> $tmp2 || {
				echo "insmod error" | tee -a $LOG
					cat $tmp2 | tee -a $LOG
					fail "Could not load kernel module $mod"
			}
			ARGS="-o rebridging2 real_int=$SECOND_IF name=meru2 rebrid_if_mbx_flag=1"
			insmod -m $KDIR/${mod}.mod $ARGS 1> $KDIR/${mod}.map 2> $tmp2 || {
				echo "insmod error" | tee -a $LOG
				cat $tmp2 | tee -a $LOG
				fail "Could not load kernel module $mod"
			}
			continue;
	    fi

	    [ "$mod" = "atsvif" ] && {
		    ARGS="g_real_if_name=$REAL_IF"
	    }

	    insmod -m $KDIR/${mod}.mod $ARGS 1> $KDIR/${mod}.map 2> $tmp2 || {
		echo "insmod error" | tee -a $LOG
		cat $tmp2 | tee -a $LOG
		fail "Could not load kernel module $mod"
	    }
	done
	rm -f $tmp $tmp2
}

start() {
	install_mods
}

stop() {
	for mod in $MMODS
	    do
	    reverse="$mod $reverse"
	done

	/sbin/lsmod > $tmp

	for mod in $reverse
	do
		egrep "^$mod" $tmp >/dev/null 2>&1 && {
			if [ "$mod" = "rebridging" ] ; then
				for (( i=1 ; i <=2 ; i++))
			   	do
		   			rmmod $mod$i > /dev/null 2>&1		
		   		done
		       fi
			if [ "$mod" = "kcomm" ]; then
				i=0
				while ! ( rmmod $mod > /dev/null 2>&1 )
				do
					i=`expr $i + 1`
					sleep 1
					echo -n "."
					[ $i -gt 20 ] && {
						break
					}
				done
			else
				rmmod $mod > /dev/null 2>&1
			fi
		}
		rm -f $KDIR/${mod}.map
	done
	rm -f $tmp
	echo ""
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	*)
		echo "Usage: $prog {start|stop}"
		exit 1
esac

exit 0
