#!/bin/bash
#
# Name: build_driver
# Copyright: (C)2005, 2006 Hewlett-Packard Company
#
# Description: Builds the Emulex driver from source
#
# CD	03/09/05	Initial Development
# CD    04/28/05	Read /etc/hp_lpfc.conf to tell what
#                       was installed; save original drivers
#                       as .orig and don't save previous modules
#                       after the initial save
# CD	06/20/05	Add -m command line option to specify a
#			different kernel version than the currently
#                       installed one; Suppress error output from make
#                       when building drivers
# CD	06/30/05	Fix issue of where Emulex modules are residing
# CD	05/08/06	Fixed typo make statement in regards to KERNELVERSION
#                       variable
# CD	06/13/06	Changed directory where ioctl module resides
# CD	07/07/06	Check for MultiPulse and rerun the Multipulse build.
#                       This needs to be done so that the hooks between MP and
#                       lpfc are reestablished 

#
# defines
#

SRCDIR=/opt/hp/hp-lpfc
LPFCDIR=$SRCDIR/lpfc
LPFCDFCDIR=$SRCDIR/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
}

# Name: rebuild_multipulse
# Description: Checks for MultiPulse installation and reruns the MP build script
#              if it exists
# In: None
# Out: None
# Returns: None 
rebuild_multipulse ()
{
 MPDIR=/opt/hp/hp-multipulse

 if test -f $MPDIR/build_driver
 then
	cd $MPDIR
	echo ""
	echo "Rerunning Multipulse build script..."
	echo ""
	./build_driver -m $KERNELVERSION
	echo ""
 fi
}

# builds Emulex adapter driver

build_adapter_driver () {

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

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

 	echo "Building lpfc.ko.."
 	echo ""

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

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

# builds Emulex ioctl driver

build_ioctl_driver () {
 
 # save old driver module
 if test ! -f $MODDIR/lpfcdfc.ko.orig && test -f $MODDIR/lpfcdfc.ko
 then
 	echo "Saving $MODDIR/lpfcdfc.ko"
 	cp $MODDIR/lpfcdfc.ko $MODDIR/lpfcdfc.ko.orig
 fi

 echo "Building lpfcdfc.ko..."
 echo ""

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

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

#
# Script Main
#

# get whether we are running a supported OS or not

if test -f /etc/SuSE-release
then
 OS=SLES
elif test -f /etc/redhat-release
then
 OS=RHEL
else
 echo ""
 echo "This script not supported on this distribution!"
 exit 1
fi

# see if there was any command line arguments

read_args $*

# set correct directory for Emulex drivers

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

# 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}'`
else
 # assume everything was installed
 LPFCINSTALL=y
fi

# build drivers from source

build_adapter_driver
echo ""
build_ioctl_driver

# Rerun Multipulse build script if necessary
rebuild_multipulse

exit 0
