#!/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

        # Check uboot flag and flash uboot if required
        /nn/bin/uboot_flash.sh

        # 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

        # Create hard disk links for RAID and HW inventory provider
        if [ -e /dev/bcm_hdd1 ]; then
           rm -f /dev/bcm_hdd1
        fi

        if [ -e /dev/bcm_hdd2 ]; then
           rm -f /dev/bcm_hdd2
        fi

        # We really don't need to force the link creation -sf since we just deleted them
        # but I hate to fail just because a link exists
        /nn/bin/fw_printenv | grep hdd1_serial_no >/dev/null 2>&1 && /bin/ln -sf /dev/sdb /dev/bcm_hdd1

        # If ata1 existed, it was assigned sdb, so second hard disk is sdd
        # Otherwise single hard disk installed in right slot as sdb

        if [ -e /dev/bcm_hdd1 ]; then
           /nn/bin/fw_printenv | grep hdd2_serial_no >/dev/null 2>&1 && /bin/ln -sf /dev/sdd /dev/bcm_hdd2
        else
           /nn/bin/fw_printenv | grep hdd2_serial_no >/dev/null 2>&1 && /bin/ln -sf /dev/sdb /dev/bcm_hdd2
        fi

        #if RAID is enabled need to update bcm links
        _bcm_links=data kernel log mfgdiags root shadow swap upgrades

        /nn/bin/fw_printenv | grep raid=y >/dev/null 2>&1
        _raid=$?
        
        # is the second HD present as far as RAID is concerned
        /sbin/mdadm --examine /dev/sdd1 >& /dev/null
        _hdd2=$?

        # removing the /dev/bcm* symlinks we will recreate them
        # to point to proper partitions below
        for _partition in $_bcm_links; do
           if [ -e /dev/bcm_"$_partition" ]; then
              rm -f /dev/bcm_"$_partition"
           fi
        done

        # check that RAID is working properly 
        if [ $_raid -eq 0 ] && [ $_hdd2 -eq 0 ]; then 
           
           # the bcm_* links that by default point to
           # /dev/sdbx paritions were removed, now we
           # set the bcm_* links to point to /dev/mdx
           # partitions created by RAID
           /bin/ln -sf /dev/md6 /dev/bcm_data
           /bin/ln -sf /dev/md1 /dev/bcm_kernel
           /bin/ln -sf /dev/md8 /dev/bcm_log
           /bin/ln -sf /dev/md5 /dev/bcm_mfgdiags
           /bin/ln -sf /dev/md3 /dev/bcm_root
           /bin/ln -sf /dev/md7 /dev/bcm_shadow
           /bin/ln -sf /dev/md12 /dev/bcm_swap
           /bin/ln -sf /dev/md9 /dev/bcm_upgrades	
        else
        
           # RAID is not enabled or just not working so
           # we point the primary HD, we set the links
           # to the /dev/sdbx partitions
           /bin/ln -sf /dev/sdb6 /dev/bcm_data
           /bin/ln -sf /dev/sdb1 /dev/bcm_kernel
           /bin/ln -sf /dev/sdb8 /dev/bcm_log
           /bin/ln -sf /dev/sdb5 /dev/bcm_mfgdiags
           /bin/ln -sf /dev/sdb3 /dev/bcm_root
           /bin/ln -sf /dev/sdb7 /dev/bcm_shadow
           /bin/ln -sf /dev/sdb12 /dev/bcm_swap
           /bin/ln -sf /dev/sdb9 /dev/bcm_upgrades  
        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 $?
