#!/bin/bash
#
# Name: copy_libs
# Copyright: (c)Copyright 2008 Hewlett-Packard Development Company, L.P. 
#
# Description: Copy libqlsdm.so to target system
#
# Modification History
#
# CD		01/27/08 Initial Development
# CD		02/25/08 If HPFCHBAKITDEBUG is "y" then turn debug on
# CD		03/05/08 Added logging
# KW    	08/04/10 Changed /etc/hba.conf entry for x86_64 
# SRC           09/11/12 Added check for existence of Qlogic agents
# Turn debug on?
 

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

#
# Defines
# 

SRCDIR=/opt/hp/hp-fc-enablement/qlogic-libs
DOLOG=0
LOGGER="/opt/hp/hp-fc-enablement/logger.pl"
QLREMOTE_AGENT=/usr/local/bin/qlremote
NETQLREMOTE_AGENT=/usr/local/bin/netqlremote

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

# Parse command line arguments
ReadArgs $*

# copy correct QLogic library

if [ "`uname -m `" = "ia64" ]
then
 #echo "Copying libqlsdm.so"

 if [ "$DOLOG" = "1" ]
 then
	$LOGGER -m "copy_libs: Copying libqlsdm.so"
 fi

# If Qlogic agents are present on the system, they would have already installed libraries
 if [ -f  $QLREMOTE_AGENT ] || [ -f $NETQLREMOTE_AGENT ]
 then
	echo "Not placing libqlsdm.so since one or more QLogic agents have been detected on the system"
 else
	echo "Copying libqlsdm.so"	
 	cp --force ${SRCDIR}/libqlsdm-ia64.so /usr/lib/libqlsdm.so
 fi

	
elif [ "`uname -m`" = "i386" ] || [ "`uname -m`" = "i586" ] || [ "`uname -m`" = "i686" ] || [ "`uname -m`" = "x86_64" ]
then
 #echo "Copying libqlsdm.so"
 
 if [ "$DOLOG" = "1" ]
 then
	$LOGGER -m "copy_libs: Copying libqlsdm.so"
 fi

 # If Qlogic agents are present on the system, they would have already installed libraries
 if [ -f  $QLREMOTE_AGENT ] || [ -f $NETQLREMOTE_AGENT ]
 then
        echo "Not placing libqlsdm.so since one or more QLogic agents have been detected on the system"
 else
        echo "Copying libqlsdm.so"
	 cp --force ${SRCDIR}/libqlsdm-ia32.so /usr/lib/libqlsdm.so
 fi

else
 echo "This RPM does not contain an HBA library for your architecture"
fi

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

if [ "`uname -m`" = "x86_64" ]
then
	# If Qlogic agents are present on the system, they would have already installed libraries
 if [ -f  $QLREMOTE_AGENT ] || [ -f $NETQLREMOTE_AGENT ]
 then
        echo "Not placing libqlsdm.so since one or more QLogic agents have been detected on the system"
 else
        echo "Copying libqlsdm.so"

	cp --force ${SRCDIR}/libqlsdm-x86_64.so /usr/lib64/libqlsdm.so
 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 libqlsdm.so /etc/hba.conf` -eq 0 ]
 then
        echo "Adding line for libqlsdm.so to /etc/hba.conf"

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

        echo "qla2xxx   /usr/lib/libqlsdm.so" >> /etc/hba.conf
 fi

 if [ "`uname -m`" = "x86_64" ]
 then
        if [ `grep -c qla2xxx64 /etc/hba.conf` -eq 0 ]
	    then
                echo "Adding line for x86_64 libqlsdm.so 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 "qla2xxx64   /usr/lib64/libqlsdm.so" >> /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 libqlsdm.so to /etc/hba.conf"
 fi
  echo "Adding line for libqlsdm.so to /etc/hba.conf"

 echo "qla2xxx  /usr/lib/libqlsdm.so" > /etc/hba.conf

 if [ "`uname -m`" = "x86_64" ]
 then
        echo "Adding line for x86_64 libqlsdm.so 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 "qla2xxx64   /usr/lib64/libqlsdm.so" >> /etc/hba.conf
 fi

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