#!/bin/sh

# Install QLSDM library. Check /etc/hba.conf

echo "Setting up QLogic HBA API library..."
echo "Please make sure the /usr/lib/libqlsdm.so file is not in use."

APIDIR=ApiPkg

mkdir -p ${APIDIR}
cd ${APIDIR}

tar xfz ../qlapi*rel.tgz

# Make sure we install with correct permission

chmod 500 ./lib/*

# First, some clean up.
MAIN_QLLIB_COPY=./lib/libqlsdm.so
rm -f $MAIN_QLLIB_COPY
rm -f /usr/lib/libqlsdm.a

# Right now we only support/check for IA32 and IA64 bit platforms.
# Later need to add support for PPC64

if [ $HOSTTYPE == ia64 ]
then
        ln ./lib/libqlsdm-ia64.so $MAIN_QLLIB_COPY
else
	ln ./lib/libqlsdm-ia32.so $MAIN_QLLIB_COPY
fi

cp --suffix=.bak --backup=simple $MAIN_QLLIB_COPY /usr/lib

# Now check the hba.conf file

if [ -s /etc/hba.conf ]
then

    if [ -r /etc/hba.conf ] && [ -w /etc/hba.conf ]
    then
        grep -v qla2x00 /etc/hba.conf > ./tmp.conf

        if [ -s ./tmp.conf ]
        then
            rm -f /etc/hba.conf.bak
            mv /etc/hba.conf /etc/hba.conf.bak
            sed '$r ./hba.conf' ./tmp.conf > /etc/hba.conf
        fi

        rm ./tmp.conf
    else
        echo "Cannot update /etc/hba.conf. Permission denied."
        exit
    fi

else
    cp ./hba.conf /etc
fi

# Now check the library data file

if [ -s /tmp/qlsdm.dat ]
then
    if [ -r /tmp/qlsdm.dat ] && [ -w /tmp/qlsdm.dat ]
    then
	rm -f /tmp/qlsdm.dat
    else
        echo "Cannot delete /tmp/qlsdm.dat. Permission denied."
        exit
    fi
fi

echo "Done."


