#!/bin/sh

###############################################################################
##
##          DELL INC. PROPRIETARY INFORMATION
##
##  This software is supplied under the terms of a license agreement or
##  nondisclosure agreement with Dell Inc. and may not be copied or disclosed
##  except in accordance with the terms of that agreement.
##
##  Copyright (c) 2003-2006 Dell, Inc. All Rights Reserved.
##
##  Abstract/Purpose:
##    Script is used to install Disney and handle any error that may occur
##    in installation.
##   This File is renamed to dupdisneyinstall.sh (for cm_MaserIE).              
##  "hapi" directory should be present in dup package.
##
##  Environment:
##    Linux
##
##  Version:
##    $Revision $
##
##  Last Modified by/on:
##    $Author: Jeff Glanz / $Date: September 16, 2005
##
###############################################################################
typeset -i _EXITCODE=0
typeset HAPI_LIB_PATH
typeset	HAPI_TAR_NAME

typeset -ri _TRUE=0 2>/dev/null
typeset -ri _FALSE=1 2>/dev/null
typeset -ri _VERBOSE=$_FALSE 2>/dev/null
bVerbose() {
    [ $_VERBOSE -eq $_TRUE ]
}


# Determine HAPI_LIB_PATH ; this is based on the presence of the corresponding hapi.tgz or hapi64.tgz
if [ -d "hapi" ]; then
	if [ -f hapi/hapi64.tgz ]; then
		HAPI_LIB_PATH="dup64"
		HAPI_TAR_NAME="hapi64.tgz"
	else
		HAPI_LIB_PATH="dup"
		HAPI_TAR_NAME="hapi.tgz"
	fi
else
	if [ -d "Hapi" ]; then
		if [ -f Hapi/hapi64.tgz ]; then
			HAPI_LIB_PATH="dup64"
			HAPI_TAR_NAME="hapi64.tgz"
		else
			HAPI_LIB_PATH="dup"
			HAPI_TAR_NAME="hapi.tgz"
		fi
	else
		if [ -f hapi64.tgz ]; then
			HAPI_LIB_PATH="dup64"
			HAPI_TAR_NAME="hapi64.tgz"
		else
			HAPI_LIB_PATH="dup"
			HAPI_TAR_NAME="hapi.tgz"
		fi
	fi
fi


typeset -r _HOMEDIR=`pwd`
typeset -r _LOGFILE_DIR=/var/log/dell
typeset -r _LOGFILE=$_LOGFILE_DIR/disneyinstall.log
export _LOGFILE
typeset -r _ROOT_UID=0
typeset _HAPI_INST_DIR=/opt/dell/${HAPI_LIB_PATH}
typeset -r _HAPI_OPT_FILE=/opt/cm_testfile
typeset -r _HAPI_CONFIG_FILE=/etc/omreg.cfg
typeset -r _IPMI_OVERRIDE="hapi.loadopenipmi=false"
typeset -r _INST_DVR_SERVICE_BASENAME=instsvcdrv
typeset -r _INST_DVR_SERVICE_NAME=${_HAPI_INST_DIR}/etc/init.d/$_INST_DVR_SERVICE_BASENAME
typeset -r _INST_DVR_SERVICE_NAME2=${_HAPI_INST_DIR}/etc/init.d/init.d/$_INST_DVR_SERVICE_BASENAME
typeset -r _IPMI0=/dev/ipmi0

# These names will be without the Version information attached to every driver
typeset -r _HAPI_RPM_PKG_NAME=`basename $_HOMEDIR/hapi/*hapi*.rpm | cut -f1,2 --delimiter='-'`
#typeset -r _IPMI_RPM_PKG_NAME=`basename $_HOMEDIR/hapi/*ipmi*.rpm | cut -f1,2 --delimiter='-'`
#typeset -r _OMILCORE_RPM_PKG_NAME=`basename $_HOMEDIR/hapi/*omilcore*.rpm | cut -f1,2 --delimiter='-'`

typeset -r _RPM_RELOC_DIR="/tmp/dellupdatepackage-hapi-$$-$RANDOM"
typeset -r _RPM_RELOC_PREFIX="--prefix=$_RPM_RELOC_DIR"
typeset -r _RPM_RELOC_PREFIX_EXPR="/tmp/dellupdatepackage-hapi-*"
typeset -r _CURR_KERNEL_VER=`uname -r`
typeset -r _GCC_VERSION=`cat /proc/version | cut -f7 -d " "`
typeset -r _PREBUILT_DVRS_DIR=$_HOMEDIR/AddnKernelSupport/$_CURR_KERNEL_VER/$_GCC_VERSION
typeset -r _SA_BASE_DIR=/var/omsa/dks/$_CURR_KERNEL_VER
typeset -r _CHECK_IPMI_DRIVER="./ipmi_driver_is_not_loaded" 

function EndScript() {
    bVerbose && echo "Endscript: exitcode $@"
    exit $@
}

#Handle Abnormal exit conditions
trap "EndScript 99" HUP INT QUIT BUS SEGV PIPE TERM # 1 2 3 10 11 13 15

#################################################
# overRideOmiSystemBlock()
# SysID's to override are defined in local array:
#    _OVERRIDE_OMI_SYSIDS
# true ---> override OMI sysid blocking
# false ---> do not override OMI sysid blocking
#################################################
overRideOmiSystemBlock() {
    typeset _SYSTEM_ID
    typeset _SYSTEM_INFO_UTILITY
    typeset _SYSID_SED
    typeset -a _OVERRIDE_OMI_SYSIDS=(19A)

    # Check sysid's and override if necessary
    [ -f ../BIOS/smbiosinfo.bin ] && {
        _SYSTEM_INFO_UTILITY="../BIOS/smbiosinfo.bin -i"
        _SYSID_SED="s/.*\W0*\(.*\)/\1/"
    }
    [ -f ./getSystemId ] && {
        _SYSTEM_INFO_UTILITY="./getSystemId"
        _SYSID_SED="s/.*0*[xX]0*\(.*\)/\1/"
    }
    [ -f ../getSystemId ] && {
        _SYSTEM_INFO_UTILITY="../getSystemId"
        _SYSID_SED="s/.*0*[xX]0*\(.*\)/\1/"
    }
    typeset _SYSTEM_INFO="$($_SYSTEM_INFO_UTILITY)"
    eval _SYSTEM_ID=$(echo -e "$_SYSTEM_INFO" | grep "System *ID" | sed "$_SYSID_SED")

    typeset _sSysID
    for _sSysID in ${_OVERRIDE_OMI_SYSIDS[@]}; do
        [ "$_SYSTEM_ID" = "$_sSysID" ] && return $_TRUE
    done
    return $_FALSE
}

#################################################
# dellOmsEsmIsActive()
# true ---> DellOmsEsm service is running
# false ---> DellOmsEsm service is not running
#################################################
dellOmsEsmIsActive() {
    /etc/init.d/dellomsaesm status >>$_LOGFILE 2>&1
    [ ! $? = 0 ] || { 
        bVerbose && echo "RPM is installed but driver is not running."
        return $_FALSE
    }
    return $_TRUE
}

#################################################
# instSvcIsActive()
# true ---> Instrumentation service is running
# false ---> Instrumentation service is not running
#################################################
instSvcIsActive() {
    ${_INST_DVR_SERVICE_NAME} status >>$_LOGFILE 2>&1
    [  $? = 0 ] || { 
        bVerbose && echo "RPM is installed but driver is not running."
        return $_FALSE
    }
    return $_TRUE
}

#################################################
# If x86_64 HAPI is installed this call will 
# trigger a immediate exit(1)
#################################################
isX86_64Hapi() {
    _DVR_ARCH=$(rpm -q $1 --queryformat=%{ARCH} ) || unset _DVR_ARCH	
    if [ "$_DVR_ARCH" == "x86_64" ]; then
        return $_TRUE
    else
        return $_FALSE
    fi
}

#################################################
# checkDriverInstallStatus()
# 0 ---> If Driver is installed and running
# 1 ---> If Driver is installed but not running
# 2 ---> If Driver is not installed
#################################################
function checkDriverInstallStatus() {
    local hapi_lib
    local _LIB_NAME
    typeset -a hapi_lib_array=(\
        libdchbas.so \
        libdchbas32.so \
        libdchipm.so \
        libdchipm32.so )
    if [ -d ${_HAPI_INST_DIR}  ]; then
        for hapi_lib in ${hapi_lib_array[@]}; do
            _LIB_NAME=`ls ${_HAPI_INST_DIR}/lib` || {
                unset _LIB_NAME
                continue
            }
            break
        done
        if [ -z "${_LIB_NAME}" ]; then
            return 1
        fi
        return 0
    fi
    return 1
}

#################################################
# checkInstallStatus()
# 0 ---> If Driver is installed and running
# 1 ---> If Driver is installed but not running
# 2 ---> If Driver is not installed
#################################################
function checkInstallStatus() {
    local hapi_rpm
    local _DVR_NAME
    typeset -a hapi_rpm_array=(\
        dellomsa-drivers \
        instsvc-drivers \
        srvadmin-hapi-dtk \
        srvadmin-hapi \
        serveradministrator-hapi)

    # first check if any of the above rpms
    # is directly installed
    for hapi_rpm in ${hapi_rpm_array[@]}; do
        _DVR_NAME=$(rpm -q $hapi_rpm --queryformat=%{NAME}) || {
            unset _DVR_NAME
            continue
        }
        bVerbose && rpm -q $_DVR_NAME
        break
    done

    # if none of the above rpms is directly installed,
    # check if instvc-drivers is provided by some other rpm
    if [ -z "${_DVR_NAME}" ]; then
        _DVR_NAME=$(rpm -q --whatprovides instsvc-drivers --queryformat=%{NAME}) || unset _DVR_NAME
    #else
    #    isX86_64Hapi $_DVR_NAME && return 1
    fi

    # Default to instsvc if 
    #  - the instsvcdrv service is present
    #  - no rpm is discovered
    [ -f ${_INST_DVR_SERVICE_NAME} -a -z "$_DVR_NAME" ] && _DVR_NAME=instsvc-drivers

    if [ -z "$_DVR_NAME" ]; then
        bVerbose && echo "No Drivers are installed"
        return 2
    fi

    # Check if disney services are active
    if [ "$_DVR_NAME" == "dellomsa-drivers" ]; then
        dellOmsEsmIsActive || return 1
    else
        instSvcIsActive || return 1
    fi
    bVerbose && echo "RPM is installed and driver is runnning."
    bVerbose && cat ${_LOGFILE} >&2
    return 0
}

#################################################
# CheckIpmiDriverIsLoaded 
# if ipmi driver is not loaded then create file in current directory
#################################################

function CheckIpmiDriverIsLoaded(){

   lsmod | grep  "ipmi" > /dev/null 2>&1 
   if ! [  $? -eq 0 ]; then
        touch $_CHECK_IPMI_DRIVER
	bVerbose && echo "ipmi driver is not loaded"
   fi

}
#################################################
# UnloadIpmiDriver()
# If ipmi dirver is loaded by dup then unload driver at end
#################################################

function UnloadIpmiDriver(){

   if  [ -f $_CHECK_IPMI_DRIVER ]; then
      rmmod  ipmi_si > /dev/null 2>&1
      rmmod  ipmi_devintf > /dev/null 2>&1
      rmmod  ipmi_msghandler > /dev/null 2>&1
      rm -f $_CHECK_IPMI_DRIVER  > /dev/null 2>&1
      bVerbose && echo "ipmi driver is unloaded"
   fi
}

#################################################
# remDriver()
# Make sure this function is redirected to $_LOGFILE
# returns status from rpm erases
#################################################
function remDriver() {
#set -xv
    typeset -i rpmRc=0
    typeset -i tRc=0
    
    UnloadIpmiDriver
    # Check if this install laid down any drivers
    [ -d $_SA_BASE_DIR ] && rm -rf $_SA_BASE_DIR

#    bVerbose && echo Stop the $_INST_DVR_SERVICE_NAME service.
#    ${_INST_DVR_SERVICE_NAME} stop >>${_LOGFILE} 2>&1
#    rpmRc=$?
#    [  $rpmRc = $_TRUE ] || { 
#	    bVerbose && echo "${_INST_DVR_SERVICE_NAME} stop failed:"
#		    bVerbose && echo "Driver could not be stopped."
#    }
#    bVerbose && echo "$_INST_DVR_SERVICE_NAME stop succeeded"
#    bVerbose && cat ${_LOGFILE} >&2
    # cleanup HAPI install prefix directory
    rm -rf ${_HAPI_INST_DIR}
    #echo remDriver returning $rpmRc
    return $rpmRc
}

#################################################
# removeDrivers()
# Make sure this function is redirected to $_LOGFILE
# returns status from rpm erases
#################################################
function removeDrivers() {
    typeset -i rpmRc=0
    typeset -i tRc=0
    typeset -i ipm0=0

    if [ -e $_IPMI0 ]; then
        ipm0=1;
    fi
    # Check if this install laid down any drivers
    [ -d $_SA_BASE_DIR ] && rm -rf $_SA_BASE_DIR
    # uninstall installed RPMS
    echo "Executing: rpm -e ${_HAPI_RPM_PKG_NAME}"
    rpm -e ${_HAPI_RPM_PKG_NAME}
    tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
#    echo "Executing: rpm -e ${_IPMI_RPM_PKG_NAME}"
#    rpm -e ${_IPMI_RPM_PKG_NAME}
#    tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
#    echo "Executing: rpm -e ${_OMILCORE_RPM_PKG_NAME}"
#    rpm -e ${_OMILCORE_RPM_PKG_NAME}
#    tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc

     #make sure the modules are loaded - Xenserver 5.6 does not load them
     modprobe ipmi_msghandler &> /dev/null
     modprobe ipmi_devintf &> /dev/null
     modprobe ipmi_si &> /dev/null

     #create the device node if one does not exist
     if [ ! -f $_IPMI0 ]; then
	if [ 0 -ne $ipm0 ]; then
		echo "Creating $IPMI0" >> ${_LOGFILE} 2>&1
		maj=`sed -n 's/ipmi.*//p' /proc/devices`   >> ${_LOGFILE} 2>&1
		[ ${maj} -eq 0 ] && return $_FALSE
		mknod /dev/ipmi0 c ${maj} 0  >> ${_LOGFILE} 2>&1
		sleep 3
	fi
     fi

    # cleanup HAPI install prefix directory
    rmdir ${_RPM_RELOC_PREFIX_EXPR}

    # cleanup Hapi Configuration File
    if [ -f ${_HAPI_CONFIG_FILE} ]; then
        echo cleaning up ${_HAPI_CONFIG_FILE}
        [ $(cat ${_HAPI_CONFIG_FILE} | wc -l) -eq \
          $(grep -c "^ *${_IPMI_OVERRIDE}" ${_HAPI_CONFIG_FILE}) ] && {
            echo echo ok to rm ${_HAPI_CONFIG_FILE}
            rm -f ${_HAPI_CONFIG_FILE}
        }
    fi
    echo removeDrivers returning $rpmRc
    return $rpmRc
}

#################################################
# copyDriver()
# true ---> Drivers install and start
# false ---> Drivers fail to install or start
#################################################
function copyDriver() {
    typeset -i rpmRc=0
    typeset _RPM_INSTALL_ERROR
    rm -rf ${_HAPI_INST_DIR} &> /dev/null

    CheckIpmiDriverIsLoaded	
    # which will be used as prefix for hapi driver installation
    (mkdir --mode=0700 -p ${_HAPI_INST_DIR} >/dev/null 2>/dev/null) || {
      touch ${_HAPI_OPT_FILE} >/dev/null 2>/dev/null
      if [ $? -eq 0 ]; then
       echo "$0: Cannot create directory ${_HAPI_INST_DIR}. Please ensure there is enough space." >&2
       rm -f ${_HAPI_OPT_FILE} >/dev/null 2>/dev/null
      fi
      # Check if this install laid down any driver
      [ -d $_SA_BASE_DIR ] && rm -rf $_SA_BASE_DIR
      return $_FALSE
   }
    # add sticky bit to secure the directory
    chmod +t ${_HAPI_INST_DIR}
    if [ -d "hapi" ]; then
        cd hapi && tar xzvf ${HAPI_TAR_NAME} > /dev/null 2>&1
        cd .. && cp -R ./hapi/${_HAPI_INST_DIR}/* ${_HAPI_INST_DIR} >>${_LOGFILE} 2>&1
    else
        if [ -d "Hapi" ]; then
            cd Hapi && tar xzvf ${HAPI_TAR_NAME} > /dev/null 2>&1
            cd .. && cp -R ./Hapi/${_HAPI_INST_DIR}/* ${_HAPI_INST_DIR} >>${_LOGFILE} 2>&1
        else
            tar xzvf  ${HAPI_TAR_NAME} > /dev/null 2>&1
            cp -R ./${_HAPI_INST_DIR}/* ${_HAPI_INST_DIR} >>${_LOGFILE} 2>&1
        fi
    fi
    rpmRc=$?
    if [ $rpmRc -eq 0 ]; then
	#Make links to ini and .ipc
	if [ -d "${_HAPI_INST_DIR}/../srvadmin/etc/srvadmin-hapi/ini" ]; then
		rm -rf ${_HAPI_INST_DIR}/etc/srvadmin-hapi/ini &> /dev/null
		ln -sf ${_HAPI_INST_DIR}/../srvadmin/etc/srvadmin-hapi/ini ${_HAPI_INST_DIR}/etc/srvadmin-hapi/ini &> /dev/null
	fi
	if [ -d "${_HAPI_INST_DIR}/../srvadmin/var/lib/openmanage/.ipc" ]; then
		rm -rf ${_HAPI_INST_DIR}/var/lib/openmanage/.ipc &> /dev/null
		ln -sf ${_HAPI_INST_DIR}/../srvadmin/var/lib/openmanage/.ipc ${_HAPI_INST_DIR}/var/lib/openmanage/.ipc &> /dev/null
	fi
        # Enable IPMI Override when required
        [ $OMIIGNOREOPENIPMI -eq  1 ] && {
            bVerbose && echo Enable IPMI override
            echo $_IPMI_OVERRIDE >> ${_HAPI_CONFIG_FILE} 
            export HAPI_ALLOW_USER_MODE=true
        }
        #make sure the modules are loaded - Xenserver 5.6 does not load them
        modprobe ipmi_msghandler &> /dev/null
        modprobe ipmi_devintf &> /dev/null
        modprobe ipmi_si &> /dev/null

	#create the device node if one does not exist
        if [ ! -f $_IPMI0 ]; then
                echo "Creating $IPMI0" >> ${_LOGFILE} 2>&1
		maj=`sed -n 's/ipmi.*//p' /proc/devices`   >> ${_LOGFILE} 2>&1
                mknod /dev/ipmi0 c ${maj} 0  >> ${_LOGFILE} 2>&1
		sleep 3 &> /dev/null
        fi
        # start the instsvcdrv service
        bVerbose && echo Start the $_INST_DVR_SERVICE_NAME service.
        export LD_LIBRARY_PATH=${_HAPI_INST_DIR}/lib:${LD_LIBRARY_PATH}
        ${_INST_DVR_SERVICE_NAME} start >> ${_LOGFILE} 2>&1
        rpmRc=$?
        [  $rpmRc = $_TRUE ] || { 
            bVerbose && echo "$_INST_DVR_SERVICE_NAME start failed:"
            bVerbose && echo "Driver could not be started."
            remDriver >> ${_LOGFILE} 2>&1
            cat ${_LOGFILE} >&2
            return $_FALSE
        }

        bVerbose && echo "$_INST_DVR_SERVICE_NAME start succeeded"
        bVerbose && cat ${_LOGFILE} >&2
        return $_TRUE
    fi

    # Drivers failed to copy
    bVerbose && echo "${_HAPI_INST_DIR} copy error"
    rpmRc=$_FALSE

    # Remove it
    remDriver >> ${_LOGFILE} 2>&1
    cat ${_LOGFILE} >&2

    # Return the RPM Error code
    return $rpmRc
}

#################################################
# installDriver()
# true ---> Drivers install and start
# false ---> Drivers fail to install or start
#################################################
function installDriver() {
    typeset -i rpmRc=0
    typeset -i tRc=0
    typeset _RPM_INSTALL_ERROR
    # Check if Device Drivers RPM is not installed
    checkInstallStatus
    [ $? = 2 ] || {
          bVerbose && echo "Please fix the problems with your Server Administrator installation"
          bVerbose && echo "and run the package again."
          return $_FALSE
    }

    # Check if prebuilt drivers exist
    if [ -d $_PREBUILT_DVRS_DIR ]; then
        [ -d $_SA_BASE_DIR ] || mkdir -p $_SA_BASE_DIR || {
           bVerbose && echo "Unable to create $_SA_BASE_DIR directory"
           return $_FALSE
        }
        cp -f $_PREBUILT_DVRS_DIR/*  $_SA_BASE_DIR 
        [ $? = 0 ] || {
           bVerbose && echo "Error copying prebuilt drivers"
           return $_FALSE
        }
    fi

    # Install the Device Drivers RPM to perform machine check and
    # DKS dependencies check
    # create the /tmp/dellupdatepackage-hapi-$$-$RANDOM dir
    # which will be used as prefix for hapi driver installation
    (mkdir --mode=0700 -p ${_RPM_RELOC_DIR} ) || {
        echo "$0: Cannot create directory ${_RPM_RELOC_DIR}. Please ensure there is enough space." >&2
        # Check if this install laid down any drivers
        [ -d $_SA_BASE_DIR ] && rm -rf $_SA_BASE_DIR
        return $_FALSE
    }
    # add sticky bit to secure the directory
    chmod +t ${_RPM_RELOC_DIR} 
#    rpm -U hapi/${_OMILCORE_RPM_PKG_NAME}*.rpm ${_RPM_RELOC_PREFIX} >>${_LOGFILE} 2>&1
#    tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
#    rpm -U hapi/${_IPMI_RPM_PKG_NAME}*.rpm ${_RPM_RELOC_PREFIX} >>${_LOGFILE} 2>&1
#    tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
#    rpm -U hapi/${_HAPI_RPM_PKG_NAME}*.rpm ${_RPM_RELOC_PREFIX} >>${_LOGFILE} 2>&1
    rpm -U hapi/${_HAPI_RPM_PKG_NAME}*.rpm >>${_LOGFILE} 2>&1
    tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
    if [ $rpmRc -eq 0 ]; then
        # Enable IPMI Override when required
        [ $OMIIGNOREOPENIPMI -eq  1 ] && {
            bVerbose && echo Enable IPMI override
            echo $_IPMI_OVERRIDE >> ${_HAPI_CONFIG_FILE} 
            export HAPI_ALLOW_USER_MODE=true
        }
        #make sure the modules are loaded - Xenserver 5.6 does not load them
        modprobe ipmi_msghandler &> /dev/null
        modprobe ipmi_devintf &> /dev/null
        modprobe ipmi_si &> /dev/null

	#create the device node if one does not exist
        if [ ! -f $_IPMI0 ]; then
                echo "Creating $IPMI0" >> ${_LOGFILE} 2>&1
		maj=`sed -n 's/ipmi.*//p' /proc/devices`   >> ${_LOGFILE} 2>&1
		[ ${maj} -eq 0 ] && return $_FALSE
                mknod /dev/ipmi0 c ${maj} 0  >> ${_LOGFILE} 2>&1
		sleep 3
        fi

        # The RPMs were installed successfully. These RPMs do not
        # start the instsvcdrv service. So, lets start it
        bVerbose && echo Start the $_INST_DVR_SERVICE_BASENAME service.
        #check both paths for the start script
        if [ -f $_INST_DVR_SERVICE_NAME ]; then
                ${_INST_DVR_SERVICE_NAME} start >>$_LOGFILE 2>&1
        else
                if [ -f $_INST_DVR_SERVICE_NAME2 ]; then
                        ${_INST_DVR_SERVICE_NAME2} start >>$_LOGFILE 2>&1
                fi
        fi
        rpmRc=$?
        bVerbose && echo "$_INST_DVR_SERVICE_BASENAME start rc=$rpmRc"
        grep FAILED $_LOGFILE >/dev/null && rpmRc=$_FALSE
        bVerbose && echo "$_INST_DVR_SERVICE_BASENAME FAILED rc=$rpmRc"
        [  $rpmRc = $_TRUE ] || { 
            bVerbose && echo "$_INST_DVR_SERVICE_BASENAME start failed:"
            bVerbose && echo "Driver could not be started."
            removeDrivers >> ${_LOGFILE} 2>&1
            cat ${_LOGFILE} >&2
            return $_FALSE
        }

        bVerbose && echo "$_INST_DVR_SERVICE_BASENAME start succeeded"
        bVerbose && cat ${_LOGFILE} >&2
        return $_TRUE
    fi

    # Drivers failed to install
    # Try to Get Device Drivers RPM error code
    _RPM_INSTALL_ERROR=`grep "ERROR" ${_LOGFILE} | sed 's/.*ERROR //'`
    bVerbose && echo "${_HAPI_RPM_PKG_NAME}*.rpm install error: $_RPM_INSTALL_ERROR"
    rpmRc=$_FALSE
    test $_RPM_INSTALL_ERROR -ge 0 2>/dev/null && rpmRc=$_RPM_INSTALL_ERROR

    # Install Failed but RPM database still reflects this.
    removeDrivers >> ${_LOGFILE} 2>&1
    cat ${_LOGFILE} >&2

    # Return the RPM Error code
    return $rpmRc
}

#######################################################
# Uninstall Driver RPM
# returns status from removeDrivers function
#######################################################
function uninstallDriver() {
    typeset -i rpmRc=0

    #stop instsvcdrv service first
    ${_INST_DVR_SERVICE_NAME} stop >>$_LOGFILE 2>&1

    # now uninstall installed RPMS
    removeDrivers >> ${_LOGFILE} 2>&1
    rpmRc=$?
    bVerbose && cat ${_LOGFILE} >&2
    return $rpmRc ## Return RPM Uninstall Error as is.
}


#######################################################
# Remove damaged 6.1 HAPI RPMs if found
#######################################################
function removeDamagedRPMs()
{
  typeset -i rpmRc=0
  typeset -i tRc=0
  if [ ! -f /etc/init.d/dsm_om_connsvc \
     -a ! -f /etc/init.d/dsm_om_shrsvc \
     -a ! -f /etc/init.d/dataeng ]; then
    HAPI_PATH=`echo ${_RPM_RELOC_PREFIX_EXPR}`
    if [ -d "${HAPI_PATH}" ]; then
      srvadmin_rpm_list=(\
          srvadmin-hapi-6.1.0 \
          srvadmin-ipmi-6.1.0 \
	  srvadmin-omilcore-6.1.0)
      \cp -rf hapi/funcs ${HAPI_PATH}
      tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
      echo "Executing: cp -rf hapi/funcs ${HAPI_PATH}"
      for srvadmin_rpm in ${srvadmin_rpm_list[@]}; do
        if [ -n "`rpm -q ${srvadmin_rpm}`" ]; then
          \rpm -e `rpm -q ${srvadmin_rpm}`
          tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
          echo "Executing: rpm -e ${srvadmin_rpm}"
        fi
      done
      \rm -rf ${_RPM_RELOC_PREFIX_EXPR}
      tRc=$?; [ $tRc -ne 0 ] && rpmRc=$tRc
      echo "Executing: rm -rf ${_RPM_RELOC_PREFIX_EXPR}"
    fi
  fi
  return $rpmRc
}


#######################################################
#   main() processing
#######################################################

#  Check for root user privilege for execution of script
[ "$UID" -eq "$_ROOT_UID" ]  || {
  echo "$0 : Script execution requires 'root' user privileges."
  EndScript 1
}

# Validate for script invocation with right nos. of parameters
if [ ! $# = 1 ]; then
   echo "Usage: $0 [install/uninstall/checkstatus]"
   EndScript 1
fi

removeDamagedRPMs >> ${_LOGFILE} 2>&1

# Make sure all the dependencies are available on the system
_DEPS="rm rpm basename"
which $_DEPS > /dev/null
[ $? == 0 ] || {
    echo "$0 : Cannot find utilities on the system to execute package." >&2
    echo "Make sure the following utilities are on the path: $_DEPS" >&2
    EndScript 1
}

# Override OMI system blocking for selected machines.
export OMIIGNORESYSID=1

# Always override IPMI for BIOS
OMIIGNOREOPENIPMI=0 # default
[ -f ./biosie.bin ] && export OMIIGNOREOPENIPMI=1

# create the log file directory and initialize the log file
/bin/mkdir -p $_LOGFILE_DIR
date > ${_LOGFILE}
echo $0 >> ${_LOGFILE}
## Do not clean up logfile 
##  - when using verbose processing.
##  - when installing Hapi.
## Clean up logfile 
##  - when uninstalling Hapi.
##  - when checking status of Hapi.
case $1 in
     install) copyDriver
        _EXITCODE=$?
        ;;
     uninstall) remDriver
        _EXITCODE=$?
        bVerbose || rm -f $_LOGFILE
        ;;
     installOld) installDriver
        _EXITCODE=$?
        ;;
     uninstallOld) uninstallDriver
        _EXITCODE=$?
        bVerbose || rm -f $_LOGFILE
        ;;
     checkstatus) checkDriverInstallStatus
        _EXITCODE=$?
        bVerbose || rm -f $_LOGFILE
        ;;
     checkstatusOld) checkInstallStatus
        _EXITCODE=$?
        bVerbose || rm -f $_LOGFILE
        ;;
     *) echo "Usage: $0 [install|uninstall|checkstatus]"
        _EXITCODE=$_FALSE
        ;;
esac
EndScript $_EXITCODE
