#!/bin/bash
#
# Name: copy_libs
# Copyright: (c)Copyright 2007 Hewlett-Packard Development Company, L.P.
#
# Description: Copies libdfc.so and libemsdm.so and makes changes to /etc/hba.conf
#
# Modification History
#
# CD	03/15/07	Initial Development

#
# Defines
#

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

#
# Script Main
#

# Copy libemsdm and libdfc libraries for the correct architecture

if [ "$ARCH" = "i386" ] || [ "$ARCH" = "i586" ] || [ "$ARCH" = "i686" ] || [ "$ARCH" = "x86_64" ]
then
	echo "Copying libemsdm.so and libdfc.so"
	cp --force $LIBDIR/i386/libemsdm.so /usr/lib/libemsdm.so

	# If we are running on an x86_64 system, copy the x86_64 version of libemsdm.so as well

	if [ "$ARCH" = "x86_64" ]
	then	
		cp --force $LIBDIR/x86_64/libemsdm.so /usr/lib64/libemsdm.so
	fi
 
	# We do not want to overwrite HBAnyware's libdfc.so
 	if test ! -f $HBANYWARE
	then
		cp --force $LIBDIR/i386/libdfc.so /usr/lib/libdfc.so

		# if we are running on an x86_64 system, copy the x86_64 version of libemsdm.so

		if [ "$ARCH" = "x86_64" ]
		then
			cp --force $LIBDIR/x86_64/libdfc.so /usr/lib64/libdfc.so
		fi
 	fi
elif [ "$ARCH" = "ia64" ]
then
	echo "Copying libemsdm.so and libdfc.so"
	cp --force $LIBDIR/ia64/libemsdm.so /usr/lib/libemsdm.so

	# We do not want to overwrite HBAnyware's libdfc.so
	if test ! -f $HBANYWARE
	then
		cp --force $LIBDIR/ia64/libdfc.so /usr/lib/libdfc.so
	fi
else
	echo "This RPM does not contain a libemsdm.so for this architecture"
fi

# Create /etc/hba.conf if it does not 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
 
