#!/bin/sh

# This script attempts to rebuild the UNH iSCSI Modules and install
# then in the kernel specific directory.

# This routine will check to see if various kernel header files are available.
# These are required to rebuild this module.
kernel_src_avail() 
{
	KERNEL_VER="`/bin/uname -r`"

	echo " "
	echo "Looking for sources to build UNH iSCSI Modules for "
	echo "        kernel version ${KERNEL_VER}."

	/bin/ls -ld /lib/modules/${KERNEL_VER}/build > /dev/null 2>&1
	if [ $? -ne 0 ] 
	then
		echo " "
		echo "ERROR: /lib/modules/${KERNEL_VER}/build does not exist"
		echo "ERROR: Could not locate kernel sources for "
		echo "       this kernel (${KERNEL_VER})."
		echo "ERROR: Cannot rebuild UNH iSCSI Modules for this kernel."
		echo "ERROR: Please load the appropriate sources to rebuild module".
		return 1
	fi

	#
	# We need to make sure that "version.h" exists.  This is a problem on SLES7
	#
	/bin/ls /lib/modules/${KERNEL_VER}/build/include/linux/version.h > /dev/null 2>&1
	if [ $? -ne 0 ] 
	then
		/bin/ls /boot/vmlinuz.version.h > /dev/null 2>&1
		if [ $? -eq 0 ] 
		then
			echo " "
			echo "Missing  /lib/modules/${KERNEL_VER}/build/include/linux/version.h"
			echo "Making a backup of /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h to"
			echo "                   /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h.ORIGINAL"
			/bin/mv /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h.ORIGINAL > /dev/null 2>&1
			echo " "
			/bin/cp /boot/vmlinuz.version.h /lib/modules/${KERNEL_VER}/build/include/linux/version.h > /dev/null 2>&1
		else
			echo " "
			echo "ERROR: /lib/modules/${KERNEL_VER}/build/include/linux/version.h does not exist"
			echo "ERROR: Could not locate kernel sources for this kernel (${KERNEL_VER})."
			echo "ERROR: Cannot rebuild UNH iSCSI Modules for this kernel."
			echo "ERROR: Please load the appropriate sources to rebuild module".
			echo " "
			return 1
		fi
	fi

	#
	# We need to make sure that "autoconf.h" exists.  This is a problem on SLES7
	#
   /bin/ls /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h > /dev/null 2>&1
	if [ $? -ne 0 ] 
	then
      	/bin/ls /boot/vmlinuz.autoconf.h > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
			/bin/cp /boot/vmlinuz.autoconf.h /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h > /dev/null 2>&1
		else
			echo " "
			echo "ERROR: /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h does not exist"
			echo "ERROR: Could not locate kernel sources for this kernel (${KERNEL_VER})."
			echo "ERROR: Cannot rebuild UNH iSCSI Modules for this kernel."
			echo "ERROR: Please load the appropriate sources to rebuild module".
			return 1
		fi
	fi

	#
	# We now need to make sure that the version.h file matches the kernel
	# we are trying to build.
	#
	/bin/fgrep ${KERNEL_VER} /lib/modules/${KERNEL_VER}/build/include/linux/version.h > /dev/null 2>&1
	if [ $? -ne 0 ] 
	then
		echo " "
		echo "WARNING: /lib/modules/${KERNEL_VER}/build/include/linux/version.h "
		echo "         does not match the version of this kernel (${KERNEL_VER})."
		echo " "

		#  We will now try to see if this is a SLES 7 system 
		#  that might have been patched.
		/bin/ls /boot/vmlinuz.version.h > /dev/null 2>&1
		if [ $? -eq 0 ] 
		then
			/bin/fgrep ${KERNEL_VER} /boot/vmlinuz.version.h > /dev/null 2>&1

			if [ $? -eq 0 ] 
			then
				echo " "
				echo "This appears to be a patched kernel and the correct files are located in the \"/boot\" directory"
				echo " "
				echo "Making a backup of /lib/modules/${KERNEL_VER}/build/include/linux/version.h to"
				echo "                   /lib/modules/${KERNEL_VER}/build/include/linux/version.h.ORIGINAL"
				mv /lib/modules/${KERNEL_VER}/build/include/linux/version.h /lib/modules/${KERNEL_VER}/build/include/linux/version.h.ORIGINAL > /dev/null 2>&1
				echo " "
				echo "Making a backup of /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h to"
				echo "                   /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h.ORIGINAL"
				/bin/mv /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h.ORIGINAL > /dev/null 2>&1
				echo " "
				echo "Copying /boot/vmlinuz.version.h to /lib/modules/${KERNEL_VER}/build/include/linux/version.h"
				/bin/cp /boot/vmlinuz.version.h /lib/modules/${KERNEL_VER}/build/include/linux/version.h > /dev/null 2>&1
				echo "Copying /boot/vmlinuz.autoconf.h to /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h"
				/bin/cp /boot/vmlinuz.autoconf.h /lib/modules/${KERNEL_VER}/build/include/linux/autoconf.h > /dev/null 2>&1
			else
				echo " "
				echo "ERROR: There does not appear to be kernel sources which match the current "
				echo "       booting Linux kernel. There must be a directory named "
				echo "       \"/lib/modules/${KERNEL_VER}\" and there must be a valid "
				echo "       directory linked to \"/lib/modules/${KERNEL_VER}/build\"."
				echo "ERROR: Cannot rebuild UNH iSCSI Modules for this kernel."
				echo "ERROR: Please load the appropriate sources to rebuild module".
				return 1
			fi
		else
			return 1
		fi	
	fi

	echo " "
	echo "Sources for Linux kernel ${KERNEL_VER} have been located."
	echo " "

	return 0
}

UNH_ISCSI_DIR=/opt/unh/iscsi
KERNEL_VER="`/bin/uname -r`"
MOD_DIR=${UNH_ISCSI_DIR}/modules/${KERNEL_VER}
SRC_DIR=${UNH_ISCSI_DIR}/src

[ -d $MOD_DIR ] && /bin/rm -rf $MOD_DIR
/bin/mkdir -p $MOD_DIR

if [ ! -d ${SRC_DIR} ] 
then
	echo "ERROR: UNH iSCSI Source directory ${SRC_DIR} missing!"
	exit 1
fi

# Rebuild the UNH iSCSI Modules
# Check for the presence of a kernel source
kernel_src_avail
[ $? -ne 0 ] && exit 1

echo "Compiling the UNH iSCSI Modules.."
cd ${SRC_DIR}
/usr/bin/make clean iscsi
if [ $? -ne 0 ] 
then
	echo "ERROR: UNH iSCSI Modules compile failed."
	exit 1
fi

echo "UNH iSCSI Modules compiled successfully."


#
# Decide which drivers to copy .
# In 2.6.0, the drivers have a suffize of .ko and 
# are located at the src/ level while the 
# 2.4.x drivers are located with the initiator or target directories.
#

if [ -f ${SRC_DIR}/unh_iscsi_initiator.ko ]
# We have 2.6.0 drivers 
then
	/bin/cp unh_iscsi_initiator.ko  unh_iscsi_target.ko unh_scsi_target.ko  ${MOD_DIR}
	if [ $? -ne 0 ]
	then
		echo "ERROR: Unable to copy UNH iSCSI Drivers to ${MOD_DIR}"
	exit 1
	fi
else

	/bin/cp initiator/unh_iscsi_initiator.o  target/unh_scsi_target.o  target/unh_iscsi_target.o ${MOD_DIR} 
	if [ $? -ne 0 ]
	then
		echo "ERROR: Unable to copy UNH iSCSI Modules to ${MOD_DIR}"
	exit 1
	fi
fi
exit 0
