#!/bin/bash
#
# Name: build_driver
# Copyright: (C)2004-2005 Hewlett-Packard Company
#
# Description: Builds the Emulex driver from source
#
# CD	03/09/04	Initial Development
# CD	09/15/04	Changes for 7.15
# CD	09/16/04	Added code to not compile lpfc
# CD	02/16/04	Save only original lpfc.o and lpfcdfc.o
#                       files.  Do not simply cycle previous one
# CD	03/15/05	Added multipulse build code
# CD	03/22/05	Set module directory to /lib/modules/.../lpfc
#                       always; execute make clean/make/make install
#                       on all driver modules; move lpfc.o in the addon
#                       directory out of the way
# CD	06/22/05	Remove multipulse build code; Add -m option to
#                       allow other kernels to be specified; suppress
#                       error messages from make
# CD	08/03/06	Move lpfcdfc.o in the addon directory to another
#			name

#
# defines
#

SRCDIR=/opt/hp/hp-lpfc
LPFCDIR=$SRCDIR/lpfc
LPFCDFCDIR=$SRCDIR/lpfc/ioctls

#
# functions
#

# prints help message

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

# This function reads in command line arguments

read_args () {

 if [ $# -gt 0 ]
 then
 	# check arguments

 	MODULEFLAG=0
 	HELPFLAG=0

 	# parse command line into arguments

 	getopt m: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:h $*`

 	while [ $1 != -- ]
 	do
 		case $1 in
			-m) MODULEFLAG=1
		    	    MODULEDIR=$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
 	fi

 	# make sure that kernel module directory exist

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

 	# set kernel version to be what MODULEDIR is

 	KERNELVERSION=$MODULEDIR
 else
 	# no command line options were given, so use uname -r to get the kernel
 	# version

 	KERNELVERSION=`uname -r`
 fi
}

# builds adapter driver from source

build_adapter_driver () {

 # build Emulex adapter driver

 if [ "$LPFCINSTALL" != "n" ]
 then
 	echo ""

 	# save old driver
 	if test ! -f $MODDIR/lpfc.o.orig && test -f $MODDIR/lpfc.o
 	then
 		echo "Saving $MODDIR/lpfc.o"
 		cp $MODDIR/lpfc.o $MODDIR/lpfc.o.orig
 	fi

 	echo "Building lpfc.o..."
 	echo ""

 	if test -d $LPFCDIR
 	then
		cd $LPFCDIR
  		make clean 2>/dev/null
  		make KERNELVERSION=$KERNELVERSION 2>/dev/null

  		if [ $? -ne 0 ]
  		then
			echo ""
			echo "Build of lpfc.o did not succeed"
	 		exit 1
 		fi
		make KERNELVERSION=$KERNELVERSION install 2>/dev/null
 	else
 		echo "Could not find $LPFCDIR"
 		exit 1
 	fi
 fi
}

# builds ioctl module from source

build_ioctl_driver () {

 # save old driver module

 if test ! -f $MODDIR/lpfcdfc.o.orig && test -f $MODDIR/lpfcdfc.o
 then
 	echo "Saving $MODDIR/lpfcdfc.o"
 	cp $MODDIR/lpfcdfc.o $MODDIR/lpfcdfc.o.orig
 fi

 echo "Building lpfcdfc.o..."
 echo ""

 if test -d $LPFCDFCDIR
 then
 	cd $LPFCDFCDIR
 	make clean 2>/dev/null
 	make KERNELVERSION=$KERNELVERSION 2>/dev/null

 	if [ $? -ne 0 ]
 	then
        	echo ""
		echo "Build of lpfcdfc.o did not succeed"
        	exit 1
 	fi
 	make KERNELVERSION=$KERNELVERSION install 2>/dev/null
 else
 	echo "Could not find $LPFCDFCDIR"
 	exit 1
 fi
}

#
# Script Main
#

# read command line arguments

read_args $*

### Initialization Code ###
# set correct directory for Emulex drivers

MODDIR=/lib/modules/${KERNELVERSION}/kernel/drivers/scsi/lpfc

# test to see if directory exists; create it if it does not exist

if test -d $MODDIR
then
 mkdir -p $MODDIR
fi

# set generic scsi directory

SCSIDIR=/lib/modules/${KERNELVERSION}/kernel/drivers/scsi

# remove any old lpfc* files from linux generic scsi directory

if test -f $SCSIDIR/lpfc.o
then
 mv $SCSIDIR/lpfc.o $SCSIDIR/lpfc.save
fi
if test -f $SCSIDIR/lpfcdfc.o
then
 mv $SCSIDIR/lpfcdfc.o $SCSIDIR/lpfcdfc.save
fi

# move lpfc.o in addon directory (if it exists) as this messes up
# the module dependencies

ADDONDIR=/lib/modules/${KERNELVERSION}/kernel/drivers/addon

if test -f $ADDONDIR/lpfc/lpfc.o
then
 echo "Saving $ADDONDIR/lpfc/lpfc.o"
 mv $ADDONDIR/lpfc/lpfc.o $SRCDIR/lpfc.save-${KERNELVERSION}
fi

if test -f $ADDONDIR/lpfc_703/lpfc_703.o
then
 echo "Saving $ADDONDIR/lpfc_703/lpfc_703.o"
 mv $ADDONDIR/lpfc_703/lpfc_703.o $SRCDIR/lpfc_703.save-${KERNELVERSION}
fi

if test -f $ADDONDIR/lpfc/lpfcdfc/lpfcdfc.o
then
 echo "Saving $ADDONDIR/lpfc/lpfcdfc/lpfcdfc.o"
 mv $ADDONDIR/lpfc/lpfcdfc/lpfcdfc.o $SRCDIR/lpfcdfc.save-${KERNELVERSION}
fi

# read /tmp/hp_lpfc.conf

if test -f /etc/hp_lpfc.conf
then
 LPFCINSTALL=`cat /etc/hp_lpfc.conf | grep HPELXLPFC | awk 'BEGIN {FS="="} {print $2}'`
 MPLINSTALL=`cat /etc/hp_lpfc.conf | grep HPELXMPL | awk 'BEGIN {FS="="} {print $2}'`
else
 # assume everything was installed
 LPFCINSTALL=y
 MPLINSTALL=y
fi

# make link from LPFCDIR to /etc for lpfc.conf

if test ! -h /etc/lpfc.conf
then
 echo "Making symbolic link from $LPFCDIR/lpfc.conf to /etc"
 rm -f /etc/lpfc.conf
 ln -fs $LPFCDIR/lpfc.conf /etc/lpfc.conf
fi
### End of Initialization ###

# build drivers

build_adapter_driver
echo ""
build_ioctl_driver

exit 0
