#!/bin/bash
###################################################################
#
#      File:   instsse
#      Name:   Ripon
#
#      Description:
#        sse install script
#  
#  
# Copyright (c) 1985-2004, 2015 by cisco Systems, Inc.
# All rights reserved.
#  
#  
#   $Id: instsse,v 1.4 2004/06/30 18:36:33 rbhattac Exp $
#
################################################################################
#the path to the directory where sse module is 
#located. Please note, its a directory name.

####################################################################

#------------------------------------------------------------
. /etc/init.d/mod_ins/module-load-functions
#Unix Command Pathnames
RM="/bin/rm -rf"
INSMOD="insmod -f"
RMMOD=rmmod 
LSMOD=lsmod
MODULE_PATH=/lib/modules


#Device Filenames
SSE_DEV_NODE=/dev/klm_sse

#Module Names (the ones that lsmod shows)
SSEMOD=klm_sse

#Filenames for the module files
SSEMOD_FILE=$MODULE_PATH/$SSEMOD\.o

#------------------------------------------------------------
#Functions
#------------------------------------------------------------

#ModuleExists()
#This function returns 1 if the 
#module $1 is currently loaded in the system
ModuleExists()
{
  exists=`$LSMOD | grep $1`
  if [ -z "$exists" ]
  then
    return 1;
  else
    return 0;
  fi
}

#RemoveModule()
#This function removes a module and exits
#if the module exists and cannot be removed
RemoveModule()
{
  ModuleExists $1
  if [ $? -eq 0 ]
  then 
    boot_debug "Removing old $1 module ... "
    $RMMOD $1;
    if [ $? -ne 0 ]
    then
	echo "failed"
	exit -2
    else
        boot_debug "done"
    fi
   fi
}

#RemoveOldDevices()
#A function to remove existing modules and device
#files and cleanup the machine
#It removes the sse module and deletes sse
# specific device nodes from the system
RemoveOldDevices() 
{
    RemoveModule $SSEMOD
    $RM $SSE_DEV_NODE 2>/dev/null
    return 0;
}

#Usage()
#   Print the Usage and exit
Usage()
{
    boot_debug "Usage : instsse [start ssemodpath | stop] "
    exit 1
}

#CheckArgs()
#   Verify that command line arguments are correct

CheckArgs() 
{
    if [ $# -lt 1 ] 
    then
	Usage
    fi
    return 0;
}

#InstallDevices()
#   Does all the work 
InstallDevices()
{
    boot_debug "Installing SSE module ... "
    #MODULE_PATH=$1
    #SSEMOD_FILE=$MODULE_PATH/$SSEMOD\.o
    $INSMOD $SSEMOD_FILE
    if [ $? != 0 ]
	then
	boot_debug "failed"
	exit -2
    else
    boot_debug "done"
    fi
    boot_debug "Creating the sse device node ... "
    DEV_NO=`cat /proc/devices | fgrep $SSEMOD | awk '{print $1}'`
    mknod $SSE_DEV_NODE c $DEV_NO 0 
    chmod 666 $SSE_DEV_NODE
    boot_debug "done"
    #Load the registry now
}

#-------------------------------------------------------------
#main() !
#-------------------------------------------------------------

#Check the Arguments first
CheckArgs $*;

ACTION=$1
MODPATH=$2

case "$ACTION" in 
    stop)
	RemoveOldDevices;
	exit $?;
	;;
    start)
	RemoveOldDevices;
	InstallDevices $MODPATH;
	;;
    *)
	Usage;
esac;


