#!/bin/bash
#
# Name: compile_all_kernels
# Copyright: (c)2004 Hewlett-Packard
#
# Description: Looks for all kernels in /lib/modules and then
#              uses master.sh to compile the QLogic driver for
#              all valid kernels that it finds
# 
# Chad Dupuis 06/22/05 Initial Development

MODDIR=/lib/modules
SRCDIR=/usr/src
WRKDIR=/opt/hp/hp-lpfc

# get list of module directories

MODULELIST=`ls $MODDIR | grep 2\.*`
SRCLIST=`ls $SRCDIR | grep linux`

# loop through module directories

cd $WRKDIR

for i in $MODULELIST
do
 if test -d $MODDIR/$i/kernel/drivers/scsi
 then
  	echo "$i is a legitamate module directory"

	# loop through source directories, see if we have a match

	SOURCEDIR=""

	for j in $SRCLIST
	do
		# remove linux from front of source directory to reveal
		# kernel version

		KERNVER=`echo $j | awk 'BEGIN {FS="-"} {for (i=2;i<=NF;i++) {if (i<NF) printf ("%s-",$i); else printf ("%s",$i) }} {print ""}'`
		if [ "$KERNVER" != "2.4" ] && [ "$KERNVER" != "" ]
		then
			if [ "`echo $i | grep $KERNVER`" != "" ]
			then
				SOURCEDIR=$j
			fi
		fi 
	done 
	
	# call master.sh for valid kernels to compile driver

	if [ "$SOURCEDIR" != "" ]
	then
		./master.sh -m $i
		echo ""
	fi
 else
	echo ""
	echo "$i is an invalid module directory"
 	echo ""
 fi

done


