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

RETVAL=0
RC=0

AWK=`which awk` || AWK=/opt/bin/awk

# See how we were called.

# 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."
        /sbin/insmod -f /nn/lib/modules/gen_probe.o
        /sbin/insmod -f /nn/lib/modules/cfi_cmdset_0002.o
        /sbin/insmod -f /nn/lib/modules/cfi_probe.o
        /sbin/insmod -f /nn/lib/modules/mtd_nn_450.o
        /sbin/insmod -f /nn/lib/modules/nn_gpiol.o
        /sbin/insmod -f /nn/lib/modules/nn_spi.o
        /sbin/insmod -f /nn/lib/modules/nn_gpioh.o
        /sbin/insmod -f /nn/lib/modules/nn_prom.o
        /sbin/insmod -f /nn/lib/modules/nn_dsp.o
        /sbin/insmod -f /nn/lib/modules/nn_rtp.o
        /sbin/insmod -f /nn/lib/modules/nn_dchan_drv.o NnDefaultMicroValue=2176
        /sbin/insmod -f /nn/lib/modules/nn_timeswitch.o
        /sbin/insmod -f /nn/lib/modules/nn_nat.o
        /sbin/insmod -f /nn/lib/modules/nn_vcom.o
        /sbin/insmod -f /nn/lib/modules/nn_codec.o
        /sbin/insmod -f /nn/lib/modules/nn_fpga.o
        /sbin/insmod -f /nn/lib/modules/isdn.o
        /sbin/insmod -f /nn/lib/modules/nn_mcc.o
        /sbin/insmod -f /nn/lib/modules/nn_isdndrv.o
        /sbin/insmod -f /nn/lib/modules/nn_sanity.o

        # Execute the codecconfig utility
        /nn/bin/codecconfig /nn/etc/nn_450_codec.conf

        # Run the midplane communications script for BCM if present
        if [ -f /etc/rc.d/init.d/mpc.sh ]; then
            /etc/rc.d/init.d/mpc.sh
        fi

# Temporary Duration - disable PSM if HWM FPGA Major Number is 0 (Proto system)
/nn/bin/psmtest -f | grep "HWM FPGA Major Number" | grep -w 0
if [ $? -eq 0 ]; then
    if [ -e /etc/rc.d/rc3.d/S31psm ];then
        mv /etc/rc.d/rc3.d/S31psm /etc/rc.d/rc3.d/pending/
    fi
    if [ -e /nn/etc/scd/monitcfg/M007B_psm ];then
        mv /nn/etc/scd/monitcfg/M007B_psm /nn/etc/scd/monitcfg/pending/
    fi
fi

        return 0 
}

stop() {
    echo -n $"Removing kernel loadable modules, if possible and if unused."

    removeifunused nn_sanity
    removeifunused nn_isdndrv
    removeifunused nn_mcc
    removeifunused isdn
    removeifunused nn_codec
    removeifunused nn_vcom
    removeifunused nn_nat
    removeifunused nn_timeswitch
    removeifunused nn_dchan_drv
    removeifunused nn_rtp
    removeifunused nn_dsp
    removeifunused nn_prom

    # Wait for latest valid opportunity to signal that shutdown is complete.
    # This must occur before nn_gpioh kernel loadable module is unloaded.
    # LEDs are set to: Power LED = Off ( x88 ), Status LED = Solid Yellow ( x99 )
    echo `date` "Shutdown Sequence - Graceful Shutdown complete. Power may be removed." >> /var/log/StartupSequence.log
    echo -en "\x88" > /dev/led0
    echo -en "\x99" > /dev/led1

    removeifunused nn_gpioh
    #removeifunused nn_spi
    removeifunused nn_gpiol
    removeifunused mtd_nn_450
    removeifunused cfi_probe
    removeifunused cfi_cmdset_0002
    removeifunused gen_probe
    return 0
}

restart() {
        stop
        start
}        

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

exit $?
