#!/bin/bash
#
# This script helps to install/uninstall brocade drivers.
# user can choose to install among the available brocade drivers
#
# Usage: <this script> --help
#

VERSION=3.2.7.1
bna=1
bfa=1
util=1
FILENAME=$0
bfarpm="bfa_driver_linux-$VERSION-0.noarch.rpm"
bnarpm="bna_driver_linux-$VERSION-0.noarch.rpm"
kver=`uname -r`
ioctl_flag=`echo $kver | awk -F "." '{print $3}' | awk -F "-" '{print $1}'`
linux_ver=`echo $kver | awk -F "." '{print $1}'`
if [ ${ioctl_flag} -lt 32 -a ${linux_ver} -lt 3 ] ; then
	utilrpm="bfa_util_linux-$VERSION-0.noarch.rpm"
else
	utilrpm="bfa_util_linux_noioctl-$VERSION-0.noarch.rpm"
fi
i=4
VENDOR_ID="1657"
CNA_DEVID_CT="0014"
CNA_DEVID_CT2="0022"
UPDATE_INITRD=0
SNMP=0
MFG_INSTALL=0
INSTALL_TO_KERNEL_VER=""
RPM_CHK_REGEX=""
KER_VERSION=""
VARIANT=""
CHECKED_SPACE="NO"

# Error codes
NO_KERNEL_RELEASE=5
NO_KERNEL_SRPM=6
NO_GCC=7
NO_KERNEL_RELEASE_GCC=8
NO_KERNEL_SRPM_GCC=9
BNA_INSTALL_FAILED=10
BFA_INSTALL_FAILED=11
UTIL_INSTALL_FAILED=12
BNA_UNLOAD_FAILED=13
BFA_UNLOAD_FAILED=14
UTIL_INSTALL_ABORT=15
NO_BNA_RPM_FOUND=16
NO_BFA_RPM_FOUND=17
NO_UTIL_RPM_FOUND=18
INITRD_BACKUP_PRESENT=19
# SNMP Error codes
UNKNOWN_OS_VERSION=20
UNSUPPORTED_OS_VERSION=21
BNA_DRIVER_NOT_FOUND=22
UNSUPPORTED_BNA_DRIVER=23
SNMP_RPM_ALREADY_INSTALLED=24
REQD_NET_SNMP_NOT_FOUND=25
SNMP_RPM_UNINSTALL_FAILED=26
SNMP_RPM_FILE_MISSING=27
SNMP_RPM_INSTALL_FAILED=28
SPACE_INSUFFICIENT=29

# State variables
BFA_DRIVER_INSTALL_STATUS="Brocade Storage Driver Install Not Invoked"
BNA_DRIVER_INSTALL_STATUS="Brocade Network Driver Install Not Invoked"
UTILITY_INSTALL_STATUS="Brocade Utilities Install Not Invoked"
SNMP_INSTALL_STATUS="Brocade SNMP Install Not Invoked"

# Default value for all architecures to support BNA noarch
ARCH=`uname -m`
if [ $ARCH == "ia64" ] || [ $ARCH == "ppc64" ]; then
	BNA_NOARCH_SUPPORTED=0
else
	BNA_NOARCH_SUPPORTED=1
fi

# INSTALL_MODE = 1 /* Default mode i.e. just place files in file system */
# INSTALL_MODE = 0 /* Place files in file system + load modules         */
INSTALL_MODE=0
kmod_kmp_install=0
kmod_kmp_uninstall=0
# INITRD_UPDATE_DONE   /* If set - indicates initrd update is done */ 
# INITRD_UPDATE_NEEDED /* If set - indicates rpm install did not update the initrd due to lack of bfa entry */
INITRD_UPDATE_DONE=0
INITRD_UPDATE_NEEDED=0
INITRD_BACKUP_NEEDED=0
HP_INITRD_UPDATE_DONE=0
BFA_INBOX_PRESENT=0
RHEV=0
REBOOT_REQUIRED="NO"
FOUND_CNA_CARD="FALSE"
DELL_INSTALL=0
NO_PROMPT_INSTALL=0

MV="/bin/mv"
BFA_KIT="/opt/brocade/adapter/bfa"

# brocade_install.log -> logs all the events happening.
# under the /var/log/brocade dir.

if ! [ -d /var/log/brocade ]; then
	mkdir /var/log/brocade
fi

LOG="/var/log/brocade/brocade_install.log"

RH7=0
if ! [ "`cat /etc/os-release 2>/dev/null | grep \"7.[0-9]\"`" == "" ]; then
       	RH7=1
	MKINITRD=/usr/bin/mkinitrd
else
	MKINITRD=/sbin/mkinitrd
fi
RH6=0
if ! [ "`cat /etc/redhat-release 2>/dev/null | grep \"6.[0-9]\"`" == "" ]; then
	RH6=1
fi
BFA_INBOX_RH=0
BNA_INBOX_RH=0
if ! [ "`cat /etc/redhat-release 2>/dev/null | grep '5.[5-9]'`" == "" ] || [ $RH6 == "1" ] || [ "$RH7" == "1" ]; then
	BFA_INBOX_RH=1
fi
if ! [ "`cat /etc/redhat-release 2>/dev/null | grep '7.[0-1]'`" == "" ] ||
   ! [ "`cat /etc/redhat-release 2>/dev/null | grep '6.[1-9]'`" == "" ] ||
   ! [ "`cat /etc/redhat-release 2>/dev/null | grep '5.[7-9]'`" == "" ] ; then
	BNA_INBOX_RH=1
fi

if [ "`rpm -qa |grep kernel-uek`" ]; then
	if [ `echo $kver | cut -d "." -f1` -gt 2 ] || [ `echo $kver | cut -d "." -f3 | cut -d "-" -f1` -ge 32 ]; then
		BFA_INBOX_RH=1
		BNA_INBOX_RH=1
	fi
fi

BFA_INBOX_SL=0
BNA_INBOX_SL=0
if [ -f /etc/SuSE-release ]; then
	SLES_VERSION=`cat /etc/SuSE-release |grep "VERSION" | awk '{print $3}'`
	if [ ${SLES_VERSION} == 11 ]; then
		SLES_PATCH_LEVEL=`cat /etc/SuSE-release |grep "PATCHLEVEL" | awk '{print $3}'`
		if [ ${SLES_PATCH_LEVEL} -ge 1 ]; then 
			BFA_INBOX_SL=1
			BNA_INBOX_SL=1
		fi
	elif [ ${SLES_VERSION} == 12 ]; then
		BFA_INBOX_SL=1
		BNA_INBOX_SL=1
	fi
fi

if [ -f `find /lib/modules/$kver/kernel/drivers/net/*/ -name bna.k*` ]; then
	bna_inbox_path=`find /lib/modules/$kver/kernel/drivers/net/*/ -name bna.k* | xargs dirname`
	if [ -f /etc/redhat-release ]; then
		BNA_INBOX_RH=1
	elif [ -f /etc/SuSE-release ]; then
		BNA_INBOX_SL=1
	fi
else
	if [ -f /etc/redhat-release ]; then
		BNA_INBOX_RH=0
	elif [ -f /etc/SuSE-release ]; then
		BNA_INBOX_SL=0
	fi
fi

function usage
{
	echo "Usage:"
	echo "  $FILENAME [--update-initrd] [--snmp]"
	echo "		Installs all RPMs(bfa, bna and util)"
	echo "		if --update-initrd is specified: updates/adds bfa to initrd."
	echo "		if --snmp is specified: installs bna SNMP AgentX sub-agent RPM."
	echo 
	echo "		Note: RPM installation automatically updates initrd if bfa is listed under"
	echo "		/etc/sysconfig/kernel for (SUSE) and /etc/modprobe.conf for (RHEL).  "
	echo

	echo "  $FILENAME --mfg [--update-initrd]"
	echo "		Installs Brocade (bfa, bna) mfg RPMs and util rpm"
	echo "		if --update-initrd is specified: updates/adds bfa to initrd."
	echo
	echo "		Note: RPM installation automatically updates initrd if bfa is listed under"
	echo "		/etc/sysconfig/kernel for (SUSE) and /etc/modprobe.conf for (RHEL).  "
	echo

	echo "  $FILENAME --install-to-kernel <kernel-version> [--update-initrd]"
	echo "		Installs Brocade (bfa, bna) RPMs and util rpm for specified kernel-version"
	echo "		if --update-initrd is specified: updates/adds bfa to initrd."
	echo
	echo "		Note: RPM installation automatically updates initrd if bfa is listed under"
	echo "		/etc/sysconfig/kernel for (SUSE) and /etc/modprobe.conf for (RHEL).  "
	echo

	echo "  $FILENAME --snmp-only"
	echo "		Installs or updates bna SNMP AgentX sub-agent RPM only"
	echo

	echo "  $FILENAME -u [--snmp]"
	echo "		Uninstalls all the installed RPM's (bfa, util, bna, bna-snmp)."
	echo "		if --snmp is specified: uninstalls the installed bna-snmp RPM only"
	echo

	echo "  $FILENAME --force-uninstall"
	echo "		Uninstalls all the installed RPM's (bfa, util, bna, bna-snmp)."
	echo "		Might need a reboot if unload of module bfa/bna fails."
	echo 

	echo "  $FILENAME --add-initrd"
	echo "		Add bfa to the initrd modules and rebuild initrd only."
	echo
	echo "  $FILENAME --rm-initrd"
	echo "		Remove bfa from the initrd modules and rebuild initrd only."
	echo
	echo "  $FILENAME -h or --help"
	echo "		Displays help."
	echo
}

# Function that logs the status of the drivers/utilities installed.

function brcd_install_status 
{
	echo -en "########  SUMMARY OF THE BROCADE INSTALL  ########\n" >> ${LOG}
	echo -en "BNA_DRIVER_INSTALL_STATUS: ${BNA_DRIVER_INSTALL_STATUS}\n" >> ${LOG}
	echo -en "BFA_DRIVER_INSTALL_STATUS: ${BFA_DRIVER_INSTALL_STATUS}\n" >> ${LOG}
	echo -en "UTILITY_INSTALL_STATUS: ${UTILITY_INSTALL_STATUS}\n" >> ${LOG}
	echo -en "SNMP_INSTALL_STATUS: ${SNMP_INSTALL_STATUS}\n" >> ${LOG}
	echo -en "Reboot_required:$REBOOT_REQUIRED\n" >> ${LOG}

	if [ $INITRD_UPDATE_DONE -eq 1 ]; then
		echo -en "INITRD_UPDATED:YES\n" >> ${LOG}
	else
		echo -en "INITRD_UPDATED:NO\n" >> ${LOG}
	fi
}

# Function to compute the kernel variant
function brcd_init_kernel_variant
{
	KER_VERSION=`uname -r`

	# If INSTALL_TO_KERNEL_VER is set - override KER_VERSION.
	if ! [ "$INSTALL_TO_KERNEL_VER" == "" ]; then
		KER_VERSION=$INSTALL_TO_KERNEL_VER
	fi

	# VARIANT is set to default for redhat release
	if [ -f /etc/redhat-release ]; then
		if ! [ `echo $KER_VERSION | grep "2.6.18"` ]; then
			VARIANT=default
		fi
	fi

	# Compute the VARIANT
	if ! [ "`echo $KER_VERSION | grep bigsmp`" == "" ]; then
		VARIANT=bigsmp
	elif ! [ "`echo $KER_VERSION | grep largesmp`" == "" ]; then
		VARIANT=largesmp
	elif ! [ "`echo $KER_VERSION | grep hugemem`" == "" ]; then
		VARIANT=hugemem
	elif ! [ "`echo $KER_VERSION | grep smp`" == "" ]; then
		VARIANT=smp
	elif ! [ "`echo $KER_VERSION | grep debuginfo`" == "" ]; then
		VARIANT=debuginfo
	elif ! [ "`echo $KER_VERSION | grep debugsource`" == "" ]; then
		VARIANT=debugsource
	elif ! [ "`echo $KER_VERSION | grep debug`" == "" ]; then
		VARIANT=debug
	elif ! [ "`echo $KER_VERSION | grep xenpae`" == "" ]; then
		VARIANT=xenpae
	elif ! [ "`echo $KER_VERSION | grep xen`" == "" ]; then
		VARIANT=xen
	elif ! [ "`echo $KER_VERSION | grep vmipae`" == "" ]; then
		VARIANT=vmipae
	elif ! [ "`echo $KER_VERSION | grep vmi`" == "" ]; then
		VARIANT=vmi
	elif ! [ "`echo $KER_VERSION | grep pae`" == "" ]; then
		VARIANT=pae
	elif ! [ "`echo $KER_VERSION | grep trace`" == "" ]; then
		VARIANT=trace
	elif ! [ "`echo $KER_VERSION | grep ppc64`" == "" ]; then
		#FIXME: Need to investigate how ppc64 is one of the variants which is
		#coming from favors_to_build in the spec filek
		VARIANT=ppc64
	elif ! [ "`echo $KER_VERSION | grep default`" == "" ]; then
		VARIANT=default
	fi

	# Compute the regular expression - to query all the installed
	# binary rpms for this particular kernel.
	# Handling of RHEL5 & RHEL6 is different from other RHEL/SUSE flavors
	# as the rpm names differ.
	if [ -f /etc/redhat-release ]; then

		if [ `echo $KER_VERSION | grep "2.6.18"` ]; then	# RHEL5 handling
			klevel=`echo $KER_VERSION|awk -F '-' '{ print $2 }' |awk -F '.' '{ print $1 }'`
			if [ "${klevel}" -lt 164 ] && [ "${klevel}" -ge 128 ]; then
				DISTRO="rhel5u3-"
			elif [ "${klevel}" -lt 194 ] && [ "${klevel}" -ge 164 ]; then
				DISTRO="rhel5u4-"
			elif [ "${klevel}" -lt 238 ] && [ "${klevel}" -ge 194 ]; then
				DISTRO="rhel5u5-"
			elif [ "${klevel}" -lt 274 ] && [ "${klevel}" -ge 238 ]; then
				DISTRO="rhel5u6-"
			elif [ "${klevel}" -lt 298 ] && [ "${klevel}" -ge 274 ]; then
				DISTRO="rhel5u7-"
			elif [ "${klevel}" -lt 348 ] && [ "${klevel}" -ge 298 ]; then
				DISTRO="rhel5u8-"
			elif [ "${klevel}" -lt 371 ] && [ "${klevel}" -ge 348 ]; then
				DISTRO="rhel5u9-"
			elif [ "${klevel}" -lt 398 ] && [ "${klevel}" -ge 371 ]; then
				DISTRO="rhel5u10-"
			elif [ "${klevel}" -ge 398 ]; then
				DISTRO="rhel5u11-"
			else
				DISTRO=""
			fi
			RPM_CHK_REGEX=".*.${VARIANT}.*.${DISTRO}.*"

		elif [ `echo $KER_VERSION | grep "2.6.32"` ]; then	# RHEL6 handling
			klevel=`echo $KER_VERSION|awk -F '-' '{ print $2 }' |awk -F '.' '{ print $1 }'`
			if [ "${klevel}" -ge 71 ]; then
				DISTRO=""
			else
				DISTRO="unsupported-distro"
			fi
			RPM_CHK_REGEX=".*.${DISTRO}.*.el6.*"
		elif [ `echo $KER_VERSION | grep "3.10.0"` ]; then	# RHEL7 handling
			klevel=`echo $KER_VERSION|awk -F '-' '{ print $2 }' |awk -F '.' '{ print $1 }'`
			if [ "${klevel}" -ge 123 ]; then
				DISTRO=""
			else
				DISTRO="unsupported-distro"
			fi
			RPM_CHK_REGEX=".*.${DISTRO}.*.el7.*"
		else							# All other OS flavors
			KER_VERSION=`echo ${KER_VERSION/$VARIANT}|tr '-' '_'`
			RPM_CHK_REGEX=".*.${VARIANT}.*.${KER_VERSION}.*"
		fi

	elif [ -f /etc/SuSE-release ]; then

		SLES10_SP1_BaseKernel="2.6.16.46_0.12"
		SLES10_SP2_BaseKernel="2.6.16.60_0.21"
		SLES10_SP3_BaseKernel="2.6.16.60_0.54.5"
		SLES10_SP4_BaseKernel="2.6.16.60_0.85.1"
		SLES11_BaseKernel="2.6.27.19_5"
		SLES11_SP1_BaseKernel="2.6.32.12_0.7"
		SLES11_SP2_BaseKernel="3.0.13_0.27"
		SLES11_SP3_BaseKernel="3.0.76_0.11"
		SLES11_SP4_BaseKernel="3.0.101_63"
		SLES12_BaseKernel="3.12.28_4"
		SLES12_SP1_BaseKernel="3.12.49_11"

		SLES_VERSION=`cat /etc/SuSE-release |grep "VERSION" | awk '{print $3}'`
		SLES_PATCH_LEVEL=`cat /etc/SuSE-release |grep "PATCHLEVEL" | awk '{print $3}'`

		# Override KER_VERSION to handle errata kernel install case.
		if [ $SLES_VERSION -eq 10 ] ; then
			if [ $SLES_PATCH_LEVEL -eq 1 ] ; then
				KER_VERSION=$SLES10_SP1_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 2 ] ; then
				KER_VERSION=$SLES10_SP2_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 3 ] ; then
				KER_VERSION=$SLES10_SP3_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 4 ] ; then
				KER_VERSION=$SLES10_SP4_BaseKernel
			fi
		elif [ $SLES_VERSION -eq 11 ] ; then
			if [ $SLES_PATCH_LEVEL -eq 0 ] ; then
				KER_VERSION=$SLES11_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 1 ] ; then
				KER_VERSION=$SLES11_SP1_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 2 ] ; then
				KER_VERSION=$SLES11_SP2_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 3 ] ; then
				KER_VERSION=$SLES11_SP3_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 4 ] ; then
				KER_VERSION=$SLES11_SP4_BaseKernel
			fi
		elif [ $SLES_VERSION -eq 12 ] ; then
			if [ $SLES_PATCH_LEVEL -eq 0 ] ; then
				KER_VERSION=$SLES12_BaseKernel
			elif [ $SLES_PATCH_LEVEL -eq 1 ] ; then
				KER_VERSION=$SLES12_SP1_BaseKernel
			fi
		fi

		KER_VERSION=`echo ${KER_VERSION/-$VARIANT}|tr '-' '_'`
		RPM_CHK_REGEX=".*.${VARIANT}.*.${KER_VERSION}.*"
	fi
}

# Function to uninstall the currently installed brcd RPM's

function brcd_rpm_uninstall 
{

	force_uninstall=1
	flag=0

	# Unloaded the currently loaded bfa/bna modules.

	# Don't unload modules if INSTALL_TO_KERNEL_VER is set -
	# as install is to different kernel.
	if [ "$INSTALL_TO_KERNEL_VER" == "" ]; then

	if [ "$1" != "$force_uninstall" ]; then

	# Stopping the HCM agent service if it is running.
	if [ -f /usr/bin/hcmagentservice ]; then
		if ! [ "`hcmagentservice status | grep running`" == "" ]; then
			echo -en "Stopping the HCM agent service ..." | tee -a ${LOG}
			/usr/bin/hcmagentservice stop > /dev/null 2>&1
			echo -en "done\n" | tee -a ${LOG}
		fi
	fi

	# uninstall SNMP sub-agent
	brcd_snmp_uninstall

	# Remove system info entry if present under /etc/modprobe.conf
	if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
		if [ "`grep 'options bfa' /etc/modprobe.d/modprobe.conf 2>/dev/null`" != "" ]; then
			sed '/options bfa / d' /etc/modprobe.d/modprobe.conf 2>/dev/null > /tmp/modprobe.conf.tmp
			${MV} -f /tmp/modprobe.conf.tmp /etc/modprobe.d/modprobe.conf
		fi
	else
		if [ "`grep 'options bfa' /etc/modprobe.conf`" != "" ]; then
			sed '/options bfa / d' /etc/modprobe.conf > /tmp/modprobe.conf.tmp
			${MV} -f /tmp/modprobe.conf.tmp /etc/modprobe.conf
		fi
	
	fi

        # Unloaded the currently loaded bfa/bna modules.

	if ! [ "`lsmod |grep bna`" == "" ]; then
		echo -en "Unloading bna driver ... " | tee -a ${LOG}
		rmmod bna > /dev/null 2>&1
		if [ $? != 0 ] ; then

			if [ "$1" != "$force_uninstall" ]; then
				echo -en "\nERROR: Module BNA could not be unloaded; Uninstall terminated!" | tee -a ${LOG}
				echo -en "\ntry $FILENAME --force-uninstall\n" | tee -a ${LOG}
				exit ${BNA_UNLOAD_FAILED}
			else
				echo -en "\nERROR: Module BNA could not be unloaded! \n" | tee -a ${LOG}
				flag=1
                        fi
		fi

		if [ "$flag" != "1" ]; then
			echo -en "done\n" | tee -a ${LOG}
		fi
	fi

	if ! [ "`lsmod |grep bfa`" == "" ]; then
		echo -en "Unloading bfa driver ... " | tee -a ${LOG}
		rmmod bfa > /dev/null 2>&1
		if [ $? != 0 ] ; then

			if [ "$1" != "$force_uninstall" ]; then
				echo -en "\nERROR: Module BFA could not be unloaded; Uninstall terminated!" | tee -a ${LOG}
				echo -en "\ntry $FILENAME --force-uninstall\n" | tee -a ${LOG}
				exit ${BFA_UNLOAD_FAILED}
			else
				echo -en "\nERROR: Module BFA could not be unloaded! \n" | tee -a ${LOG}
				flag=1
			fi
		fi

		if [ "$flag" != "1" ]; then
			echo -en "done\n" | tee -a ${LOG}
		fi
	fi

	fi # Force Uninstall

	fi # INSTALL_TO_KERNEL_VER

	# The array below lists the possible brocade rpm's that might be
	# installed on the system.

	rpm_uninstall_types=( bfa_util_linux bfa_driver_linux bna_driver_linux dud-bfa brocade-bfa brocade-bna kmod-bfa kmod-brocade-bfa kmod-bna kmod-brocade-bna dud-bna dud-brocade-bfa )
	echo "Uninstalling the installed Brocade RPM's" | tee -a ${LOG}

	# Uninstalling the Brocade rpm's
	for brcd_rpm in ${rpm_uninstall_types[@]}
	do
		# Removing brocade driver RPM's
		bfa_rpms=`rpm -qa|grep $brcd_rpm`
                for bfa_rpm in ${bfa_rpms}
                do
                        if [ "`echo $bfa_rpm | grep bfa_util_linux`" != "" ] ||
                           [ "`echo $bfa_rpm | grep bfa_driver_linux`" != "" ] ||
                           [ "`echo $bfa_rpm | grep bna_driver_linux`" != "" ] ||
                           [ "`echo $bfa_rpm | grep $RPM_CHK_REGEX`" != "" ] ; then

				if ! [ "`echo $bfa_rpm|grep dud-bfa`" == "" ]; then
					kmod_kmp_uninstall=1
				fi

				if ! [ "`echo $bfa_rpm|grep kmod|grep bfa`" == "" ]; then
					kmod_kmp_uninstall=1
				fi

				if ! [ "`echo $bfa_rpm|grep bfa|grep kmp`" == "" ]; then
					kmod_kmp_uninstall=1
				fi

				echo "Removing $bfa_rpm RPM" | tee -a ${LOG}
				if [ "$kmod_kmp_install" == "1" -a "$kmod_kmp_uninstall" == "1" ]; then
					rpm -e --nopostun $bfa_rpm
					if [ -f /etc/redhat-release ]; then
						rm -f /var/run/rpm-kmod-b[fn]a*modules
						rm -f /var/run/rpm-dud-b[fn]a*modules
					elif [ -f /etc/SuSE-release ]; then
						rm -f /var/run/rpm-brocade-b[fn]a-kmp*modules
					fi
				else
					rpm -e $bfa_rpm
				fi
			fi
		done
	done

	# Remove dangling symbolic links for bfa, bna if any; can occur if
	# driver update happens from older kmod rpms (built using ddiskit-0.9.9)
	# to the ones built from ddiskit-1.0. Since we skip the --nopostun part
	# during kmod rpms uninstall where cleanup used to happen.
	for i in `find /lib/modules/ -type l -name b[fn]a.ko`;
	do
		[ -e $i ] || rm -f $i;
	done

	# If flag is set to 1: it means unload of atleast one module failed
	# Recommend user to reboot to cleanup state.

	if [ "$flag" == "1" ]; then
		echo -en "We strongly recommend a reboot to avoid any issues.\n" | tee -a ${LOG}
	fi
}

# Function uninstalls bna-snmp rpm only

function brcd_snmp_uninstall
{
	local found_snmp="`rpm -qa bna-snmp\* | grep bna-snmp 2> /dev/null`"

	if [ $? -eq 0 -a -n "${found_snmp}" ]; then
		rpm -e ${found_snmp} | tee -a ${log}
		if [ $? -eq 0 ]; then
			echo "${found_snmp} rpm uninstalled" | tee -a ${LOG}
		else
			echo "failed to uninstall ${found_snmp} rpm" | tee -a ${LOG}
		fi
	else
		echo "bna-snmp rpm not installed" | tee -a ${LOG}
	fi
}


# Function to check for system dependencies.

function check_system_dep
{
	# Checking if the kernel source and gcc exists

	ARCH=$(uname -m | sed "s/i686/i386/")
	UNAME=$(uname -r)
	KERNEL_IMAGE=""
	errorcode=0
	kernel_devel=""

	if [ -f /etc/redhat-release ]; then
		if [ ${ARCH} = "ia64" ] ; then
			KERNEL_IMAGE="/boot/efi/efi/redhat/vmlinuz-${UNAME}"
		else
			KERNEL_IMAGE="/boot/vmlinuz-${UNAME}"
		fi

		kernel_devel=$(rpm -qf ${KERNEL_IMAGE} --qf "%{NAME}-devel-%{VERSION}\n")
		if [ $? -ne 0 ]; then
			errorcode=1
		else
			rpm -q ${kernel_devel} >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				errorcode=2
			fi
		fi
	elif [ -f /etc/SuSE-release ]; then
		KERNEL_IMAGE="/boot/vmlinuz-${UNAME}"
		kernel_devel=$(rpm -qf ${KERNEL_IMAGE} --qf "kernel-source-%{VERSION}\n")
		if [ $? -ne 0 ] ; then
			errorcode=1
		else
			rpm -q ${kernel_devel} >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				#no kernel_devel
				errorcode=2
			fi
		fi
	fi

	rpm -q gcc >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		if [ ${errorcode} -eq 0 ]; then
			#no gcc only
			errorcode=3 
		elif [ ${errorcode} -eq 1 ]; then
			#no gcc, and can not determine kernel version
			errorcode=4 
		elif [ ${errorcode} -eq 2 ]; then
			#no gcc, and no kernel_devel
			errorcode=5 
		fi
	fi

	if [ ${errorcode} -eq 1 ]; then
		echo "Warning: unable to determine kernel release" | tee -a ${LOG}
	elif [ ${errorcode} -eq 2 ]; then
		echo "Warning: no ${kernel_devel} rpm" | tee -a ${LOG}
	elif [ ${errorcode} -eq 3 ]; then
		echo "ERROR: Install failure: no gcc" | tee -a ${LOG}
		exit ${NO_GCC}
	elif [ ${errorcode} -eq 4 ]; then
		echo "ERROR: Install failure: no gcc and unable to determine kernel release" | tee -a ${LOG}
		exit ${NO_KERNEL_RELEASE_GCC}
	elif [ ${errorcode} -eq 5 ]; then
		echo "ERROR: Install failure: no gcc and ${kernel_devel} rpm" | tee -a ${LOG}
		exit ${NO_KERNEL_SRPM_GCC}
	fi
}


# Func checks if any Brocade bfa/bna/util rpms are installed
# Uninstalls the old ones / Terminates installation based on 
# users input choice.

function brcd_prev_rpm_check
{
	found_brcd_rpm=0
	
	# Checking for existance of previous brocade drivers
 
	# The array below lists the possible brocade rpm's that might be
	# installed on the system.

	rpm_install_types=( bfa_util_linux bfa_driver_linux bna_driver_linux dud-bfa brocade-bfa brocade-bna kmod-bfa kmod-brocade-bfa kmod-bna kmod-brocade-bna dud-bna dud-brocade-bfa bna-snmp )

	# Checking for previous the Brocade rpm's
	for brcd_rpm in ${rpm_install_types[@]}
	do
		bfa_rpms="`rpm -qa|grep $brcd_rpm`"
		for bfa_rpm in ${bfa_rpms}
		do
			if [ "`echo $bfa_rpm | grep bfa_util_linux`" != "" ] ||
			   [ "`echo $bfa_rpm | grep bfa_driver_linux`" != "" ] ||
			   [ "`echo $bfa_rpm | grep bna_driver_linux`" != "" ] ||
			   [ "`echo $bfa_rpm | grep $RPM_CHK_REGEX`" != "" ] ; then
				if [ $found_brcd_rpm == 0 ] ; then
					echo "Found Previously Installed Brocade RPM's:" | tee -a ${LOG}
					found_brcd_rpm=1
				fi
				echo $bfa_rpm
			fi
		done
	done

	if [ $found_brcd_rpm == 1 ] ; then

		if [ $NO_PROMPT_INSTALL == 1 ]; then
			echo "Removing Installed Brocade RPM's." | tee -a ${LOG}
			response="y"
		else
			echo "Remove old and continue fresh driver installation? (y/n) >" | tee -a ${LOG}
			read response
		fi

		if [ "$response" != "y" ]; then
			echo "Terminating Installation!" | tee -a ${LOG}
			brcd_install_status
			exit 1
		else
			# Driver behavior init
			brcd_install_init
			if [ $INSTALL_MODE == 1 ]; then
				force=1
			else
				force=0
			fi
			brcd_rpm_uninstall $force
		fi
	else
		# Driver behavior init
		brcd_install_init
	fi
}

# Function to populate the system info entry
# in /etc/modprobe.conf

function populate_system_info
{
	os_name=""
	os_patch=""
	if [ -f /etc/oracle-release ]; then
		os_name=`cat /etc/oracle-release`
	elif [ -f /etc/redhat-release ]; then
		os_name=`cat /etc/redhat-release`
	elif [ -f /etc/SuSE-release ]; then
		os_name=`cat /etc/SuSE-release | grep -i "^SUSE Linux"`
		os_patch=`cat /etc/SuSE-release | grep "^PATCHLEVEL"`
	fi

	if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
		sed '/options bfa / d' /etc/modprobe.d/modprobe.conf 2>/dev/null > /tmp/modprobe.conf.tmp
	else
		sed '/options bfa / d' /etc/modprobe.conf > /tmp/modprobe.conf.tmp
	fi

	# BFA Module params
	BFA_MOD_PARAM="options bfa"

	host_name=`uname -n`

	if [[ ${host_name} != "" ]]; then
		BFA_MOD_PARAM="${BFA_MOD_PARAM}"" host_name=\"${host_name}\""
	fi

	if [[ ${os_name} != "" ]]; then
		BFA_MOD_PARAM="${BFA_MOD_PARAM}"" os_name=\"${os_name}\""
	fi

	if [[ ${os_patch} != "" ]]; then
		BFA_MOD_PARAM="${BFA_MOD_PARAM}"" os_patch=\"${os_patch}\""
	fi

	if [[ ${BFA_MOD_PARAM} != "options bfa" ]]; then
		echo "${BFA_MOD_PARAM}" >> /tmp/modprobe.conf.tmp
	fi

	if [ "${RH6}" == "1" ] || [ "$RH7" == "1" ]; then
		${MV} -f --suffix=.bak --backup=existing /tmp/modprobe.conf.tmp /etc/modprobe.d/modprobe.conf
	else
		${MV} -f --suffix=.bak --backup=existing /tmp/modprobe.conf.tmp /etc/modprobe.conf
	fi
}

# Function that install the BNA driver.
# exits with ret_val 1 if install fails.

function install_brcd_bna
{
	# BNA driver rpm that is to be installed.
	bna_driver_rpm=$bnarpm
		
	#Install the bna driver RPM
	
	if ! [ -f ${bna_driver_rpm} ]; then
		echo "ERROR: Install failed: Cannot locate the bna driver rpm!" | tee -a ${LOG}
		BNA_DRIVER_INSTALL_STATUS="Brocade Network Driver Install Failed" 
	
		echo "WARNING: Reverting back all the changes made during install!" | tee -a ${LOG}
	
		# Peel back any previously installed Brocade rpm's
		force=0
		brcd_rpm_uninstall $force

		brcd_install_status
		exit ${NO_BNA_RPM_FOUND}
	else

		echo "Installing the BNA driver RPM: ${bna_driver_rpm}" | tee -a ${LOG}
		rpm -ivh --force ${bna_driver_rpm}
		if [ $? != 0 ] || [ -f /var/log/brocade/mk.log ] ; then
			echo "ERROR: BNA driver rpm install failed: Terminating!" | tee -a ${LOG}
			BNA_DRIVER_INSTALL_STATUS="Brocade Network Driver Install Failed"

			echo "WARNING: Reverting back all the changes made during install!" | tee -a ${LOG}

			# Peel back any previously installed Brocade rpm's
			force=0
			brcd_rpm_uninstall $force

			brcd_install_status
			exit ${BNA_INSTALL_FAILED}
		fi
	fi

	# Handling RHEV: Create a symbolic link under the current running RHEV kernel;
	# and do a depmod for the bna module to be located by modprobe.
	if [ "$RHEV" == "1" ]; then
		DUD_PATH=`rpm -qlp ${bna_driver_rpm} | grep bna.ko`
		if [ -f $DUD_PATH ]; then
			BNA_PATH="/lib/modules/`uname -r`/extra"
			mkdir -p ${BNA_PATH}
			ln -s $DUD_PATH ${BNA_PATH}/bna.ko
			/sbin/depmod
		fi
	fi
	
	# Setting the state of the BNA driver install to success
	BNA_DRIVER_INSTALL_STATUS="Brocade Network Driver Install Success"
}

# Function that install the BFA driver.
# exits with ret_val 1 if install fails.

function install_brcd_bfa
{
	# BFA driver rpm that needs to be installed.
	bfa_driver_rpm=$bfarpm

	#Install the bfa driver RPM

	if ! [ -f ${bfa_driver_rpm} ]; then
		echo "ERROR: Install failed: Cannot locate the bfa driver rpm!" | tee -a ${LOG}
		BFA_DRIVER_INSTALL_STATUS="Brocade Storage Driver Install Failed"

		echo "WARNING: Reverting back all the changes made during install!" | tee -a ${LOG}

		# Peel back any previously installed Brocade rpm's
		force=0
		brcd_rpm_uninstall $force

		brcd_install_status
		exit ${NO_BFA_RPM_FOUND}
	else

		echo "Installing the BFA driver RPM: ${bfa_driver_rpm}" | tee -a ${LOG}
		rpm -ivh --force ${bfa_driver_rpm}
		if [ $? != 0 ] || [ -f /var/log/brocade/mk.log ] ; then
			echo "ERROR: BFA driver rpm install failed: Terminating!" | tee -a ${LOG}
			BFA_DRIVER_INSTALL_STATUS="Brocade Storage Driver Install Failed"

			echo "WARNING: Reverting back all the changes made during install!" | tee -a ${LOG}

			# Peel back any previously installed Brocade rpm's
			force=0
			brcd_rpm_uninstall $force

			brcd_install_status
			exit ${BFA_INSTALL_FAILED}
		fi
	fi

	# Handling RHEV: Create a symbolic link under the current running RHEV kernel;
	# and do a depmod for the bfa module to be located by modprobe.
	if [ "$RHEV" == "1" ]; then
		DUD_PATH=`rpm -qlp ${bfa_driver_rpm} | grep bfa.ko`
		if [ -f $DUD_PATH ]; then
			BFA_PATH="/lib/modules/`uname -r`/extra"
			mkdir -p ${BFA_PATH}
			ln -s $DUD_PATH ${BFA_PATH}/bfa.ko
			/sbin/depmod
		fi
	fi

	# Setting the state of the BFA driver install to success
	BFA_DRIVER_INSTALL_STATUS="Brocade Storage Driver Install Success"
}

# Function that install the UTIL
# exits with ret_val 1 if install fails.

function install_brcd_util
{
	# Util driver rpm that needs to be installed.

	util_driver_rpm=$utilrpm

	# Installing the util driver RPM.
	if ! [ -f ${util_driver_rpm} ]; then
		echo "ERROR: Install failed: Cannot locate the util rpm!" | tee -a ${LOG}
		UTILITY_INSTALL_STATUS="Brocade Utilities Install Failed"
		brcd_install_status
		exit ${NO_UTIL_RPM_FOUND}
	else

		# We check if either BFA or BNA driver is loaded, if not
		# we dont install the UTIL rpm.

		if [ "`rpm -qa|grep bfa`" != "" ] || [ "`rpm -qa|grep bna`" != "" ] ; then

			echo "Installing the util driver RPM" | tee -a ${LOG}
			rpm -ivh ${util_driver_rpm}
			if [ $? != 0 ] ; then
				echo "UTIL rpm install failed: Terminating!" | tee -a ${LOG}
				UTILITY_INSTALL_STATUS="Brocade Utilities Install Failed"
				brcd_install_status
				exit ${UTIL_INSTALL_FAILED}
			fi
		else
			echo "ERROR: Util installation terminated: Dont have Brocade driver rpms installed!" | tee -a ${LOG}
			UTILITY_INSTALL_STATUS="Brocade Utilities Install Failed"
			brcd_install_status
			exit ${UTIL_INSTALL_ABORT}
		fi
	fi

	# Setting the state of the utilities install to success
	UTILITY_INSTALL_STATUS="Brocade Utilities Install Success"
}

# Function to log error and exit with error code
function snmp_log_exit_error
{
	echo "$1" | tee -a ${log}
	SNMP_INSTALL_STATUS="$2"
	brcd_install_status
	exit $3
}

# Function that install the SNMP
function install_brcd_snmp
{
	local check=0
	local action="Install"
	if [ $# -eq 1 -a "$1" == "check" ]; then
		#check_system_dep should have already been done by BASI
		check=1
		action="Check"
	fi

	if [ ! -x "`which lsb_release 2> /dev/null`" ]; then
		snmp_log_exit_error "can't determine OS version: lsb_release missing" \
			"Brocade SNMP ${action} Failed" \
			${UNKNOWN_OS_VERSION}
	fi

	local version="`lsb_release -rs 2> /dev/null`"
	local distro=""
	if [ -f /etc/redhat-release ]; then
		[[ ${version} =~ ^4 ]] && distro="rhel4"
		[[ ${version} =~ ^5 ]] && distro="rhel5"
		[[ ${version} =~ ^6 ]] && distro="rhel6"
		[[ ${version} =~ ^7 ]] && distro="rhel7"
	elif [ -f /etc/SuSE-release ]; then
		[[ ${version} =~ ^10 ]] && distro="sles10"
		if [[ ${version} =~ ^11 ]]; then
			patch="`awk '/PATCHLEVEL/{print $3}' /etc/SuSE-release 2> /dev/null`"
			case "$patch" in
			"4")
				distro="sles11sp4"
				;;
			"3")
				distro="sles11sp3"
				;;
			"2")
				distro="sles11sp2"
				;;
			"1")
				distro="sles11sp1"
				;;
			"0")
				distro="sles11"
				;;
			esac
		elif [[ ${version} =~ ^12 ]]; then
			patch="`awk '/PATCHLEVEL/{print $3}' /etc/SuSE-release 2> /dev/null`"
			case "$patch" in
			"0")
				distro="sles12"
				;;
			"1")
				distro="sles12sp1"
				;;
			esac
		fi
	fi

	if [ -z "${distro}" ]; then
		snmp_log_exit_error "unsupported OS version" \
			"Brocade SNMP ${action} Failed" \
			${UNSUPPORTED_OS_VERSION}
	fi

	local driver_version="`modinfo -F version bna 2> /dev/null`"
	if [ $? -ne 0 -o -z "${driver_version}" ]; then
		snmp_log_exit_error "could not find bna.ko kernel module" \
			"Brocade SNMP ${action} Failed" \
			${BNA_DRIVER_NOT_FOUND}
	fi

	if [ ${driver_version:0:1} -ne 3 ]; then
		snmp_log_exit_error "unsupported driver version ${driver_version}" \
			"Brocade SNMP ${action} Failed" \
			${UNSUPPORTED_BNA_DRIVER}
	fi

	local found_snmp="`rpm -qa bna-snmp\* | grep bna-snmp 2> /dev/null`"
	local status=$?

	if [ ${check} -eq 1 ]; then
		if [ ${status} -eq 0 -a -n "${found_snmp}" ]; then
			local installed="`rpm --queryformat="%{version}" -q ${found_snmp} 2> /dev/null`"
			if [ "${installed}" == "${VERSION}" ]; then
				snmp_log_exit_error "${found_snmp} found installed" \
					"Brocade SNMP ${action} Failed" \
					${SNMP_RPM_ALREADY_INSTALLED}
			fi
		fi
		rpm -qa net-snmp\* | grep net-snmp-[5-9] > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			snmp_log_exit_error "required net-snmp package not found" \
				"Brocade SNMP ${action} Failed" \
				${REQD_NET_SNMP_NOT_FOUND}
		fi
		snmp_log_exit_error "bna-snmp rpm check successful" \
			"Brocade SNMP ${action} Success" \
			0 #should be able to install
	fi

	if [ ${status} -eq 0 -a -n "${found_snmp}" ]; then
		echo "uninstalling ${found_snmp}" | tee -a ${log}
		rpm -e ${found_snmp} | tee -a ${log}

		if [ $? -ne 0 ]; then
			snmp_log_exit_error "uninstalling ${found_snmp} failed" \
				"Brocade SNMP ${action} Failed" \
				${SNMP_RPM_UNINSTALL_FAILED}
		fi
	fi

	local arch="`rpm --eval "%{_arch}"`"

	local snmp_rpm="bna-snmp-${VERSION}-${distro}.${arch}.rpm"

	if [ ! -f ${snmp_rpm} ]; then
		snmp_log_exit_error "${snmp_rpm} file: missing" \
			"Brocade SNMP ${action} Failed" \
			${SNMP_RPM_FILE_MISSING}
	fi

	rpm -ivh ${snmp_rpm} | tee -a ${log}

	if [ $? -ne 0 ]; then
		snmp_log_exit_error "installing ${snmp_rpm} failed" \
			"Brocade SNMP ${action} Failed" \
			${SNMP_RPM_INSTALL_FAILED}
	fi

	# Setting the state of the utilities install to success
	SNMP_INSTALL_STATUS="Brocade SNMP ${action} Success"
}


# Function that updates the HP-initrd;
# back up the previous initrd if the parameter to
# back up is set else just update the initrd.

function brcd_bfa_hp_initrd_update
{
	KERNELRELEASES=`uname -r`
	HP_INITRD=HP-initrd-${KERNELRELEASES}.img
	backup=0

	# HP initrd update is done
	if [ $HP_INITRD_UPDATE_DONE -eq 1 ]; then
		return;
	fi

	# If INSTALL_TO_KERNEL_VER is passed override
	# KERNELVERSIONS with INSTALL_TO_KERNEL_VER param 2.
	if [ "$2" != "" ] ; then
		KERNELRELEASES=$2
		HP_INITRD=HP-initrd-${KERNELRELEASES}.img
	fi

        if [ "`ls /boot/HP-initrd-$KERNELRELEASES.img-* 2>/dev/null | grep -v old | grep -v bak | sort | tail -n 1`" != "" ]; then
		cd /boot > /dev/null 2>&1
		HP_INITRD=`ls HP-initrd-$KERNELRELEASES.img-* | grep -v old | grep -v bak | sort | tail -n 1`
		cd - > /dev/null 2>&1
	fi

	# Update the HP-INITRD.
	if [ -f /boot/${HP_INITRD} ]; then

		if ! [ ${backup} == $1 ]; then
			echo "Saving current HP-initrd to /boot/${HP_INITRD}.bak" | tee -a ${LOG}
			cp -f /boot/${HP_INITRD} /boot/${HP_INITRD}.bak
		fi

		if [ -f /opt/hp/hp-fc-enablement/make_initrd ]; then
			/opt/hp/hp-fc-enablement/make_initrd
			HP_INITRD_UPDATE_DONE=1

		elif [ -f /etc/SuSE-release ]; then
			echo -n "HP-initrd update for SUSE ..." | tee -a ${LOG}
			${MKINITRD} -k /boot/vmlinuz-${KERNELRELEASES} -i /boot/${HP_INITRD}.tmp > /dev/null 2>&1
			${MV} -f /boot/${HP_INITRD}.tmp /boot/${HP_INITRD}
			if [ -h /boot/initrd ]; then
				ln -sf /boot/${HP_INITRD} /boot/initrd
			fi
			HP_INITRD_UPDATE_DONE=1
			echo -en " done\n" | tee -a ${LOG}

		elif [ -f /etc/redhat-release ]; then
			echo -n "HP-initrd update for RHEL ..." | tee -a ${LOG}
			${MKINITRD} --allow-missing -f /boot/${HP_INITRD}.tmp ${KERNELRELEASES} > /dev/null 2>&1
			${MV} -f /boot/${HP_INITRD}.tmp /boot/${HP_INITRD}
			if [ -h /boot/initrd ]; then
				ln -sf /boot/${HP_INITRD} /boot/initrd
			fi
			HP_INITRD_UPDATE_DONE=1
			echo -en " done\n" | tee -a ${LOG}
		fi
	fi
}

# Function that updates the initrd by adding bfa;
# We also save the current initrd before update 
# to initrd.old.

function brcd_add_bfa_initrd_update
{

	# IMPORTANT: Do not change the KERNELRELEASES definition; it will be
	# replaced during driver reuse!
	# set variables
	KERNELRELEASES=`uname -r`

	initrd_module="bfa"

	# We check if the bfa module is installed before updating
	# the initrd.
	path="/lib/modules/`uname -r`"
	if ! [ "`find $path -name bfa.ko`" == "" ]; then

		# update the module dependencies for the target kernel/flavor
		if [ -e /boot/System.map-${KERNELRELEASES} ]; then
			/sbin/depmod -a -F /boot/System.map-${KERNELRELEASES} ${KERNELRELEASES}
		fi

		if [ -f /etc/SuSE-release ]; then

			# Updating the initrd i.e. adding bfa when we find the bfa is listed
			# under the /etc/sysconfig/kernel

			if [ -e /etc/sysconfig/kernel -a -e /boot/initrd-${KERNELRELEASES} ]; then

				source /etc/sysconfig/kernel

				run_mkinitrd=
				for module in $INITRD_MODULES; do
					case " ${initrd_module} " in
					*" $module "*)
						run_mkinitrd=1
						break ;;
					esac
				done

				if ! [ -n "$run_mkinitrd" ]; then

					MODULE="bfa"
					(
						source /etc/sysconfig/kernel
						sed --in-place=.orig /etc/sysconfig/kernel -e "s/INITRD_MODULES.*/INITRD_MODULES=\"$INITRD_MODULES $MODULE\"/g"
					)
				fi

				echo -n "initrd update ...." | tee -a ${LOG}

				for image in vmlinuz image vmlinux linux bzImage; do
					if [ -f /boot/$image-${KERNELRELEASES} ]; then
						${MKINITRD} -k /boot/$image-${KERNELRELEASES} \
								-i /boot/initrd-${KERNELRELEASES}.tmp > /dev/null 2>&1 \
						|| exit 1
						break
					fi
				done

				${MV} -f /boot/initrd-${KERNELRELEASES}.tmp /boot/initrd-${KERNELRELEASES}
				if [ -h /boot/initrd ]; then
					ln -sf /boot/initrd-${KERNELRELEASES} /boot/initrd
				fi

				echo -en " done\n" | tee -a ${LOG}
			fi
		fi


		if [ -f /etc/redhat-release ]; then
			if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
				modulefile=/etc/modprobe.d/modprobe.conf
			else
				modulefile=/etc/modprobe.conf
			fi

			if [ -f $modulefile ]; then

				scsimodules=`grep "alias[[:space:]]\+scsi_hostadapter" $modulefile | grep -v '^[        ]*#' | LC_ALL=C sort -u | awk '{ print $3 }'`

				if [ -n "$scsimodules" ]; then

					run_mkinitrd=
					for module in $scsimodules; do
						case " ${initrd_module} " in
						*" $module "*)
							run_mkinitrd=1
							break ;;
						esac
					done
				fi

				if ! [ -n "$run_mkinitrd" ]; then
					# add modules to modules.conf
					if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
						echo "alias scsi_hostadapter_brcd_bfa bfa" >> /etc/modprobe.d/modprobe.conf
					else
						echo "alias scsi_hostadapter_brcd_bfa bfa" >> /etc/modprobe.conf
					fi
				fi

				if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
					echo -n "initrd update ...." | tee -a ${LOG}
				
					# mkinitrd for redhat
					${MKINITRD} --allow-missing -f /boot/initramfs-${KERNELRELEASES}.img.tmp ${KERNELRELEASES} > /dev/null 2>&1
					${MV} -f /boot/initramfs-${KERNELRELEASES}.img.tmp /boot/initramfs-${KERNELRELEASES}.img
					if [ -h /boot/initrd ]; then
						ln -sf /boot/initramfs-${KERNELRELEASES}.img /boot/initrd
					fi
					# if system has efi initrd copy the initrd under boot to efi
					if [ -f /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img ]; then
						cp -f /boot/initramfs-${KERNELRELEASES}.img /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img > /dev/null 2>&1
					fi
				else
					echo -n "initrd update ...." | tee -a ${LOG}

					# mkinitrd for redhat
					${MKINITRD} --allow-missing -f /boot/initrd-${KERNELRELEASES}.img.tmp ${KERNELRELEASES} > /dev/null 2>&1
					${MV} -f /boot/initrd-${KERNELRELEASES}.img.tmp /boot/initrd-${KERNELRELEASES}.img
					if [ -h /boot/initrd ]; then
						ln -sf /boot/initrd-${KERNELRELEASES}.img /boot/initrd
					fi
					# if system has efi initrd copy the initrd under boot to efi
					if [ -f /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img ]; then
						cp -f /boot/initrd-${KERNELRELEASES}.img /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img > /dev/null 2>&1
					fi
				fi
				echo -en " done\n" | tee -a ${LOG}
			fi	
		fi

		# HP_initrd update if present.
		backup=1
		brcd_bfa_hp_initrd_update $backup

	else
		echo -en "ERROR: Module bfa not installed; please install bfa and try updating initrd !\n" | tee -a ${LOG}
		exit 1
	fi
}

# Function that updates the initrd by removing bfa.

function brcd_remove_bfa_initrd_update
{

	rm_bfa=0

	# IMPORTANT: Do not change the KERNELRELEASES definition; it will be
	# replaced during driver reuse!
	# set variables
	KERNELRELEASES=`uname -r`

	initrd_module="bfa"

	# update the module dependencies for the target kernel/flavor
	if [ -e /boot/System.map-${KERNELRELEASES} ]; then
		/sbin/depmod -a -F /boot/System.map-${KERNELRELEASES} ${KERNELRELEASES}
	fi

	# initrd update for SUSE linux.

	if [ -f /etc/SuSE-release ]; then

		# Updating the initrd i.e. removing bfa when we find the bfa is listed
		# under the /etc/sysconfig/kernel

		if [ -e /etc/sysconfig/kernel -a -e /boot/initrd-${KERNELRELEASES} ]; then

			source /etc/sysconfig/kernel

			run_mkinitrd=
			for module in $INITRD_MODULES; do
				case " ${initrd_module} " in
				*" $module "*)
					run_mkinitrd=1
					break ;;
				esac
			done

			if [ -n "$run_mkinitrd" ]; then

				if ! [ ${rm_bfa} == "$1" ]; then
					MODULE="bfa"
					sed --in-place=.orig /etc/sysconfig/kernel -e "s/$MODULE/""/g"
				fi

				echo -n "initrd update ...." | tee -a ${LOG}

				for image in vmlinuz image vmlinux linux bzImage; do
					if [ -f /boot/$image-${KERNELRELEASES} ]; then
						${MKINITRD} -k /boot/$image-${KERNELRELEASES} \
								-i /boot/initrd-${KERNELRELEASES}.tmp > /dev/null 2>&1 \
						|| exit 1
						break
					fi
				done

				${MV} -f /boot/initrd-${KERNELRELEASES}.tmp /boot/initrd-${KERNELRELEASES}
				if [ -h /boot/initrd ]; then
					ln -sf /boot/initrd-${KERNELRELEASES} /boot/initrd
				fi

				echo -en " done\n" | tee -a ${LOG}
			fi
		fi
	fi

	# initrd update for RHEL linux.

	if [ -f /etc/redhat-release ]; then
		if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
			modulefile=/etc/modprobe.d/modprobe.conf
		else
			modulefile=/etc/modprobe.conf
		fi
		if [ -f $modulefile ]; then

			scsimodules=`grep "alias[[:space:]]\+scsi_hostadapter" $modulefile | grep -v '^[        ]*#' | LC_ALL=C sort -u | awk '{ print $3 }'`

			if [ -n "$scsimodules" ]; then

				run_mkinitrd=
				for module in $scsimodules; do
					case " ${initrd_module} " in
					*" $module "*)
						run_mkinitrd=1
						break ;;
					esac
				done
			fi

			if [ -n "$run_mkinitrd" ]; then

				if ! [ ${rm_bfa} == $1 ]; then
					# remove modules from /etc/modprobe.conf
					if [ $RH6 == 1 ] || [ "$RH7" == "1" ];  then
						sed -i -e '/alias scsi_hostadapter_brcd_bfa.*$/d' /etc/modprobe.d/modprobe.conf 2>/dev/null
					else
						sed -i -e '/alias scsi_hostadapter_brcd_bfa.*$/d' /etc/modprobe.conf
					fi
				fi

				echo -n "initrd update ...." | tee -a ${LOG}

				# mkinitrd for redhat
				if [ "$RH6" == 1 ] || [ "$RH7" == "1" ];  then
					${MKINITRD} --allow-missing -f /boot/initramfs-${KERNELRELEASES}.img.tmp ${KERNELRELEASES} > /dev/null 2>&1

					${MV} -f /boot/initramfs-${KERNELRELEASES}.img.tmp /boot/initramfs-${KERNELRELEASES}.img
					if [ -h /boot/initrd ]; then
						ln -sf /boot/initramfs-${KERNELRELEASES}.img /boot/initrd
					fi

				# if system has efi initrd copy the initrd under boot to efi; after updating the one
				# under boot; which will not have bfa after bfa.ko gets removed for RHEL.
					if [ -f /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img ]; then
						cp -f /boot/initramfs-${KERNELRELEASES}.img /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img > /dev/null 2>&1
					fi
				else
					${MKINITRD} --allow-missing -f /boot/initrd-${KERNELRELEASES}.img.tmp ${KERNELRELEASES} > /dev/null 2>&1

					${MV} -f /boot/initrd-${KERNELRELEASES}.img.tmp /boot/initrd-${KERNELRELEASES}.img
					if [ -h /boot/initrd ]; then
						ln -sf /boot/initrd-${KERNELRELEASES}.img /boot/initrd
					fi

				# if system has efi initrd copy the initrd under boot to efi; after updating the one
				# under boot; which will not have bfa after bfa.ko gets removed for RHEL.
					if [ -f /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img ]; then
						cp -f /boot/initrd-${KERNELRELEASES}.img /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img > /dev/null 2>&1
					fi
				fi

				echo -en " done\n" | tee -a ${LOG}
			fi
		fi
	fi

	# HP_initrd update if present.
	backup=1
	brcd_bfa_hp_initrd_update $backup
}

# Function that will backup the current running initrd to
# initrd-XXX.bak; If a backup already exists, based on
# the user input if he wants to overwrite we do that when 
# the force flag is passed in.   

function brcd_initrd_backup
{

	KERNELRELEASES=`uname -r`
	initrd_name=""
	force_backup=1

	# Fetch the right initrd name
	if [ -f /etc/SuSE-release ]; then
		initrd_name="initrd-${KERNELRELEASES}"
	elif [ -f /etc/redhat-release ]; then
		if [ $RH6 == 1 ] || [ "$RH7" == "1" ];  then
			initrd_name="initramfs-${KERNELRELEASES}.img"
		else
			initrd_name="initrd-${KERNELRELEASES}.img"
		fi
	fi

	# Check for any existing Brocade backup; else
	# back up the initrd.

	if [ -f /boot/"$initrd_name.bak" ]; then

		# If force flag is set; overwrite the current backup;
		# else exit with error code 1.
		if [ "$1" == "$force_backup" ]; then
			cp -f /boot/$initrd_name /boot/"$initrd_name.bak"

			# Backup the EFI initrd if the system is EFI.
			if [ -f /boot/efi/efi/redhat/$initrd_name ]; then
				cp -f /boot/efi/efi/redhat/$initrd_name /boot/efi/efi/redhat/"$initrd_name.bak"
			elif [ -f /boot/efi/efi/SuSE/$initrd_name ]; then
				cp -f /boot/efi/efi/SuSE/$initrd_name /boot/efi/efi/SuSE/"$initrd_name.bak"
			fi

			echo -en "initrd backup complete\n" | tee -a ${LOG}
			echo -en "Backup file name : $initrd_name.bak\n" | tee -a ${LOG}
		else 	
			echo -en "Brocade initrd previous backup present\n"
			echo -en "Backup file name : $initrd_name.bak\n"
			exit $INITRD_BACKUP_PRESENT;
		fi
	else
		cp /boot/$initrd_name /boot/"$initrd_name.bak"

		# Backup the EFI initrd if the system is EFI.
		if [ -f /boot/efi/efi/redhat/$initrd_name ]; then
			cp -f /boot/efi/efi/redhat/$initrd_name /boot/efi/efi/redhat/"$initrd_name.bak"
		elif [ -f /boot/efi/efi/SuSE/$initrd_name ]; then
			cp -f /boot/efi/efi/SuSE/$initrd_name /boot/efi/efi/SuSE/"$initrd_name.bak"
		fi

		echo -en "initrd backup complete\n" | tee -a ${LOG}
		echo -en "Backup file name : $initrd_name.bak\n" | tee -a ${LOG}
	fi
}

# Function that checks if we need to backup initrd and 
# takes a backup if during this current install/uninstall
# we update the current running initrd.

function brcd_check_and_backup_running_initrd
{
	# Cases where we need to backup the initrd:
	# 1. If INSTALL_MODE = 1, we assume its Boot Over SAN setup and update initrd during install.
	# 2. When we have inbox BFA present, since inbox BFA will be part of initrd we update
	#    initrd during install.
	# 3. When UPDATE_INITRD variable is set (user invoked script with --update-initrd)
	# 4. When we find bfa entry in the modprobe.conf or /etc/sysconfig/kernel files
	force_backup=$1
	if [ -f /etc/redhat-release ]; then
		if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
			modulefile=/etc/modprobe.d/modprobe.conf
		else
			modulefile=/etc/modprobe.conf
		fi
		if [ -f $modulefile ]; then
			if ! [ "`grep "alias[[:space:]]\+scsi_hostadapter" $modulefile | grep bfa`" == "" ]; then
				INITRD_BACKUP_NEEDED=1
			fi
		fi
	elif [ -f /etc/SuSE-release ]; then
		if ! [ "`cat /etc/sysconfig/kernel| grep ^INITRD_MODULES | grep bfa`" == "" ]; then
			INITRD_BACKUP_NEEDED=1
		fi
	fi

	if [ $INSTALL_MODE -eq 1 ] || [ $UPDATE_INITRD -eq 1 ] ||
	   [ "$BFA_INBOX_PRESENT" == "1" ] || [ $INITRD_BACKUP_NEEDED -eq 1 ] ||
	   [ $force_backup -eq 1 ] ; then
			# Calling func that does the backup update
			if ! [ "$RHEV" == "1" ]; then
				brcd_initrd_backup 1
			fi
	fi
}

# Function that does the init part i.e setting the
# flags that determine the behavior of the script.

function brcd_install_init
{
	# check if the system has cna card; then only install bna driver.
	# Check for CNA_DEVID_CT
	if [ "`lspci -n | grep $VENDOR_ID:$CNA_DEVID_CT`" != "" ]; then
		FOUND_CNA_CARD="TRUE"
	else
		# CT2 can be in only FC mode - so check if the Ethernet fn exists.
		if [ "`lspci -n | grep $VENDOR_ID:$CNA_DEVID_CT2`" != "" ] &&
		   [ "`lspci | grep Brocade | grep "Ethernet controller"`" != "" ]; then
			FOUND_CNA_CARD="TRUE"
		fi
	fi

	# Skip rest of brcd_install_init if INSTALL_TO_KERNEL_VER is set
	# Since we are installing to a different kernel.
	if [ "$INSTALL_TO_KERNEL_VER" == "" ]; then

	ARCH=$(uname -m | sed "s/i686/i386/")

	# Stopping the HCM agent service if it is running.
	if [ -f /usr/bin/hcmagentservice ]; then
		if ! [ "`hcmagentservice status | grep running`" == "" ]; then
			echo -en "Stopping the HCM agent service ..." | tee -a ${LOG}
			/usr/bin/hcmagentservice stop > /dev/null 2>&1
			echo -en "done\n" | tee -a ${LOG}
		fi
	fi

	# Check the usage count of the bfa & bna modules after stopping the hcmagent;
	# if greater than 0 then try to just place the files in file system
	# else unload the modules; install new rpm's and load the modules.

	if ! [ "`lsmod |grep bfa`" == "" ]; then

		bfa_usage_cnt=`lsmod | grep ^bfa | awk '{print $3}'`

		if ! [ $bfa_usage_cnt == 0 ]; then
			# default mode just place files in file system
			INSTALL_MODE=1
			REBOOT_REQUIRED="YES"
			echo -en "WARN: Module BFA could not be unloaded!\n" | tee -a ${LOG}
		else
			echo -en "Unloading bfa driver ..." | tee -a ${LOG}
			rmmod bfa > /dev/null 2>&1
			if [ $? != 0 ] ; then
				echo -en "WARN: Module BFA could not be unloaded!\n" | tee -a ${LOG}
				INSTALL_MODE=1
				REBOOT_REQUIRED="YES"
			fi
			echo -en "done\n" | tee -a ${LOG}
		fi
	fi

	if ! [ "`lsmod |grep bna`" == "" ]; then

		bna_usage_cnt=`lsmod | grep ^bna | awk '{print $3}'`

		if ! [ $bna_usage_cnt == 0 ]; then
			# default mode just place files in file system
			INSTALL_MODE=1
			REBOOT_REQUIRED="YES"
			echo -en "WARN: Module BNA could not be unloaded!\n" | tee -a ${LOG}
		else
			echo -en "Unloading bna driver ..." | tee -a ${LOG}
			rmmod bna > /dev/null 2>&1
			if [ $? != 0 ] ; then
				echo -en "WARN: Module BNA could not be unloaded!\n" | tee -a ${LOG}
				INSTALL_MODE=1
				REBOOT_REQUIRED="YES"
			fi
			echo -en "done\n" | tee -a ${LOG}
		fi
	fi

	# For (> RHEL5.5): We have bfa module compiled into the kernel; so we need to manually rename
	# so that it does not disrupt loading of newer Brocade modules.
	if [ -f /etc/redhat-release ]; then
		if [ "$BFA_INBOX_RH" == "1" ]; then
			bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
 			if [ -f $bfa_inbox_path/bfa.ko ] ; then
				${MV} -f $bfa_inbox_path/bfa.ko $bfa_inbox_path/bfa.k_inbox
				BFA_INBOX_PRESENT=1
			elif [ -f $bfa_inbox_path/bfa.k_inbox ] ; then
				BFA_INBOX_PRESENT=1
			fi
		fi
		if [ "$BNA_INBOX_RH" == "1" ]; then
 			if [ -f $bna_inbox_path/bna.ko ] ; then
				${MV} -f $bna_inbox_path/bna.ko $bna_inbox_path/bna.k_inbox
				/sbin/depmod
			fi
		fi
	fi
	if [ -f /etc/SuSE-release ]; then
		if [ "$BFA_INBOX_SL" == "1" ]; then
                	bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
                	if [ -f $bfa_inbox_path/bfa.ko ] ; then
                       		${MV} -f $bfa_inbox_path/bfa.ko $bfa_inbox_path/bfa.k_inbox
                       		BFA_INBOX_PRESENT=1
                	fi
		fi
		if [ "$BNA_INBOX_SL" == "1" ]; then
			if [ -f $bna_inbox_path/bna.ko ]; then
				${MV} -f $bna_inbox_path/bna.ko $bna_inbox_path/bna.k_inbox
				/sbin/depmod
			fi
		fi
	fi

	if [ -f /etc/fedora-release ]; then
		bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
		if [ -f $bfa_inbox_path/bfa.ko ]; then
			${MV} -f $bfa_inbox_path/bfa.ko $bfa_inbox_path/bfa.k_inbox
			/sbin/depmod
		fi

		if [ -f $bna_inbox_path/bna.ko ]; then
			${MV} -f $bna_inbox_path/bna.ko $bna_inbox_path/bna.k_inbox
			/sbin/depmod
		fi
	fi

	# Removed weak-updates drivers 
	if [ -d /lib/modules/`uname -r`/weak-updates ]; then
		find /lib/modules/`uname -r`/weak-updates -name bfa.ko -exec rm -f {} \;
		find /lib/modules/`uname -r`/weak-updates -name bna.ko -exec rm -f {} \;
	fi

	# For RHEL 4; if dud is used the bfa.ko will be placed under /lib/modules/2.6.9-XX.ELxxx/updates/
	# This is not installed using an rpm so needs to be removed using script before the new bfa driver
	# is installed. Check is done for all kernels to make sure only intented bfa.ko is present.
	
	path="/lib/modules/`uname -r`/updates"
	if [ -d $path ]; then 
		if ! [ "`find $path -name bfa.ko`" == "" ]; then
			rm -f $path/bfa.ko >/dev/null 2>&1
		fi
	fi

	# For RHEV: Removing the symbolic links created during install
	# and then do depmod.
	if [ "$RHEV" == "1" ]; then
		RHEV_PATH="/lib/modules/`uname -r`/extra"
		if [ -h ${RHEV_PATH}/bna.ko ]; then
			rm -fr ${RHEV_PATH}/bna.ko
		fi
		if [ -h ${RHEV_PATH}/bfa.ko ]; then
			rm -fr ${RHEV_PATH}/bfa.ko
		fi
		/sbin/depmod
	fi

	# BACKUP INITRD: Check if we need to backup the current running initrd -
	# We backup the current running initrd if we are going to update the initrd
	# during the current installation or uninstallation.
	brcd_check_and_backup_running_initrd 0

	fi #for - INSTALL_TO_KERNEL_VER
}

# Function to check the available space to create initrd
# It exists with error if space is insufficient

check_space()
{
	if [ "$CHECKED_SPACE" = "YES" ]; then
		return
	fi

	KERNELRELEASES=`uname -r`

	# we need to check initrd images for four kernel types/version: 2.6.18 - 2.6.32 - HP initrd - suse
	req=`du -sk /boot/initrd-${KERNELRELEASES}.img /boot/initramfs-${KERNELRELEASES}.img /boot/HP-initrd-${KERNELRELEASES}.img /boot/initrd-${KERNELRELEASES} 2> /dev/null | awk '{req += $1} END {printf("%d ", (req/1024)+5)}'`
	efi_rh_req=`du -sk /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img /boot/efi/efi/redhat/initrd-${KERNELRELEASES} 2> /dev/null | awk '{req += $1} END {printf("%d ", (req/1024))}'`
	efi_suse_req=`du -sk /boot/efi/efi/SuSE/initrd-${KERNELRELEASES}.img /boot/efi/efi/SuSE/initramfs-${KERNELRELEASES}.img /boot/efi/efi/SuSE/initrd-${KERNELRELEASES} 2> /dev/null | awk '{req += $1} END {printf("%d ", (req/1024))}'`
	bak_avail=`du -sk /boot/initrd-${KERNELRELEASES}.img*bak /boot/initramfs-${KERNELRELEASES}.img*bak /boot/HP-initrd-${KERNELRELEASES}.img*bak /boot/initrd-${KERNELRELEASES}*bak 2> /dev/null | awk '{req += $1} END {printf("%d", req/1024)}'`
	bak_efi_rh_avail=`du -sk /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img*bak /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img*bak /boot/efi/efi/redhat/initrd-${KERNELRELEASES}*bak 2> /dev/null | awk '{req += $1} END {printf("%d", req/1024)}'`
	bak_efi_suse_avail=`du -sk /boot/efi/efi/SuSE/initrd-${KERNELRELEASES}.img*bak /boot/efi/efi/SuSE/initramfs-${KERNELRELEASES}.img*bak /boot/efi/efi/SuSE/initrd-${KERNELRELEASES}*bak 2> /dev/null | awk '{req += $1} END {printf("%d", req/1024)}'`
	#get the available space and take care of df -k output in multiple lines
	#The 4th column is the Available space
	disk_avail=`df -k /boot | awk 'BEGIN {c=0;p=0;done=0} {if(c && !done) {n=split($0, a); if (p+n >= 4) out=a[4-p]; if (out != "") done=1; p += n  } c++;} END {printf ("%d", out/1024)}'`
	tot_avail=`expr $bak_avail + $bak_efi_rh_avail + $bak_efi_suse_avail + $disk_avail`
	tot_req=`expr $req + $efi_rh_req + $efi_suse_req`
	status=`echo $tot_avail $tot_req | awk '{if($1 < $2) print "WARNING"; else print "SUCCESS";}'`

	#echo "req: " $tot_req " bak_avail: " $bak_avail " disk_avail: "$disk_avail" tot_avail: "$tot_avail
	#echo "Status:" $status

	if [ "$status" = "WARNING" ]; then
		echo "WARNING: Not enough disk space to create backup initrd ($tot_avail MB) - required ($tot_req MB) ... exiting" | tee -a ${LOG}
		brcd_install_status
		exit ${SPACE_INSUFFICIENT}
	else
		echo "Checking available space ($tot_avail MB), required ($tot_req MB) ... good"
	fi

	CHECKED_SPACE="YES"
}

# In no-arch rpm installation cases, check if initrd is required. If so, call check_space
noarch_check_need_initrd()
{
	KERNELRELEASES=`uname -r`

	initrd_module=bfa
	if [ -f /etc/SuSE-release ]; then

		if [ -e /etc/sysconfig/kernel -a -e /boot/initrd-${KERNELRELEASES} ]; then

			source /etc/sysconfig/kernel

			for module in $INITRD_MODULES; do
				case " ${initrd_module} " in
				*" $module "*)
					check_space
					break ;;
				esac
			done
		fi
	fi

	if [ -f /etc/redhat-release ]; then
		if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
			modulefile=/etc/modprobe.d/modprobe.conf
		else
			modulefile=/etc/modprobe.conf
		fi

		if [ -f $modulefile ]; then

			scsimodules=`grep "alias[[:space:]]\+scsi_hostadapter" $modulefile | grep -v '^[        ]*#' | LC_ALL=C sort -u | awk '{ print $3 }'`

			if [ -n "$scsimodules" ]; then

				for module in $scsimodules; do
					case " ${initrd_module} " in
					*" $module "*)
						check_space
						break ;;
					esac
				done
			fi
		fi
	fi
}


# Handling RHEV: Check if this is a Redhat Enterprise Virtualization edition.
if [ -f /etc/redhat-release ]; then
	if ! [ "`cat /etc/redhat-release | grep "Red Hat Enterprise Virtualization"`" == "" ]; then
		RHEV=1
	fi
fi

# Main Code Block for Install / Uninstall.
if  [ $# -gt 0 ] ; then
	i=$#
	while [ "$i" != "0" ] ; do

                case $1 in

                        -file ) # Installation.
                                shift
                                FILENAME=$1
				if ! [ "`echo $FILENAME | grep dell`" == "" ]; then
					DELL_INSTALL=1
				fi
                                shift
                                ;;
                        -bfa )  # Installation.
                                shift
                                bfarpm=$1
                                shift
                                ;;
                        -bna )  # Installation.
                                shift
                                bnarpm=$1
                                shift
				;;
			-util ) # Util rpm.
				shift
				utilrpm=$1
				shift
				;;
			--update-initrd ) #Initrd update.
				UPDATE_INITRD=1
				check_space
				shift
				;;
			--snmp ) # Install SNMP
				SNMP=1
				shift
				;;
			--mfg ) # mfg installation.
				MFG_INSTALL=1
				shift
				;;
			-n ) # Force uninstall of previously found
			      # Brocade rpm's without user intervention.
				NO_PROMPT_INSTALL=1
				shift
				;;
			--install-to-kernel ) # Must be same as uname -r and we
					      # install only binary rpms in this case.
				shift
				INSTALL_TO_KERNEL_VER=$1
				shift
				;;
		esac
		let i--
	done
fi

# If MFG_INSTALL is set - install the Brocade mfg rpms.
if [ $MFG_INSTALL -eq 1 ]; then
	bfarpm="bfa_driver_linux_mfg-$VERSION-0.noarch.rpm"
	bnarpm="bna_driver_linux_mfg-$VERSION-0.noarch.rpm"
fi

# Initalize the kernel variant - used to
# compute the rpm check regular expression
brcd_init_kernel_variant

# Parse options other than the one's needed during actual installation.
#if  [ $# -gt 0 ]; then
# Forced uninstall hack
NO_PROMPT_INSTALL=1
FORCED_PARAMS="--force-uninstall"
if  [ 1 -gt 0 ]; then
	while [ "$FORCED_PARAMS" != "" ] ; do

		case $FORCED_PARAMS in

			--chk-sysdep)
				# Case to just check the system dependency
				# With i/p being the rpm names passed in we
				# do system dependency chk if rpm is noarch
				# else return: Only for HCM.

				if ! [ "`echo $bfarpm | grep noarch`" == "" -a "`echo $bnarpm | grep noarch`" == "" ]; then
					# Func call that checks the system dependency
					check_system_dep
				fi
				exit 0
				;;
 
			--backup-initrd)

				check_space

				# Only for HCM; Backup the current running initrd
				# If previous copy exists overwrite it, if the force
				# flag is passed in.
			
				force_backup=0	
				shift

				if [ "$1" == "--force" ]; then
					force_backup=1
				fi

				# RHEV does not have initrd.
				if ! [ "$RHEV" == "1" ]; then
					brcd_initrd_backup $force_backup
				fi
				exit 0
				;;

			--add-initrd)	
			
				check_space

				# Saving current initrd -> initrd-xxx.no_bfa and 
				# Updating the initrd with bfa added.

				# RHEV does not support initrd update.
				if ! [ "$RHEV" == "1" ]; then
					# Calling func that does the initrd update
					brcd_add_bfa_initrd_update
				fi
				exit 0
				;;

			--chk-snmp)
				# only check for SNMP dependencies
				install_brcd_snmp "check"
				exit 0
				;;

			--snmp-only)
				# Install or updates bna-snmp rpm
				install_brcd_snmp
				exit 0
				;;

			-u )	# Uninstalling the Brocade rpm's

				if [ "$2" == "--snmp" ]; then
					brcd_snmp_uninstall
					exit 0
				fi

				check_space

				echo "Uninstalling the Brocade driver $VERSION RPM's" | tee ${LOG}
				force=0

				# Internal uninstall handling for HCM; we will do a force-uninstall
				# when we find we cannot unload the modules.
				shift
                                if [ "$1" == "hcm" ]; then

					force=1
					# Driver behavior init
        	                        brcd_install_init
		
					brcd_rpm_uninstall $force

					# Updating the initrd to remove bfa and revert back
					# changes made during prev initrd update
					# RHEV does not support initrd update.
					if ! [ "$RHEV" == "1" ]; then
						if [ "${kmod_kmp_uninstall}" == "0" ]; then
							brcd_remove_bfa_initrd_update
						else
							backup=1
							brcd_bfa_hp_initrd_update $backup
						fi
					fi

					if [ $INSTALL_MODE == 1 ]; then
						echo -en "WARNING: We strongly recommend a reboot to avoid any issues.\n" >> ${LOG}
						echo -en "WARNING: If booting over Brocade HBA, you must have a new Brocade driver installed for booting!\n" >> ${LOG}
                                	fi
				else                                      

					# Function that checks if we need to backup initrd and
					# takes a backup if during this current install/uninstall
					# we update the current running initrd.
					brcd_check_and_backup_running_initrd 1

					# Calling func that does the rpm uninstall 
					brcd_rpm_uninstall $force

					# RHEV does not support initrd update.
                                        if ! [ "$RHEV" == "1" ]; then
						rm_bfa=0
						# Updating the initrd if bfa is listed under
						# INITRD modules.
						if [ "${kmod_kmp_uninstall}" == "0" ]; then
							brcd_remove_bfa_initrd_update $rm_bfa
						else
							backup=1
							brcd_bfa_hp_initrd_update $backup
						fi
					fi
				fi

				if [ -f /etc/redhat-release ]; then
 
				# For RHEV: Removing the symbolic links created during install
				# and then do depmod.
				if [ "$RHEV" == "1" ]; then
					RHEV_PATH="/lib/modules/`uname -r`/extra" 
					if [ -h ${RHEV_PATH}/bna.ko ]; then
						rm -fr ${RHEV_PATH}/bna.ko
					fi
					if [ -h ${RHEV_PATH}/bfa.ko ]; then
						rm -fr ${RHEV_PATH}/bfa.ko	
					fi
					/sbin/depmod
				fi

				# For (> RHEL5.5): Reverting back the changes we made during the install
				# i.e. rename back to bfa.ko from bfa.k_inbox.
				if [ "$BFA_INBOX_RH" == "1" ]; then
					bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
					if [ -f $bfa_inbox_path/bfa.k_inbox ]; then
						${MV} -f $bfa_inbox_path/bfa.k_inbox $bfa_inbox_path/bfa.ko
						# RHEV does not support initrd update.
						if ! [ "$RHEV" == "1" ]; then
							brcd_add_bfa_initrd_update
						fi
					fi
				fi
				if [ "$BNA_INBOX_RH" == "1" ]; then
					if [ -f $bna_inbox_path/bna.k_inbox ]; then
						${MV} -f $bna_inbox_path/bna.k_inbox $bna_inbox_path/bna.ko
						/sbin/depmod
					fi
				fi
				fi

				if [ -f /etc/SuSE-release ]; then
				if [ "$BFA_INBOX_SL" == "1" ]; then
       					bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
       					if [ -f $bfa_inbox_path/bfa.k_inbox ] ; then
               					${MV} -f $bfa_inbox_path/bfa.k_inbox $bfa_inbox_path/bfa.ko
                                       		brcd_add_bfa_initrd_update
       					fi
				fi
				if [ "$BNA_INBOX_SL" == "1" ]; then
					if [ -f $bna_inbox_path/bna.k_inbox ]; then
						${MV} -f $bna_inbox_path/bna.k_inbox $bna_inbox_path/bna.ko
						/sbin/depmod
					fi
				fi
				fi

				if [ -f /etc/fedora-release ]; then
					bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
					if [ -f $bfa_inbox_path/bfa.k_inbox ] ; then
						${MV} -f $bfa_inbox_path/bfa.k_inbox $bfa_inbox_path/bfa.ko
						/sbin/depmod
					fi

					if [ -f $bna_inbox_path/bna.k_inbox ]; then
						${MV} -f $bna_inbox_path/bna.k_inbox $bna_inbox_path/bna.ko
						/sbin/depmod
					fi
				fi
				exit 0
				;;

			 --force-uninstall )  
				
				check_space

				echo "Uninstalling the Brocade driver $VERSION RPM's" | tee ${LOG}

				# Function that checks if we need to backup initrd and
				# takes a backup if during this current install/uninstall
				# we update the current running initrd.
				brcd_check_and_backup_running_initrd 1

				# Uninstalling the Brocade rpm's by force.
				# Uninstalls source rpm's even unload of module fails.

				force=1
                                # Calling func that does the rpm uninstall
                                brcd_rpm_uninstall $force

				if [ -f /etc/redhat-release ]; then
 
				# For RHEV: Removing the symbolic links created during install
				# and then do depmod.
				if [ "$RHEV" == "1" ]; then
					RHEV_PATH="/lib/modules/`uname -r`/extra" 
					if [ -h ${RHEV_PATH}/bna.ko ]; then
						rm -fr ${RHEV_PATH}/bna.ko
					fi
					if [ -h ${RHEV_PATH}/bfa.ko ]; then
						rm -fr ${RHEV_PATH}/bfa.ko	
					fi
					/sbin/depmod
				fi

				# For (> RHEL5.5): Reverting back the changes we made during the install
				# i.e. rename back to bfa.ko from bfa.k_inbox.
				if [ "$BFA_INBOX_RH" == "1" ]; then
					bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
					if [ -f $bfa_inbox_path/bfa.k_inbox ]; then
						${MV} -f $bfa_inbox_path/bfa.k_inbox $bfa_inbox_path/bfa.ko
						# RHEV does not support initrd update.
						if ! [ "$RHEV" == "1" ]; then
							brcd_add_bfa_initrd_update
						fi
					fi
				fi
				if [ "$BNA_INBOX_RH" == "1" ]; then
					if [ -f $bna_inbox_path/bna.k_inbox ]; then
						${MV} -f $bna_inbox_path/bna.k_inbox $bna_inbox_path/bna.ko
						/sbin/depmod
					fi
				fi
				fi

				if [ -f /etc/SuSE-release ]; then
				if [ "$BFA_INBOX_SL" == "1" ]; then
       					bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
       					if [ -f $bfa_inbox_path/bfa.k_inbox ] ; then
               					${MV} -f $bfa_inbox_path/bfa.k_inbox $bfa_inbox_path/bfa.ko
                                       		brcd_add_bfa_initrd_update
       					fi
				fi
				if [ "$BNA_INBOX_SL" == "1" ]; then
					if [ -f $bna_inbox_path/bna.k_inbox ]; then
						${MV} -f $bna_inbox_path/bna.k_inbox $bna_inbox_path/bna.ko
						/sbin/depmod
					fi
				fi
				fi

				if [ -f /etc/fedora-release ]; then
					bfa_inbox_path="/lib/modules/`uname -r`/kernel/drivers/scsi/bfa/"
					if [ -f $bfa_inbox_path/bfa.k_inbox ] ; then
						${MV} -f $bfa_inbox_path/bfa.k_inbox $bfa_inbox_path/bfa.ko
						/sbin/depmod
					fi

					if [ -f $bna_inbox_path/bna.k_inbox ]; then
						${MV} -f $bna_inbox_path/bna.k_inbox $bna_inbox_path/bna.ko
						/sbin/depmod
					fi
				fi

				echo -en "WARNING: We strongly recommend a reboot to avoid any issues.\n" | tee -a ${LOG}
				echo -en "WARNING: If booting over Brocade HBA, you must have a new Brocade driver installed for booting!\n" | tee -a ${LOG}
                                exit 0
                                ;;

			--rm-initrd )

				rm_bfa=1
				# Updating the initrd to remove bfa and revert back
				# changes made during prev initrd update
				# RHEV does not support initrd update.
				if ! [ "$RHEV" == "1" ]; then
					brcd_remove_bfa_initrd_update $rm_bfa
				fi
				exit 0
				;;

			-h | --help )	# Display Usage Information	
				usage
				exit 0
				;;

			* )	# Default case
				usage
				exit 1
		esac
	done

else
	
	#Default Case: Install all the three RPM's bfa,bna and util.

	echo "Installing the Brocade driver $VERSION RPM's" | tee ${LOG}

	# Determine if the passed in rpm's are binary or noarch
	# If either one is noarch we need to do a system dep check.

	if ! [ "`echo $bfarpm | grep noarch`" == "" -a "`echo $bnarpm | grep noarch`" == "" ]; then
		noarch_check_need_initrd

		# If --install-to-kernel is set - we install only binary rpms: if this is false we should bail out.
		if [ "$INSTALL_TO_KERNEL_VER" != "" ]; then
			echo "Error: Option --install-to-kernel can be used with only binary rpms" | tee ${LOG}
			exit ${UNSUPPORTED_OS_VERSION}
		fi

		# Func call that checks the system dependency
		# For DELL dkms install we dont check for dependency.
		if ! [ "$DELL_INSTALL" == "1" ]; then
			check_system_dep
		fi
	else
		kmod_kmp_install=1;
		check_space
	fi

	# Func call that checks the existance of previous
	# Brocade drivers and Uninstalls / Terminates the
	# installation based on users choice.
	
	brcd_prev_rpm_check

	# Function to populate the system info before
	# installing the driver rpm; makes sure we have
	# system info before mkinitrd from driver rpm.

	populate_system_info

	# Install the bna driver RPM
	# Func call to install the bna driver

	# Remove any previous install failure log before fresh install.
	if [ -f /var/log/brocade/mk.log ]; then
		rm -f /var/log/brocade/mk.log
	fi

	if [ $BNA_NOARCH_SUPPORTED == 1 ] ; then
		# if tomcard card is present then install bna
		if [ ${FOUND_CNA_CARD} == "TRUE" ]; then
			install_brcd_bna
			[ $SNMP -eq 1 ] && install_brcd_snmp
		fi
	fi

	# Install the bfa driver RPM
	# Func call to install the bfa driver

	install_brcd_bfa

	# If INSTALL_TO_KERNEL_VER is set - override KERNELRELEASES.
	if ! [ "$INSTALL_TO_KERNEL_VER" == "" ]; then
		KERNELRELEASES=$INSTALL_TO_KERNEL_VER
	fi

	# In case of kmod/kmp installation, the initrd has been updated under /boot
	if [ "${kmod_kmp_install}" == "1" ]; then
		if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
			if [ -f /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img ]; then
				cp -f /boot/initramfs-${KERNELRELEASES}.img /boot/efi/efi/redhat/initramfs-${KERNELRELEASES}.img > /dev/null 2>&1
			fi
		else
			if [ -f /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img ]; then
				cp -f /boot/initrd-${KERNELRELEASES}.img /boot/efi/efi/redhat/initrd-${KERNELRELEASES}.img > /dev/null 2>&1
			fi
		fi
	fi

	# Installing the util driver RPM.
	# Func call to install the util driver rpm
	
	install_brcd_util

	# If the INSTALL MODE is not default we try to load the
	# modules bfa and bna.
	# If INSTALL_TO_KERNEL_VER is set ignore module load.
	if [ $INSTALL_MODE == 0 -a "$INSTALL_TO_KERNEL_VER" == "" ]; then

		if [ $BNA_NOARCH_SUPPORTED == 1 ] ; then
			# if tomcard card is present then install bna
			if [ ${FOUND_CNA_CARD} == "TRUE" ]; then

				# In cases where the Network Manager is turned on bna will always be reloaded;
				# Removing the module after placing the new bna.ko and loading this new one will
				# make sure we dont have the firmware mismatch.
				if ! [ "`lsmod |grep bna`" == "" ]; then
					rmmod bna > /dev/null 2>&1
				fi

				echo -en "Loading bna driver ... " | tee -a ${LOG}
				modprobe bna
				echo -en "done\n" | tee -a ${LOG}
			fi
		fi

		echo -en "Loading bfa driver ... " | tee -a ${LOG}
		modprobe bfa
		echo -en "done\n" | tee -a ${LOG}

		# Apply the persistent configuration for brocade drivers.
		if ! [ $ARCH == "ppc64" ]; then
		${BFA_KIT}/bfa_cfg.sh
		fi
	fi

	# Cases where we need to update the initrd:
	# 1. For kmod/kmp/noarch rpm install (when INSTALL_MODE = 1) and:
	#	a) bfa is not listed in INITRD_MODULES under /etc/sysconfig/kernel for SuSE.
	#	b) bfa entry "alias scsi_hostadapter_XX bfa" is missing in the modprobe.conf for Redhat.
	# 2. When we have inbox BFA present.
	# 3  When UPDATE_INITRD variable is set (user invoked script with --update-initrd).
	if [ -f /etc/redhat-release ]; then
		if [ "$RH6" == "1" ] || [ "$RH7" == "1" ]; then
			modulefile=/etc/modprobe.d/modprobe.conf
		else
			modulefile=/etc/modprobe.conf
		fi
		if [ -f $modulefile ]; then
			if [ "`grep "alias[[:space:]]\+scsi_hostadapter" $modulefile | grep bfa`" == "" ]; then
				INITRD_UPDATE_NEEDED=1
			else
				INITRD_UPDATE_DONE=1
			fi
		fi
	elif [ -f /etc/SuSE-release ]; then
		if [ "`cat /etc/sysconfig/kernel| grep ^INITRD_MODULES | grep bfa`" == "" ]; then
			INITRD_UPDATE_NEEDED=1
		else
			INITRD_UPDATE_DONE=1
		fi
	fi

	# Update the HP-initrd if present.
	if [ "${kmod_kmp_install}" == "1" ] || [ $INITRD_UPDATE_DONE -eq 1 ] ; then
		backup=1
		brcd_bfa_hp_initrd_update $backup $INSTALL_TO_KERNEL_VER
	fi

	if [ $INSTALL_MODE -eq 1 ] || [ $UPDATE_INITRD -eq 1 ] || [ "$BFA_INBOX_PRESENT" == "1" ] ; then
		if [ $INITRD_UPDATE_NEEDED -eq 1 ]; then
			# Calling func that does the initrd update
			if ! [ "$RHEV" == "1" ]; then
				brcd_add_bfa_initrd_update
				INITRD_UPDATE_DONE=1
			fi
		fi
	fi

	# Writing the brocade_driver_install status to the log
	brcd_install_status

	# If the installed rpm is noarch; check the value of allow_unsupported_modules.
	# If 0 then print a warning to user saying the same that the module does not get
	# loaded automatically during reboot even if it is added to initrd.

	if ! [ "`echo $bfarpm | grep noarch`" == "" ]; then
		# For SUSE 11 and SUSE 12;
		if [ -f /etc/SuSE-release ]; then
			
			sles_release=`cat /etc/SuSE-release |grep VERSION | awk '{print$3}'`
			file="/etc/modprobe.d/unsupported-modules"
			
			if [ ${sles_release} == 11 ] || [ ${sles_release} == 12 ]; then
				unsupported_modules=`cat $file | grep -v "#" | grep 'allow_unsupported_modules [01]' | awk '{print$2}'`
                                unsupported_modules=`echo $unsupported_modules | sed 's/ //g'`
				if [ "${unsupported_modules}" == "0" ]; then
					if [ "$REBOOT_REQUIRED" == "YES" ]; then
						echo "WARNING: allow_unsupported_modules in $file is set to 0, you must change it to 1 and do mkinitrd to get the driver loaded automatically during system boot."
					else
						echo "WARNING: allow_unsupported_modules in $file is set to 0, you may need to change it to 1 to get the driver loaded automatically during system boot."
					fi
				fi
			fi
		fi
	fi

	if [ "$REBOOT_REQUIRED" == "YES" ]; then
		echo -en "WARNING: We strongly recommend a reboot to avoid any issues.\n" | tee -a ${LOG}
	fi
fi
