#!/bin/bash
#
#
#/******************************************************************************
#                             
#                         INTEL CONFIDENTIAL
#
#              Copyright (c) 1999-2009 Intel Corporation.
#                        All Rights Reserved.
#
#        The source code contained or described herein and all
#     documents related to the source code ("Material") are owned
#    by Intel Corporation or its suppliers or licensors.  Title to
#    the Material remains with Intel Corporation or its suppliers
#       and licensors.  The Material may contains trade secrets and
#      proprietary and confidential information of Intel or its
#       suppliers and licensors.  The Material is protected by
#        worldwide copyright and trade secret laws and treaty
#      provisions. No part of the Material may be used, copied,
#         reproduced, modified, published, uploaded, posted,
#      transmitted, distributed, or disclosed in any way without
#             Intel's prior express written permission.
#
#    No license under any patent, copyright, trade secret or other
#     intellectual property right is granted to or conferred upon
#       you by disclosure or delivery of the Materials, either
#    expressly, by implication, inducement, estoppel or otherwise.
#     Any license under such intellectual property rights must be
#             express and approved by Intel in writing.
#
#   Include any supplier copyright notices as supplier requires Intel to use.
#   Include supplier trademarks or logos as supplier requires Intel to use, 
#   preceded by an asterisk. An asterisked footnote can be added as follows:
#	*Third Party trademarks are the property of their respective owners.
#
#    Unless otherwise agreed by Intel in writing, 
#    you may not remove or alter this notice or any other notice embedded
#    in Materials by Intel or Intels suppliers or licensors in any way.
#
#################################################################################
# chkconfig: - 03456 20 80
# description: Installs the BIOS Kernel module for Syscfg \
#              utility.
# processname: smi
#

#DVRDIR=$SYSCFG_PATH/drivers
DVRDIR=/usr/local/syscfg/drivers


#Exit Status Codes
ERR_INV_ARG=1
ERR_NON_ROOT=2
ERR_KERNEL_REL=3
ERR_INSMOD=4
ERR_RMMOD=5
ERR_MKNOD=8
ERR_MAJNUM=7
ERR_USAGE=6
DRV_NAME=smi
FILE_NAME=smi.o
FILE_NAME26=smi.ko

#-------------- Function for install -------------
InstallDriver()
{
	majorDevNumber=

	# Removed since we need the GPL message on the console, directing to null
	echo ====== Syscfg Driver Installation ====== > /dev/null

	if [ -z $1 ]
	then
		echo Driver installation: ERROR - Internal Function 'InstallDriver' requires one argument.
		return $ERR_INV_ARG
	fi

	echo test > /dev/NULL
	if [ $? -ne 0 ]
	then
		echo Driver installation: ERROR - User does not have root previlige.
		return $ERR_NON_ROOT
	fi

	cat /proc/devices | grep $1 > /dev/NULL
	if [ $? -eq 0 ]
	then
		/sbin/rmmod $1
	fi

	# Removed since we need the GPL message on the console, directing to null
	echo Driver installation: Installing Kernel Module $DVRDIR/$1 > /dev/null

	kernelversion=`uname -r | sed "s/-/./" | cut -d "." -f1-2`

	if [ "2.4" = "$kernelversion" ]
	then
		/sbin/insmod $DVRDIR/$FILE_NAME
	fi

	if [ "2.6" = "$kernelversion" ]
	then
	    /sbin/insmod $DVRDIR/$FILE_NAME26

	fi


	if [ $? -ne 0 ]
	then
		echo Driver installation: ERROR - 'insmod' command failed
		return $ERR_INSMOD
	fi

	majorDevNumber=`cat /proc/devices | grep $DRV_NAME | cut -d " " -f1`
	if [ ! "$majorDevNumber" = "" ]
	then
		# Removed since we need the GPL message on the console, directing to null
		echo Driver installation: Making device node /dev/$1 > /dev/null
		rm -f /dev/$DRV_NAME
		mknod /dev/$DRV_NAME c $majorDevNumber 0
		if [ $? -ne 0 ]
		then
			echo Driver installation: ERROR - Unable to make device node /dev/$1
			return $ERR_MKNOD
		fi
	else
		echo Driver installation: ERROR - Unable to determine the mojor device number for module $1
		return $ERR_MAJNUM
	fi

	# Removed since we need the GPL message on the console, directing to null
	echo Driver installation: $1 installation complete > /dev/null
	echo > /dev/null
	return 0
}
#--------- Function to uninstall driver -------------
RemoveDriver()
{
	if [ -z $1 ]
	then
		echo Driver installation: ERROR - Internal Function 'RemoveDriver' requires atleast one argument.
		return $ERR_INV_ARG
	fi

	echo test > /dev/NULL
	if [ $? -ne 0 ]
   then
      echo Driver installation: ERROR - User does not have root previlige.
      return $ERR_NON_ROOT
   fi

	cat /proc/devices | grep $DRV_NAME > /dev/NULL
	if [ $? -eq 0 ]
	then
		echo Driver installation: Removing Kernel Module $DVRDIR/$1
		kernelversion=`uname -r | sed "s/-/./" | cut -d "." -f1-2`
		if [ "2.4" = "$kernelversion" ]
		then
			/sbin/rmmod $DRV_NAME > /dev/NULL
		fi
		if [ "2.6" = "$kernelversion" ]
		then
			/sbin/rmmod $DRV_NAME > /dev/NULL
		fi

		if [ $? -ne 0 ]
		then
			echo Driver installation: ERROR - rmmod command failed.
			return $ERR_RMMOD
		fi
	fi

	if [ -c /dev/$DRV_NAME ]
	then
		echo Driver installation: Removing device node /dev/$1
		rm -f /dev/$DRV_NAME
	fi
	echo Driver installation: Driver $DRV_NAME uninstalled.
	echo
	return 0
}
#---------------------------------------------------
case $1 in
	start)
		InstallDriver smi
      if [ $? -ne 0 ]
      then
         echo Driver installation: ERROR - Installation of driver module $DVRDIR/smi failed.
			RET_VAL=$?
      fi
		;;
	stop)
		RemoveDriver smi
      if [ $? -ne 0 ]
      then
         echo Driver installation: ERROR - Installation of driver module $DVRDIR/smi failed.
			RET_VAL=$?
      fi
		;;
	test)
		;;
	*)
		#RET_VAL=$ERR_USAGE
		#echo $Usage: $0 { start|stop }
		echo inside default : need to remove this line
		;;
esac

exit $RET_VAL
