#!/bin/bash
#
# Name: edit_conf
# Copyright: (c)2005i, 2006 Hewlett-Packard
#
# Description: edits configuration files for systems with 2.6 kernels
#
# Chad Dupuis 03/01/05 Initial Development
# Chad Dupuis 07/16/05 Added add_options function which includes
#                      lpfc load time parameters in /etc/modprobe.conf
# Chad Dupuis 01/31/06 Put lpfc modules at the end of the INITRD variable
#                      in /etc/sysconfig/kernel
# Chad Dupuis 07/21/06 Use "lpfc " when looking for the options line in
#                      /etc/modprobe.conf so we don't overwrite the
#                      lpfcmpl line
# Chad Dupuis 07/31/06 Select only the first INITRD line from
#                      /etc/sysconfig/kernel when editing that INITRD list

#
# defines
#

MODCONF=/etc/modprobe.conf

# file that has HP specific settings
PARAMFILE=/etc/hp_lpfc.conf

#
# functions
#

# adds an entry into MODCONF 

add_entry () {

 # get the scsi_hostadapterxx number

 SCSIHOSTADAPTER=scsi_hostadapter
 DONE=0
 CNT=1

 while [ $DONE -eq 0 ]
 do
 	TEXT=`cat ${MODCONF}.new | grep ${SCSIHOSTADAPTER}${CNT}`

 	if [ "$TEXT" = "" ]
 	then
  		DONE=1
 	else
  		CNT=`expr $CNT + 1`
 	fi
 done

 echo "adding line to $MODCONF: alias ${SCSIHOSTADAPTER}${CNT} $1"
 echo "alias ${SCSIHOSTADAPTER}${CNT} $1" >> $MODCONF.new
}


# edits the /etc/sysconfig/kernel file as needed

edit_sysconfig () {

 # we are SUSE, edit /etc/sysconfig/kernel

 KERNELFILE="/etc/sysconfig/kernel"

 # check to see if we need to put lpfc 

 if [ "`cat $KERNELFILE | grep "lpfc "`" = "" ]
 then
 	# initialize new kernel file

	echo -n "" > $KERNELFILE.new
	
	# initialize MODULES variable

	MODULES=`cat $KERNELFILE | grep INITRD_MODULES | head -n 1 | awk 'BEGIN {FS="="} {print $2}' | sed 's/\"//g'`

	# loop through modules and see if lpfcdd is there; if it is, remove it

	for i in $MODULES
	do
		if [ "$i" != "lpfcdd" ]
		then
			MODULES2="$i "$MODULES2
		else
			echo "Not using lpfcdd"
		fi
	done

	MODULES=$MODULES2

	# insert entry for lpfc

	if [ "`cat $KERNELFILE | grep "lpfc "`" = "" ]
        then
                echo "Adding entry for lpfc to $KERNELFILE"
                MODULES=$MODULES" lpfc"
        fi
	
	# create new kernel file
	
        while read line
        do
        	if [ "`echo $line | grep INITRD_MODULES`" = "" ]
                then
                	printf "%s\n" "$line" >> $KERNELFILE.new
                else
                        echo "INITRD_MODULES=\"$MODULES\"" >> $KERNELFILE.new
                fi
        done < $KERNELFILE

	mv -f $KERNELFILE $KERNELFILE.old
	mv -f $KERNELFILE.new $KERNELFILE
	chmod 644 $KERNELFILE
	chown root:root $KERNELFILE
 else
	# do nothing
	echo "Entry for lpfc exists in $KERNELFILE already"
 fi
}


# adds options for lpfc driver into /etc/modprobe.conf

add_options () {

 OTHER_OPTIONS=""
 DRVPARAMS="lpfc_nodev_tmo lpfc_lun_queue_depth lpfc_discovery_threads"
 
 ### get driver parameters from driver parameter file ###

 # nodev timeout value

 if [ "`cat $PARAMFILE | grep nodev_timeout`" != "" ]
 then
        NODEVTMO="lpfc_nodev_tmo=`cat $PARAMFILE | grep nodev_timeout | awk 'BEGIN {FS="="} {print $2}'`"
 else
        NODEVTMO=""
 fi

 # queue depth

 if [ "`cat $PARAMFILE | grep qdepth`" != "" ]
 then
        QDEPTH="lpfc_lun_queue_depth=`cat $PARAMFILE | grep qdepth | awk 'BEGIN {FS="="} {print $2}'`"
 else
        QDEPTH=""
 fi

 # discovery threads

 if [ "`cat $PARAMFILE | grep discovery_threads`" != "" ]
 then
        DISCTHREADS="lpfc_discovery_threads=`cat $PARAMFILE | grep discovery_threads | awk 'BEGIN {FS="="} {print $2}'`"
 else
        DISCTHREADS=""
 fi
 
 # get current options line from MODCONF

 OPTIONS=`cat $MODCONF | grep "options $1" | sed "s/options $1//"`

 if [ "$OPTIONS" = "" ]
 then
 	OTHER_OPTIONS=""
 else
 	for i in $OPTIONS
	do
		# assume that the driver option we are looking at
		# is not one of the ones we want to set

		ISEXTRAOPTION=1

		for j in $DRVPARAMS
		do

			# if the option we are examining matches
			# one of the options that we set, then
			# do not add this to list of other options

			if [ "`echo $i | grep $j`" != "" ]
			then
				ISEXTRAOPTION=0
			fi
		done

		if [ $ISEXTRAOPTION -eq 1 ]
		then
			OTHER_OPTIONS=${OTHER_OPTIONS}" "$i
		fi
	done
 fi

 # add options line to /etc/modules.conf

 echo "adding line to $MODCONF: options $1 $OTHER_OPTIONS $NODEVTMO $QDEPTH $DISCTHREADS"
 echo "options $1 $OTHER_OPTIONS $NODEVTMO $QDEPTH $DISCTHREADS" >> $MODCONF.new
}


#
# script main
#

# create temporary modprobe* file; we do this for both Red Hat and SUSE
# since we are adding an "options lpfc" line to both

cat $MODCONF | grep -v "alias scsi_hostadapter.* lpfc" | grep -v "options lpfc " > $MODCONF.new

# check to see if we are SUSE or Red Hat

if test -f /etc/SuSE-release 
then
 # We are SUSE, edit /etc/sysconfig/kernel is needed
 edit_sysconfig 
elif test -f /etc/redhat-release
then
 # We are Red Hat, add entries directly to MODCONF
 add_entry lpfc
else
 echo ""
 echo "Distribution not supported!"
 exit 1
fi

# add driver load time parameters

add_options "lpfc "

# move new modprobe* into place

mv -f $MODCONF $MODCONF.old
mv -f $MODCONF.new $MODCONF
chmod 644 $MODCONF
chown root:root $MODCONF

