#!/bin/bash
#
# Name: copy_libs
# Copyright: (c)Copyright 2011 Hewlett-Packard Development Company, L.P. 
#
# Description: Copy Brocade API lib's to target system
#
# Modification History
#
# CD		07/24/08 Initial Development
# KW        	04/15/11 General Update  
# KW		01/10/12 Update to load the 3.x API libs on all O/S 
# KW		02/08/12 Update to load API lib's by driver version 
# SRC		09/12/12 Added check for presence of BCU 

# Turn debug on?

if [ "$HPFCHBAKITDEBUG" = "y" ]
then
	echo ""
	echo "$0"
	echo ""
	set -x
fi

#
# Defines
# 

SRCDIR=/opt/hp/hp-fc-enablement/brocade-libs
DOLOG=0
LOGGER="/opt/hp/hp-fc-enablement/logger.pl"
DRVVER="`modinfo -F version bfa 2>/dev/null`"
LIBNAME="bfahbaapi.so"
BCU=/usr/bin/bcu
# 
# Functions
#

# Name: PrintHelp
# Description: Prints a help message
# In: None
# Out: None
# Returns: None
PrintHelp () {
	echo "NAME"
	echo ""
	echo "copy_libs"
	echo ""
	echo "DESCRIPTION"
	echo ""
	echo "Copies Brocade HBAAPI libraries to target server"
	echo ""
	echo "OPTIONS"
	echo ""
	echo "-h, --help - Prints help message"
	echo "-l, --log  - Print actions to log file"
}

# Name: ReadArgs
# Description: Parse command line arguments
# In: Command line arguments
# Out: None
# Returns: None
ReadArgs () {
	for i in $*
	do
		if [ "$i" = "-h" ] || [ "$i" = "--help" ]
		then
			PrintHelp
			exit 0
		elif [ "$i" = "-l" ] || [ "$i" = "--log" ]
		then
			DOLOG=1
		fi
	done
}

#
# Script main
#

#
# Check that we have a library for the current driver version
#
#cd $SRCDIR
#
if [ "$DRVVER" = "" ]
then
	echo "No bfa driver on this system"
	
	if [ "$DOLOG" = "1" ]
	then
	  $LOGGER -m "copy_libs: Could not find a bfa.ko module on this system"
	fi
	
	exit 1
fi

if test ! -d $DRVVER
then
	echo "Could not find a $LIBNAME for bfa driver $DRVVER"

	if [ "$DOLOG" = "1" ]
	then
		$LOGGER -m "copy_libs: Could not find a $LIBNAME for bfa module version $DRVVER"
	fi

	exit 1
fi

#
#DRVMAJVER="`echo $DRVVER | awk 'BEGIN {FS="."} {print $1}'`"
#	# echo $DRVMAJVER
#	if [ $DRVMAJVER -ne 3 ]
#	then 
#		echo "Not running a 3.x bfa driver"
#		exit 1 
#	fi
#
# 
# Check to see if we are SLES 10 SP 4
# Removed this test Jan 2012 KW   
#if test -f /etc/SuSE-release
#then
#        SLESREV="`grep VERSION /etc/SuSE-release | awk '{print $3}'`"
#        UPDATE="`grep PATCHLEVEL /etc/SuSE-release | awk '{print $3}'`"
#
#        if [ "$SLESREV" = "10" ]
#        then
#                if [ $UPDATE -ne 4 ]
#                then
#                        #echo ""
#                        #echo "DEBUG not SLES 10 SP 4"
#                        #echo ""
#                        exit 1
#                fi
#	else exit 1
#        fi
#else exit 1
#fi
#
	# create marker to indicate libs were installed with this rpm  
	# touch /opt/hp/hp-fc-enablement/.hp-fc-enablement-marker

# Set the library directory
#LIBDIR="${SRCDIR}/${DRVVER}"
KERNELVER=`uname -r`
KERNELMAJ=`echo $KERNELVER | awk -F "." '{print $1}' | awk -F "-" '{print $1}'`
KERNELRELEASE=`echo $KERNELVER | awk -F "." '{print $3}' | awk -F "-" '{print $1}'`

if [ ${KERNELRELEASE} -lt 32 ] 
then
	LIBDIR=${SRCDIR}/${DRVVER}/libs 
else
        LIBDIR=${SRCDIR}/${DRVVER}/libs_noioctl
fi

if [ ${KERNELMAJ} -gt 2 ]
then
	LIBDIR=${SRCDIR}/${DRVVER}/libs_noioctl
fi

# Adding a check for RHEL5.9 Brocade inbox driver 3.0.23.0

if [ `grep -c -i "Red Hat Enterprise Linux Server release 5.9" /etc/issue` -ge 1 ]
then

	if [ `modinfo bfa | grep -i filename | grep -c "kernel/drivers/scsi/bfa"` -ge 1 ]
	then
		LIBDIR=${SRCDIR}/${DRVVER}/libs_noioctl
	else
		echo "This version of the enablement kit is supported ONLY for the Brocade inbox [3.0.23.0] driver for RHEL5.9 "
		echo "Not placing any brocade libraries in the system"	
		exit 1
	fi
	
fi

# create marker to indicate libs were installed with this rpm
 touch /opt/hp/hp-fc-enablement/.hp-fc-enablement-marker


## debug #
#	echo libdir: $LIBDIR
#	echo   kern: $KERNELMAJ 

# Parse command line arguments
ReadArgs $*

# Copy correct Brocade library

# ia64 not supported at this time
#if [ "`uname -m `" = "ia64" ]
#then
#	echo "Copying $LIBNAME"
#
#	if [ "$DOLOG" = "1" ]
#	then
#		$LOGGER -m "copy_libs: Copying $LIBNAME"
#	fi

#	cp --force ${LIBDIR}/ia64/$LIBNAME /usr/lib/$LIBNAME
if [ "`uname -m`" = "i386" ] || [ "`uname -m`" = "i586" ] || [ "`uname -m`" = "i686" ] || [ "`uname -m`" = "x86_64" ]
then
# echo "Copying $LIBNAME"
 
 if [ "$DOLOG" = "1" ]
 then
	$LOGGER -m "copy_libs: Copying $LIBNAME"
 fi
 # If BCU is found on the system, libraries would have been placed along with it. So don't copy libraries
 if [ -f $BCU ] 
 then
	echo "BCU has been detected on the system. Not placing libraries"
 else
	echo "Copying $LIBNAME" 
	cp --force ${LIBDIR}/bfahbaapi-i386.so /usr/lib/$LIBNAME
 fi

else
 echo "This RPM does not contain a $LIBNAME library for your architecture"
fi

# Copy x86_64 libqlsdm.so library if uname -m is "x86_64"

if [ "`uname -m`" = "x86_64" ]
then
if [ -f $BCU ]
 then
        echo "BCU has been detected on the system. Not placing libraries"
 else
        echo "Copying $LIBNAME"

	cp --force ${LIBDIR}/bfahbaapi-x86-64.so /usr/lib64/$LIBNAME
 fi
fi
 
# Modify /etc/hba.conf

if [ -f "/etc/hba.conf" ]
then
	# file exists, see if we need to make the edit

	if [ `grep -c $LIBNAME /etc/hba.conf` -eq 0 ]
	then
        	echo "Adding line for $LIBNAME to /etc/hba.conf"

		if [ "$DOLOG" = "1" ]
		then
			$LOGGER -m "copy_libs: Adding line for $LIBNAME /etc/hba.conf"
		fi

        	echo "com.brocade.bfa   /usr/lib/$LIBNAME" >> /etc/hba.conf
 	fi

	if [ "`uname -m`" = "x86_64" ]
	then
        	if [ `grep -c "/usr/lib64/bfahbaapi.so" /etc/hba.conf` -eq 0 ]
	        then
	                echo "Adding line for x86_64 $LIBNAME to /etc/hba.conf"
                
			if [ "$DOLOG" = "1" ]
			then
				$LOGGER -m "copy_libs: Adding line for x86_64 $LIBNAME to /etc/hba.conf"
			fi

			echo "com.brocade.bfa   /usr/lib64/$LIBNAME" >> /etc/hba.conf
        fi
 fi
else
	# file does not exist, create it

	echo "Modifying /etc/hba.conf"

	if [ "$DOLOG" = "1" ]
	then
		$LOGGER -m "copy_libs: Adding line for $LIBNAME to /etc/hba.conf"
	fi
	echo "Adding line for $LIBNAME to /etc/hba.conf"
	echo "com.brocade.bfa  /usr/lib/$LIBNAME" > /etc/hba.conf

	if [ "`uname -m`" = "x86_64" ]
	then
        	echo "Adding line for x86_64 $LIBNAME to /etc/hba.conf"

		if [ "$DOLOG" = "1" ]
		then
			$LOGGER -m "copy_libs: Adding line for x86_64 libqlsdm.so to /etc/hba.conf"
		fi

		echo "com.brocade.bfa   /usr/lib64/$LIBNAME" >> /etc/hba.conf
	fi

	chmod 644 /etc/hba.conf
	chown root:root /etc/hba.conf
fi
