#!/bin/bash
#
# Name: copy_libs
# Copyright: (c)Copyright 2007-2009 Hewlett-Packard Development Company, L.P. 
#
# Description: Copy libemsdm.so and libdfc.so to target system
#
# Modification History
#
# CD	03/19/07 Initial Development
# CD	05/23/07 Changed distro directories to rhel-5.0 and
#                sles10-sp1
# CD	09/27/07 Add variable for libdfc name
# CD	12/10/07 Changes for hp-fc-enablement
# CD	02/25/08 If HPFCHBAKITDEBUG is "y" then turn debug on
# CD	03/04/08 Added logging
# CD	09/18/08 Check current driver version from modinfo to determine if we
#                have a compatible library
# CD	11/25/08 Copy libemsdm after libdfc
# CD	08/05/09 Change ia64 libemsdm.so name from libemsdm64.so to libemsdm.so
# KW 	07/09/10 Added libHBAAPI.so, libemulexhbaapi.so 
#                ** NOTE ** used the "libdfc" copy code and dir's for these
# KW   	11/01/10 Added lib's for RHEL 6; used DISTRO=rhel-5.0, no ia64    
#
# SRC	09/13/12 Added Check for Emulex Management Utility OCM
# SRC 	10/31/12 Added check for presence of OOB and edited code to place libraries based on type of Driver (OOB or Inbox)
# SRC 	29/01/13 Removed all driver related checks - thisensures that libraries are placed in target only on basis of OS Distro
# Turn debug on?
# KR    06/22/13 Refreshed the libs with 6.3.19.b20


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

#
# Defines
#

WRKDIR=/opt/hp/hp-fc-enablement/emulex-libs
#WRKDIR=/opt/hp/HP-CNA-FC-Emulex-Enablement-Kit/emulex-libs
LIBDIR=$WRKDIR
HBACONF=/etc/hba.conf
HBACONFLINE="/usr/lib/libemsdm.so"
HBACONFLINE64="/usr/lib64/libemsdm.so"
HBANYWARE=/usr/sbin/hbanyware/hbanyware
OCMANAGER_CLI=/usr/sbin/ocmanager/hbacmd
OCMANAGER_GUI=/usr/sbin/ocmanager/ocmanager
ARCH="`uname -m`"
LIBDFCNAME="libdfc.so"
DOLOG=0
LOGGER="/opt/hp/hp-fc-enablement/logger.pl"
#LOGGER="/opt/hp/HP-CNA-FC-Emulex-Enablement-Kit/logger.pl"
INBOX=1
#DRIVER_PATH=/kernel/drivers/scsi/lpfc

# 
# 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 Emulex HBAAPI libraries to target server"
	echo ""
	echo "OPTIONS"
	echo ""
	echo "-h, --help - Prints help message"
	echo "-l, --log  - Log events during installation"
}

# 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
}

# Name: GetEmulexDriverVersion
# Description: Uses modinfo to get the lpfc driver version
# In: None
# Out: ELXDRVVERSION filled in
# Returns: None
GetEmulexDriverVersion () {
#	ELXDRVVERSION="`modinfo -F version lpfc | awk '{print $1}' |  sed "s/0://g"`"
#	DRIVER_PATH="`modinfo lpfc | grep filename | grep /kernel/drivers/scsi/lpfc`"
#	if [ "$DRIVER_PATH" ]
#	then 
#		INBOX=1
#	else
#		INBOX=0
#	fi
INBOX=0
}

# Name: SetDfcLibDirectory
# Description: Based on the current lpfc driver version set what directory
#              we should look in for the compatible library
# In: None
# Out: DFCDIR set to the correct value
# Returns: None
SetDfcLibDirectory () {
# Get/set what the current driver version is
#	GetEmulexDriverVersion
	
#	 Set what LIBDFC shold be
#	if [ $INBOX -eq 0 ]
#	then
#		DFCDIR=$LIBDIR/libdfc/$ELXDRVVERSION
#		
#		if test ! -d $DFCDIR
#	        then
#        	        echo "Could not find a compatible libdfc library for $ELXDRVVERSION"
#               		 exit 1
#        	fi
#
#	else
#		# Path for libraries is Driver independent if driver is inbox
		DFCDIR=$LIBDIR/libdfc/

	# Now check if it actually exists
	
#	if test ! -d $DFCDIR
#	then
#		echo "Could not find a compatible libdfc library for $ELXDRVVERSION"
#		exit 1
#
#	fi
}

# Name: CopyLibemsdm
# Description: Copies the correct version of libemsdm.so to the target server
# In: None
# Out: None
# Returns: None
CopyLibemsdm () {
	#
	# Copy libemsdm.so for the correct architecture

	if [ "$ARCH" = "i386" ] || [ "$ARCH" = "i586" ] || [ "$ARCH" = "i686" ] || [ "$ARCH" = "x86_64" ]
	then
		echo "Copying libemsdm.so"
	
		if [ "$DOLOG" = "1" ]
		then
			$LOGGER -m "copy_libs: Copying libemsdm.so"
		fi

		cp --force $LIBDIR/libemsdm/i386/libemsdm.so /usr/lib/libemsdm.so

		# If we are x86_64 we have to copy both the x86_64 version as well

		if [ "$ARCH" = "x86_64" ]
		then
			cp --force $LIBDIR/libemsdm/amd64/libemsdm.so /usr/lib64/libemsdm.so
		fi
	elif [ "$ARCH" = "ia64" ]
	then
		echo "Copying libemsdm.so"
 
 		if [ "$DOLOG" = "1" ]
 		then
 			$LOGGER -m "copy_libs: Copying libemsdm.so"
 		fi

 		cp --force $LIBDIR/libemsdm/ia64/libemsdm.so /usr/lib/libemsdm.so
	fi
}

# Name: ModifyHbaConf
# Description: Modifies /etc/hba.conf so that applications can access the
#              libemsdm library
# In: None
# Out: None
# Returns: None
ModifyHbaConf () {
	# Create /etc/hba.conf if it doesn't exist

	if test ! -f $HBACONF
	then
		touch $HBACONF
	fi

	# Add line "lpfc /usr/lib/libemsdm.so" to /etc/hba.conf if needed; If
	# /usr/sbin/hbanyware/hbanyware exists, take the line out.
	
	#Added check on OCMANAGER_CLI and GUI
	#if test -f $HBANYWARE && [ "`cat $HBACONF | grep "$HBACONFLINE"`" != "" ]
	if [ -f $HBANYWARE ] || [ -f $OCMANAGER_CLI ] || [ -f $OCMANAGER_GUI ] && [ "`cat $HBACONF | grep "$HBACONFLINE"`" != "" ]
	then
	# We don't want to remove the line from HBACONF

	#	cat $HBACONF | grep -v $HBACONFLINE  > $HBACONF.new
	#	mv -f $HBACONF $HBACONF.old
	#	mv -f $HBACONF.new $HBACONF
		chmod 644 $HBACONF
	#Added check on OCMANAGER_CLI and GUI
	#elif [ "`cat $HBACONF | grep "$HBACONFLINE"`" = "" ] && test ! -f $HBANYWARE  
	elif [ "`cat $HBACONF | grep "$HBACONFLINE"`" = "" ] && ! [ -f $HBANYWARE ] && ! [ -f $OCMANAGER_CLI ] && ! [ -f $OCMANAGER_GUI ]
	then
		# We want to add the line to HBACONF

 		echo "Adding line for libemsdm.so to $HBACONF"
 
		if [ "$DOLOG" = "1" ]
		then
			$LOGGER -m "copy_libs: Adding line to libemsdm.so to $HBACONF"
 		fi
		
		echo " Adding line for libemulexhbaapi.so to $HBACONF"
 		echo "lpfc	$HBACONFLINE" >> $HBACONF
		echo "com.emulex.emulexapilibrary  /usr/lib/libemulexhbaapi.so" >> $HBACONF	
	fi

	# Add line "lpfc /usr/lib64/libemsdm.so" to /etc/hba.conf if needed; If
	# /usr/sbin/hbanyware/hbanyware exists, take the line out.

	if [ "$ARCH" = "x86_64" ]
	then
		 #Added check on OCMANAGER_CLI and GUI
		if [ -f $HBANYWARE ] || [ -f $OCMANAGER_CLI ] || [ -f $OCMANAGER_GUI ] && [ "`cat $HBACONF | grep "$HBACONFLINE64"`" != "" ]
		then
 			# We want to remove the line from HBACONF

 		#	cat $HBACONF | grep -v $HBACONFLINE64  > $HBACONF.new
		#	mv -f $HBACONF $HBACONF.old
		#	mv -f $HBACONF.new $HBACONF
			chmod 644 $HBACONF
		 #Added check on OCMANAGER_CLI and GUI
		#elif [ "`cat $HBACONF | grep "$HBACONFLINE64"`" = "" ] && test ! -f $HBANYWARE
		elif [ "`cat $HBACONF | grep "$HBACONFLINE64"`" = "" ] && ! [ -f $HBANYWARE ] && ! [ -f $OCMANAGER_CLI ] && ! [ -f $OCMANAGER_GUI ]
		then
			# We want to add the line to HBACONF

			echo "Adding line for x86_64 libemsdm.so to $HBACONF"
		
			if [ "$DOLOG" = "1" ]
			then
				$LOGGER -m "copy_libs: Adding line for x86_64 libemsdm.so to $HBACONF"
			fi
			 echo " Adding line for x86_64 libemulexhbaapi.so to $HBACONF"
			echo "lpfc	$HBACONFLINE64" >> $HBACONF
			echo "com.emulex.emulexapilibrary  /usr/lib64/libemulexhbaapi.so" >> $HBACONF
		fi
	fi
}

# Name: SelectLibdfcDistribution
# Description: Sets what the current distribution is
# In: None
# Out: DISTRO - which distribution we are running
# Returns: None
SelectLibdfcDistribution () {
	# First determine the correct distro
	
	if test -f /etc/redhat-release
        then
                if [ `grep -c "Red Hat Enterprise Linux Server release 6" /etc/redhat-release` -gt 0 ]
		then
			DISTRO=RHEL6
		elif [ `grep -c "Red Hat Enterprise Linux Server release 5" /etc/redhat-release` -gt 0 ]
		then
			DISTRO=RHEL5
		fi

        elif test -f /etc/SuSE-release
        then
                if [ `grep -c "SUSE Linux Enterprise Server 10" /etc/SuSE-release` -gt 0 ]
                then
                        DISTRO=SLES10
                elif [ `grep -c "SUSE Linux Enterprise Server 11" /etc/SuSE-release` -gt 0 ]
                then
                        DISTRO=SLES11
                fi
        else
                DISTRO=""
        fi
	

}

# Name: CopyLibdfc
# Description: Copy libdfc.so to server
# In: None
# Out: None
# Returns: None
CopyLibdfc () {
	# Check to see if we have a compatible library
	SetDfcLibDirectory

	# Select which distribution we are using
	SelectLibdfcDistribution

	#
	# Copy libdfc.so for the correct architecture

	# Now copy libdfc if we have a version of it for this distro and HBAnyware is not installed
	
	if [ "$DISTRO" != "" ]
	then
		if [ "$ARCH" = "i386" ] || [ "$ARCH" = "i586" ] || [ "$ARCH" = "i686" ] || [ "$ARCH" = "x86_64" ]
		then
	 		# We do not want to overwrite HBAnyware's libdfc.so
	 		#Added check on OCMANAGER_CLI and GUI
			# if test ! -f $HBANYWARE
	 		if ! [ -f $HBANYWARE ] &&  ! [ -f $OCMANAGER_CLI ] &&  ! [ -f $OCMANAGER_GUI ]
			then
				echo "Copying libdfc.so"
				echo "Copying libemulexhbaapi.so"
			
				if [ "$DOLOG" = "1" ]
				then
					$LOGGER -m "copy_libs: Copying libdfc.so"
				fi
					
	#			if [ $INBOX -eq 0 ]
	#			then
	#		 	cp --force $DFCDIR/libdfc/i386/${DISTRO}/${LIBDFCNAME} /usr/lib/libdfc.so
	#		 	cp --force $DFCDIR/libdfc/i386/${DISTRO}/libHBAAPI.so /usr/lib/libHBAAPI.so
	#		 	cp --force $DFCDIR/libdfc/i386/${DISTRO}/libemulexhbaapi.so /usr/lib/libemulexhbaapi.so
	#			elif [ $INBOX -eq 1 ]
	#			then
				cp --force $DFCDIR/${DISTRO}/i386/${LIBDFCNAME} /usr/lib/libdfc.so
                                cp --force $DFCDIR/${DISTRO}/i386/libHBAAPI.so /usr/lib/libHBAAPI.so
                                cp --force $DFCDIR/${DISTRO}/i386/libemulexhbaapi.so /usr/lib/libemulexhbaapi.so
	#			fi
	#			# If the architecture is x86_64, then copy the x86_64 version of libdfc.so
	#
				if [ "$ARCH" = "x86_64" ]
				then
	#				if [ $INBOX -eq 0 ]
	#				then
	#				cp --force $DFCDIR/libdfc/x86_64/${DISTRO}/${LIBDFCNAME} /usr/lib64/libdfc.so
	#				cp --force $DFCDIR/libdfc/x86_64/${DISTRO}/libHBAAPI.so /usr/lib64/libHBAAPI.so
	#				cp --force $DFCDIR/libdfc/x86_64/${DISTRO}/libemulexhbaapi.so /usr/lib64/libemulexhbaapi.so
	#				elif [ $INBOX -eq 1 ]
	#				then
					cp --force $DFCDIR/${DISTRO}/x86_64/${LIBDFCNAME} /usr/lib64/libdfc.so
	                                cp --force $DFCDIR/${DISTRO}/x86_64/libHBAAPI.so /usr/lib64/libHBAAPI.so
        	                        cp --force $DFCDIR/${DISTRO}/x86_64/libemulexhbaapi.so /usr/lib64/libemulexhbaapi.so
	#				fi
					
				fi
			
	 		else
				echo "An Emulex Utility has been detected on the system. Not placing Emulex Libraries"
			fi
		elif [ "$ARCH" = "ia64" ]
		then
	 		# We do not want to overwrite HBAnyware's libdfc.so
			#Added check on OCMANAGER_CLI and GUI
			# if test ! -f $HBANYWARE 
	 		if ! [ -f $HBANYWARE ] && ! [ -f $OCMANAGER_CLI ] && ! [ -f $OCMANAGER_GUI ] 
	 		then
				echo "Copying libdfc.so"
				echo "Copying libemulexhbaapi.so"
			
				if [ "$DOLOG" = "1" ]
				then
					$LOGGER -m "copy_libs: Copying libdfc.so"	
				fi
		
	#			if [ $INBOX -eq 0 ]
	#			then
	#			cp --force $DFCDIR/libdfc/ia64/${DISTRO}/${LIBDFCNAME} /usr/lib/libdfc.so
	#			cp --force $DFCDIR/libdfc/ia64/${DISTRO}/libHBAAPI.so /usr/lib/libHBAAPI.so
	#			cp --force $DFCDIR/libdfc/ia64/${DISTRO}/libemulexhbaapi.so /usr/lib/libemulexhbaapi.so
	#			elif [ $INBOX -eq 1 ]
	#			then
				cp --force $DFCDIR/${DISTRO}/ia64/${LIBDFCNAME} /usr/lib/libdfc.so
                                cp --force $DFCDIR/${DISTRO}/ia64/libHBAAPI.so /usr/lib/libHBAAPI.so
                                cp --force $DFCDIR/${DISTRO}/ia64/libemulexhbaapi.so /usr/lib/libemulexhbaapi.so

	#				fi
			else
				echo "An Emulex Utility has been detected on the system. Not placing Emulex Libraries"
			 fi
		else
 			# If we do not have a libdfc for this distribution do not continue as
			# we do not want copy any other libraries or make modifications to
			# /etc/hba.conf
		
			echo "This RPM does not contain libraries  for this architecture"
			exit 1
		fi
	fi
}

#
# Script Main
#

# Read command line arguments
ReadArgs $*

# Change to directory where libraries reside
cd $WRKDIR

# Copy libdfc.so
CopyLibdfc

# Copy libemsdm.so
CopyLibemsdm

# Modify /etc/hba.conf
ModifyHbaConf

