#!/bin/bash
#
# Name: build_driver
# Copyright: (C)2003, 2004 Hewlett-Packard Company
#
# Description: Builds the QLogic driver from source
# 
# CD	11/12/03	Initial Development
# CD	01/29/04	Added command line parameters so the kernel
#                       module and source directory could be specified
#
# CD    02/25/04	Fixed typo in help message
# CD    04/26/04	Fixed SLES 8 SP3 kernel name parsing issue
# CD	08/19/04	Move qla2200_conf.o and qla2300_conf.o to another
#                       filename if they exist in ..addon/qla2200
# CD	12/01/04	Save original modules only once as .orig files 
#                       instead of cycling .old modules

# prints out a help messege

# prints command line help

print_help () {
 echo "Usage: $0 [-h -s source_directory -m module_directory ]"
 echo ""
 echo "-s: kernel source directory to use"
 echo "-m: kernel module directory to use"
 echo "-h: prints help"
 echo ""
 echo "example: $0 -s linux-2.4.21-9.EL -m 2.4.21-9.ELsmp"
}

# check to see if we have command line arguments

if [ $# -gt 0 ]
then
 # check arguments
 
 SOURCEFLAG=0
 MODULEFLAG=0
 HELPFLAG=0

 # parse command line into arguments

 getopt m:s:h $* 1>/dev/null 2>/dev/null

 # check result of parsing

 if [ $? != 0 ]
 then
 	echo "Bad argument or missing argument"
 	exit 1
 fi

 set -- `getopt m:s:h $*`

 while [ $1 != -- ]
 do
 	case $1 in
		-m) MODULEFLAG=1
		    MODULEDIR=$2
		    shift;;
		-s) SOURCEFLAG=1
		    SOURCEDIR=$2
		    shift;;
		-h) HELPFLAG=1;; 
		*) echo "$1 is an illegal argument"
		   exit 1;;
 	esac
 	shift   # next flag
 done

 shift   # skip -- 

 if [ $HELPFLAG -eq 1 ]
 then
 	print_help
 	exit 0
 elif [ $SOURCEFLAG -ne 1 ] || [ $MODULEFLAG -ne 1 ]
 then
 	echo "-m and -s arguments must both be used at the same time"
 	exit 1
 fi

 # make sure that kernel source and module directories exist

 if [ ! -d /lib/modules/${MODULEDIR} ]
 then
 	echo "/lib/modules/${MODULEDIR} does not exist"
 	exit 1
 fi

 if [ ! -d /usr/src/${SOURCEDIR} ] && [ ! -d /usr/src/${SOURCEDIR}-include ]
 then
	 echo "/usr/src/${SOURCEDIR} and /usr/src/${SOURCEDIR}-include do not exist"
	 exit 1
 fi

 # set kernel version to be what SOURCEDIR is

 KERNELVERSION=$MODULEDIR

 # OSVER is set to the kernel source directory we want to use

 # determine the right directory to point OSVER to

 if [ -d /usr/src/linux-2.4 ]
 then
 	# for Red Hat distributions, we simply use the kernel source
	# directory as provided from the command line

	OSVER=$SOURCEDIR
 	DISTRO="red hat"

	# determine if we are going to compile SMP flags into the driver
	# from the .config file that exists in the source directory

	if [ -a /usr/src/${OSVER}/.config ]
	then
		OUTPUT=`cat /usr/src/${OSVER}/.config | grep CONFIG_SMP=y`

		if [ "$OUTPUT" = "" ]
		then
			SMP=0
		else
			SMP=1
		fi
	else
		echo ""
		echo "Cannot determine whether or not to compile in SMP"
		echo "symbols into the driver as there in no .config file"
		echo "in the /usr/src/${OSVER} directory"
		exit 1
	fi
 elif [ -d /usr/src/linux ]
 then
 	# for SUSE distributions, we need to see if we are using an
	# old kernel or a new one

	DISTRO="suse"
 
 	if [ `echo $KERNELVERSION | cut -c 5-6` -lt 21 ]
 	then
		# if we are using a kernel that is older than
 		# 2.4.21, set OSVER to the source directory that
		# we got from the command line
 		
		OSVER=$SOURCEDIR

		# determine if we are going to compile SMP flags into the driver
		# from the .config file that exists in the source directory

		if [ -a /usr/src/${OSVER}/.config ]
		then
			OUTPUT=`cat /usr/src/${OSVER}/.config | grep CONFIG_SMP=y`

			if [ "$OUTPUT" = "" ]
			then
				SMP=0
			else
				SMP=1
			fi
		else
			echo ""
			echo "Cannot determine whether or not to compile in SMP"
			echo "symbols into the driver as there in no .config file"
			echo "in the /usr/src/${OSVER} directory"
			exit 1
		fi	
 	else
 		# if we are using a kernel that is 2.4.21 or greater,
  		# we need to point OSVER to one of the precompiled
  		# header directories
  		
		KERNTYPE=`echo $KERNELVERSION | awk 'BEGIN {FS="-"} {if ($4 == "" ) print $3; else print $3"-"$4}'`

  		# make sure that the precompiled directory we are about
  		# to use exists

  		if [ -a /usr/src/${SOURCEDIR}-include/${KERNTYPE} ]
  		then
   			OSVER="${SOURCEDIR}-include/${KERNTYPE}"
  		else
   			echo "The directory /usr/src/${SOURCEDIR}/${KERNTYPE} does not exist"
   			echo "so the compilation cannot continue"
   			exit 1
  		fi  
  	
		# determine if we are going to compile SMP flags into the driver
		# from the .config file that exists in the source directory
	
		if [ -a /usr/src/${OSVER}/.config ]
		then
			OUTPUT=`cat /usr/src/${OSVER}/.config | grep CONFIG_SMP=y`
	
			if [ "$OUTPUT" = "" ]
			then
				SMP=0
			else
				SMP=1
			fi
		else
			echo ""
			echo "Cannot determine whether or not to compile in SMP"
			echo "symbols into the driver as there in no .config file"
			echo "in the /usr/src/${OSVER} directory"
			exit 1
		fi
  	fi
 fi 
else

 # we do not have command line arguments, so we have to determine
 # the right source directory to point the driver sources to
 # using the current running kenrel

 KERNELVERSION=`uname -r`

 # check for kernel source directories 

 if [ ! -h /usr/src/linux ] && [ ! -h /usr/src/linux-2.4 ] 
 then
 	echo "Cannot find any kernel sources"
 	exit 1
 fi

 # determine the right directory to point OSVER to

 if [ -d /usr/src/linux-2.4 ]
 then
 	OSVER="linux-2.4"
 	DISTRO="red hat"
 elif [ -d /usr/src/linux ]
 then
 	DISTRO="suse"
 
 	# we are using a SuSE distribution so
	 # we need to determine the kernel version

 	if [ `uname -r | cut -c 5-6` -lt 21 ]
 	then
		# if we are using a kernel that is older than
 		# 2.4.21, set OSVER to linux
 		OSVER=linux
 	else
 		# if we are using a kernel that is 2.4.21 or greater,
  		# we need to point OSVER to one of the precompiled
  		# header directories
  		KERNTYPE=`uname -r | awk 'BEGIN {FS="-"} {if ($4 == "") print $3; else print $3"-"$4}'`

  		# make sure that the precompiled directory we are about
  		# to use exists

  		if [ -a /usr/src/linux-include/${KERNTYPE} ]
  		then
   			OSVER="linux-include/${KERNTYPE}"
		else
   			echo "The directory /usr/src/linux-include/${KERNTYPE} does not exist"
   			echo "so the compilation cannot continue"
   			exit 1
  		fi  
  
  	fi
 fi 

 # determine if the kernel we are currently running is SMP or UNI

 SMPOUTPUT=`cat /proc/version | grep SMP`

 if [ "$SMPOUTPUT" = "" ] 
 then
 	SMP=0
 else
 	SMP=1
 fi
fi

# defines

DRVSRC="/opt/hp/src/hp_qla2x00src"
SCSIDIR="/lib/modules/${KERNELVERSION}/kernel/drivers/scsi"

# determine module directory

if [ -d /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2xxx ]
then
 MODDIR="/lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2xxx"
elif [ -d /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200 ]
then
 MODDIR="/lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200"
else
 MODDIR="/lib/modules/${KERNELVERSION}/kernel/drivers/scsi"
fi

cd $DRVSRC

echo ""
echo "Compiling QLA driver..."
echo ""

echo "make clean"
make clean 1>/dev/null 2>/dev/null

# compile the driver

echo "make HSG80=n OSVER=$OSVER SMP=$SMP all"
make HSG80=n OSVER=$OSVER SMP=$SMP all 

if [ $? -ne 0 ]
then
	echo ""
	echo "The compilation did not suceed"
	exit 1
fi

echo ""

# check compilation results

ls qla2200.o 1>/dev/null 2>/dev/null

if [ $? -ne 0 ]
then
 echo "The compilation did not complete correctly!"
 exit 1
fi

ls qla2300.o 1>/dev/null 2>/dev/null

if [ $? -ne 0 ]
then
 echo "The compilation did not complete correctly!"
 exit 1
fi

# save old modules

if [ "`uname -m`" != "ia64" ] && [ "`uname -m`" != "x86_64" ]
then
 if test ! -f $MODDIR/qla2200.orig && test -f $MODDIR/qla2200.o 
 then
  cp $MODDIR/qla2200.o $MODDIR/qla2200.orig 
 fi

 if test ! -f /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200/qla2200_conf.orig && test -f /lib/modules/${KERNELVERSION}/kernel/addon/qla2200/qla2200_conf.o
 then
 	mv -f /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200/qla2200_conf.o /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200/qla2200_conf.orig
 fi
fi

if test ! -f $MODDIR/qla2300.orig && test -f $MODDIR/qla2300.o 
then
 cp $MODDIR/qla2300.o $MODDIR/qla2300.orig 
fi

if test ! -f $SCSIDIR/qla2300_conf.orig && test -f $SCSIDIR/qla2300_conf.o  
then
 cp $SCSIDIR/qla2300_conf.o $SCSIDIR/qla2300_conf.orig 
fi

if test ! -f /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200/qla2300_conf.orig && test -f /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200/qla2300_conf.o
then
 mv -f /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200/qla2300_conf.o /lib/modules/${KERNELVERSION}/kernel/drivers/addon/qla2200/qla2300_conf.orig
fi

# copy new modules

if [ "`uname -m`" != "ia64" ] && [ "`uname -m`" != "x86_64" ]
then
 echo "Copying qla2200.o to $MODDIR"
 cp qla2200.o $MODDIR
fi

echo "Copying qla2300.o to $MODDIR"
cp qla2300.o $MODDIR
echo "Copying qla2300_conf.o to $SCSIDIR"
cp qla2300_conf.o $SCSIDIR

# update QLogic driver configuration from  
# /etc/qla2300.conf to the new modules in the /lib/modules/... directory

if [ -f "/etc/qla2300.conf" ]
then
 echo "qla2x00: Updating ${SCSIDIR}/qla2300_conf.o module with /etc/qla2300.conf data..."
 
 if [ "$KERNELVERSION" = "`uname -r`" ]
 then
	# update the qla2300_conf.o module in the
	# default directory
 	${DRVSRC}/qla_opts -w qla2300_conf
 else
	# update the qla2300_conf.o module in the
	# new kernel directory
	${DRVSRC}/qla_opts -f ${SCSIDIR}/qla2300_conf.o -w qla2300_conf
 fi
fi

# run depmod command only if the kernel we are compiling for
# and the running kernel are the same

if [ "`uname -r`" = "$KERNELVERSION" ]
then
 echo "Running depmod -a"
 depmod -a
else
 # we need to manually insert and entry for
 # qla2300_conf.o into modules.dep

 if [ "`cat /lib/modules/${KERNELVERSION}/modules.dep | grep qla2300_conf.o`" = "" ]
 then
 	echo "Updating /lib/modules/${KERNELVERSION}/modules.dep"
 	cp /lib/modules/${KERNELVERSION}/modules.dep /lib/modules/${KERNELVERSION}/modules.dep.tmp
 	echo "" >> /lib/modules/${KERNELVERSION}/modules.dep.tmp
 	echo "$SCSIDIR/qla2300_conf.o:" >> /lib/modules/${KERNELVERSION}/modules.dep.tmp
 	mv -f /lib/modules/${KERNELVERSION}/modules.dep /lib/modules/${KERNELVERSION}/modules.dep.old
 	mv -f /lib/modules/${KERNELVERSION}/modules.dep.tmp /lib/modules/${KERNELVERSION}/modules.dep
 	chmod 644 /lib/modules/${KERNELVERSION}/modules.dep
 	chown root:root /lib/modules/${KERNELVERSION}/modules.dep
 fi
fi

exit 0 

