#!/bin/bash
#
# Name: build_driver
# Copyright: (c)2004 Hewlett-Packard Company
#
# Description: builds QLA 8.xx drivers
#
# Chad Dupuis	05/24/04 Initial Development

# defines

DRVSRCDIR=qla2xxx-8.00.00
MODULELIST="qla2xxx.ko qla2300.ko qla6312.ko"

# function defs

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"
}

#
# Script Main
#

# 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}-obj ]
 then
	echo "/usr/src/${SOURCEDIR} and /usr/src/${SOURCEDIR}-obj do not exist"
	exit 1
 fi

 # set kernel version to be what SOURCEDIR is

 KERNELVERSION=$MODULEDIR

 # set the module and source directories to be those specified on
 # the command line

 OBJDIR=/usr/src/${SOURCEDIR}-obj
 SRCDIR=/usr/src/${SOURCEDIR} 
else
 # no command line options were given, so use uname -r to get the kernel
 # version and set the source and object module directoires 

 KERNELVERSION=`uname -r`

 # set the module and source directories to what the defaults should be
 
 SRCDIR=/usr/src/linux
 OBJDIR=/usr/src/linux-obj
fi

# set the directory to put the newly compiled modules in

MODDIR=/lib/modules/`uname -r`/kernel/drivers/scsi/qla2xxx

# print out the modules and source directories being used

echo "Kernel modules directory: $MODDIR"
echo "Kernel source directory:  $OBJDIR"
echo ""

# check to see if source directory exists

if test ! -d $DRVSRCDIR 
then
 echo "Cannot find $DRVSRCDIR"
 exit 1
fi

# check to make sure that the kernel source directory is there

if test ! -d $SRCDIR
then
 echo "Cannot find $SRCDIR"
 exit 1
fi

# check to see if precompiled object headers exits

if test ! -d $OBJDIR
then
 echo "Cannot find $OBJDIR"
 exit 1
fi

# check to see if module directory is there

if test ! -d $MODDIR
then
 echo "Cannot find $MODULEDIR"
 exit 1
fi

# get architecture

if [ "`uname -m`" = "i386" ] || [ "`uname -m`" = "i486" ] || [ "`uname -m`" = "i586" ] || [ "`uname -m`" = "i686" ]
then
 ARCH=i386
elif [ "`uname -m`" = "ia64" ]
then
 ARCH=ia64 
elif [ "`uname -m`" = "x86_64" ]
then
 ARCH=x86_64
else
 echo "Cannot determine architecture, exiting"
 exit 1
fi

# determine kernel type

KERNTYPE=`echo $KERNELVERSION | awk 'BEGIN {FS="-"} {if ($4 == "") print $3; else print $3"-"$4}'`

if [ "$KERNTYPE" = "" ]
then
 echo "Could not determine kernel subtype"
 exit 1
fi

# go to driver source code directory

cd $DRVSRCDIR

# make clean

echo "make clean"
make -C $SRCDIR M=`pwd` clean
if [ $? -ne 0 ]
then
 echo ""
 echo "Make clean failed!"
 exit 1
fi
echo ""

# compile driver

echo "make -C $SRCDIR O=${OBJDIR}/${ARCH}/${KERNTYPE} M=`pwd` modules"
make -C $SRCDIR O=${OBJDIR}/${ARCH}/${KERNTYPE} M=`pwd` modules
if [ $? -ne 0 ]
then
 echo ""
 echo "Driver build failed!"
 exit 1
fi
echo ""

# copy new modules 

for i in $MODULELIST
do
 # save old modules
 echo "Saving old $i"
 mv -f $MODDIR/$i $MODDIR/$i.old
 
 # copy new modules
 echo "Copying $i to $MODDIR"
 cp $i $MODDIR/$i
done

# run depmod

echo "Running depmod -a"
depmod -a
exit 0

