#!/bin/bash
#
# Name: copy_libs
# Copyright: (c)Copyright 2007 Hewlett-Packard Development Company, L.P. 
#
# Description: Copy libemsdm.so and libdfc.so to target system
#
# Modification History
#
# CD		03/19/07 Initial Development

#
# Defines
#

WRKDIR=/opt/hp/hp-lpfc
LIBDIR=$WRKDIR/libs
HBACONF=/etc/hba.conf
HBACONFLINE="/usr/lib/libemsdm.so"
HBACONFLINE64="/usr/lib64/libemsdm.so"
HBANYWARE=/usr/sbin/hbanyware/hbanyware
ARCH="`uname -m`"

#
# Script Main
#

cd $WRKDIR

#
# Copy libemsdm.so for the correct architecture

if [ "$ARCH" = "i386" ] || [ "$ARCH" = "i586" ] || [ "$ARCH" = "i686" ] || [ "$ARCH" = "x86_64" ]
then
	echo "Copying libemsdm.so"
	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/x86_64/libemsdm.so /usr/lib64/libemsdm.so
	fi
elif [ "$ARCH" = "ia64" ]
then
 echo "Copying libemsdm.so"
 cp --force $LIBDIR/libemsdm/ia64/libemsdm.so /usr/lib/libemsdm.so
fi

#
# Copy libdfc.so for the correct architecture

# First determine the correct distro

if test -f /etc/redhat-release
then
	DISTRO=rhel-4.0
elif test -f /etc/SuSE-release
then
	DISTRO=sles-9.0
else
	DISTRO=""
fi

# 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
 
 		if test ! -f $HBANYWARE
		then
			echo "Copying libdfc.so"
		 	cp --force $LIBDIR/libdfc/i386/${DISTRO}/libdfc.so /usr/lib/libdfc.so

			# If the architecture is x86_64, then copy the x86_64 version of libdfc.so
	
			if [ "$ARCH" = "x86_64" ]
			then
				cp --force $LIBDIR/libdfc/x86_64/${DISTRO}/libdfc.so /usr/lib64/libdfc.so
			fi
 		fi
	elif [ "$ARCH" = "ia64" ]
	then
 		# We do not want to overwrite HBAnyware's libdfc.so

 		if test ! -f $HBANYWARE
 		then
			echo "Copying libdfc.so"	
			cp --force $LIBDIR/libdfc/ia64/${DISTRO}/libdfc.so /usr/lib/libdfc.so
		 fi
	else
 		echo "This RPM does not contain a libemsdm.so for this architecture"
	fi
fi

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

if test -f $HBANYWARE && [ "`cat $HBACONF | grep "$HBACONFLINE"`" != "" ]
then
 # We 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
elif [ "`cat $HBACONF | grep "$HBACONFLINE"`" = "" ] && test ! -f $HBANYWARE 
then
 # We want to add the line to HBACONF

 echo "Adding line for lpfc driver to $HBACONF"
 echo "lpfc	$HBACONFLINE" >> $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
	if test -f $HBANYWARE && [ "`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
	elif [ "`cat $HBACONF | grep "$HBACONFLINE64"`" = "" ] && test ! -f $HBANYWARE 
	then
		# We want to add the line to HBACONF

		echo "Adding line for lpfc driver to $HBACONF"
		echo "lpfc	$HBACONFLINE64" >> $HBACONF
	fi
fi
