#!/bin/bash
#
# modules Insert/Remove the kernel loadable modules 
#

RETVAL=0
RC=0

# See how we were called.
  
. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/init_functions
. /etc/profile.d/SetNNEnvVar.sh

# removeifunused()
# parameter : kernel loadable module name
# 1) determines if a specified kernel loadable module is loaded
# 2) determines if it is currently still being used
# 3) if not being used, unloads the module
# 4) if still being used, logs message to /var/log/shutdown.log

removeifunused() {
    lsmod | grep -w $1
    if [ "$?" -eq "0" ]; then
        lsmod | grep -w $1 | awk '{print $1" "$3}' | grep -w 0
        if [ "$?" -eq "0" ]; then
            /sbin/rmmod $1
        else
            echo Could not remove device $1, being used >> /var/log/shutdown.log
        fi
    fi
}

start() {
        echo -n $"Inserting kernel loadable modules."
	/bin/mknod /dev/PCI_Dev c 253 0
	/bin/mknod /dev/GPIO_Dev c 252 0
	/bin/mknod /dev/nn_dsc c 39 0
	/bin/mknod /dev/sda b 8 0
	/bin/mknod /dev/sda1 b 8 1
	/bin/mknod /dev/sda2 b 8 2
	/bin/mknod /dev/sda3 b 8 3
	/bin/mknod /dev/sda4 b 8 4
	/nn/sbin/MSCDevices.sh 
	/nn/sbin/ISDNDevices.sh 
	/nn/sbin/RTPDevice.sh 
	echo -n "Created Devices"

	/sbin/insmod -f slhc
	/sbin/insmod -f isdn
	/sbin/insmod -f ppp_generic
        /sbin/insmod -f ppp_async
        /sbin/insmod -f ppp_deflate
	/sbin/insmod -f bsd_comp
        /sbin/insmod -f /nn/lib/modules/pciScan.o
        /sbin/insmod -f /nn/lib/modules/genport.o
	/sbin/insmod -f /nn/lib/modules/gapKPPPd.o
        /sbin/insmod -f /nn/lib/modules/krnldll.o
	/sbin/insmod -f /nn/lib/modules/lldial.o
	/sbin/insmod -f /nn/lib/modules/nn_rtp.o defaultStrictFilter=1
	/sbin/insmod -f /nn/lib/modules/mt_common.o
        /sbin/insmod -f /nn/lib/modules/msclindrv.o
        /sbin/insmod -f /nn/lib/modules/mt_ds30.o
        /sbin/insmod -f /nn/lib/modules/mt_jitter.o
        /sbin/insmod -f /nn/lib/modules/mt_rtp.o
        /sbin/insmod -f /nn/lib/modules/mt_udp.o
	/sbin/insmod -f /nn/lib/modules/mt_mipa.o
	modprobe iptable_nat
        #/sbin/insmod -f ip_conntrack
        #/sbin/insmod -f iptable_nat

	#Adding for DSC
        /sbin/insmod -f /nn/lib/modules/oSIP.o
        /sbin/insmod -f /nn/lib/modules/DSCmod.o

	if [ -f /nn/sbin/wan.sh ]; then
		/nn/sbin/wan.sh start
	fi

	#Install Modem
        /nn/etc/ppp/modem/ModemInstallation/InstallModem

        #Load Modem Drivers
        if [ -f /nn/etc/ppp/modem/LoadModemDrivers ]; then
                /nn/etc/ppp/modem/LoadModemDrivers
        fi

        # nn_netfilter module is installed via following binary
        # comment out comment # moved to separate location due to reliance on Pdrd
        #/nn/bin/nn_netfilter_install
        return 0 
}

stop() {
        echo -n $"Removing kernel loadable modules, if possible and if unused."
	#removeifunused ip_conntrack
	#removeifunused iptable_nat
	removeifunused mt_mipa
        removeifunused mt_udp
        removeifunused mt_rtp
        removeifunused mt_jitter
        removeifunused mt_ds30
        removeifunused msclindrv
	removeifunused mt_common
	removeifunused nn_rtp
        removeifunused lldial
        removeifunused krnldll
        removeifunused genport
        removeifunused pciScan
	removeifunused gapKPPPd

	if [ -f /nn/sbin/wan.sh ]; then
		/nn/sbin/wan.sh stop
	fi

	removeifunused DSCmod
	removeifunused oSIP
        removeifunused bsd_comp
	removeifunused ppp_deflate
        removeifunused ppp_async
        removeifunused ppp_generic
        removeifunused isdn
        removeifunused slhc

	rm -f /dev/GPIO_Dev
	rm -f /dev/PCI_Dev
	rm -f /dev/nn_dsc
	rm -f /dev/sda
	rm -f /dev/sda1
	rm -f /dev/sda2
	rm -f /dev/sda3
	rm -f /dev/sda4
	rm -rf /dev/msc
	rm -f /tmp/osa_ipc/*DS30*
#	rm /nn/sbin/core*  --- To be enabled during release 

        return 0 
}        

restart() {
        stop
        start
}        

reload() {
        restart
}        

case "$1" in
  start)
          start
        ;;
  stop)
          stop
        ;;
  restart)
          restart
        ;;
  reload)
          reload
        ;;
  condrestart)
          [ -f /var/lock/subsys/$prog ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|condrestart}"
        exit 1
esac

exit $?
