#!/bin/bash
#
# Name: edit_conf
# Copyright: (c)2003-2005 Hewlett-Packard Company
#
# Description: edits configuration files for systems with 2.6 kernels
#
# Chad Dupuis 05/26/04 Initial Development
# Chad Dupuis 06/02/04 Modify /etc/modprobe.conf.local with qla2xxx
#                      driver parameters
# Chad Dupuis 07/21/04 Added code to put entry for qla6312 in 
#                      /etc/sysconfig/kernel file
# Chad Dupuis 09/09/04 Changed OS check from using /etc/issue to 
#                      checking for /usr/src/linux/README.SUSE
# Chad Dupuis 10/07/04 Added changes for RHEL 4; Add definition for
#                      qla2xxx_conf module to configuration files
# Chad Dupuis 04/22/05 Added ql2xlbType parameter
# Chad Dupuis 05/04/05 Put qla modules at then end of the INITRD_MODULES
#                      variable in /etc/sysconfig/kernel instead of the
#                      beginning
# Chad Dupuis 07/28/05 Add a "remove qlaxxx" line to modprobe*
# Chad Dupuis 10/12/05 Add qla2400 module to system configuration files
# Chad Dupuis 11/16/05 Add the auto restore parameter (ql2xautorestore)
# Chad Dupuis 12/06/05 Make sure that qla2xxx_conf module comes before 
#		       qla2xxx module in modprobe.conf
# Chad Dupuis 06/19/06 Removed qla6312 module
# Chad Dupuis 08/01/06 Added code to modify /etc/modprobe.d/module-renames
# Chad Dupuis 09/22/06 Do not alter where QLogic modules are in boot order

# defines

if test -f /etc/modprobe.conf.local
then
 MODCONF=/etc/modprobe.conf.local
else
 MODCONF=/etc/modprobe.conf
fi

PARAMFILE=/etc/hp_qla2x00.conf
DRVPARAMS="qlport_down_retry ql2xloginretrycount ql2xfailover ql2xmaxqdepth ql2xlbType ql2xautorestore"

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

# Name: edit_modprobe
# Description: Edits /etc/modprobe.conf to add definitions for QLogic adapters
# In: None
# Out: None
# Returns: None

edit_modprobe () {
	# We want to check to see if there is already a definition for QLogic
	# modules in modprobe.conf.  If there is then insert the QLogic adapter
	# there otherwise just put them at the end of the file.

	if [ "`cat $MODCONF | grep qla`" != "" ]
	then
		# There is already an entry for QLogic modules, put them where 
		# the definition is now

		# We need to add another state variable so that we know when we've
		# already added the QLogic module definitions
		ALLREADYADDED=n

		# Initialize new modprobe.conf
		echo -n "" > $MODCONF.new
		while read line
		do
			if [ "`echo $line | grep qla`" != "" ] && [ "$ALLREADYADDED" = "n" ]
			then
				# We've found the first instance of a QLogic adapter
				# put the module definitions here
				
				add_entry qla2xxx_conf
				add_entry qla2xxx
 				add_entry qla2300
				add_entry qla2400
				ALLREADYADDED=y
			elif [ "`echo $line | grep qla`" != "" ] && [ "$ALLREADYADDED" = "y" ]
			then
				# do nothing
				echo -n ""
			else
				echo $line >> $MODCONF.new
			fi	
		done < $MODCONF
	else
		# Create temporary modprobe* file

		cat $MODCONF | grep -v "options qla2xxx" | grep -v "alias scsi_hostadapter.* qla2xxx" | grep -v "alias scsi_hostadapter.* qla2300" | grep -v "alias scsi_hostadapter.* qla2400" | grep -v "alias scsi_hostadapter.* qla6312" | grep -v "alias scsi_hostadapter.* qla2xxx_conf" > $MODCONF.new		
		
		# Add QLogic module entries

		add_entry qla2xxx_conf
 		add_entry qla2xxx
 		add_entry qla2300
		add_entry qla2400

	fi
}

# adds options for qla2xxx driver into /etc/modules.conf

add_options () {

 OTHER_OPTIONS=""

 # get driver parameters from driver parameter file

 # qdepth

 if [ "`cat $PARAMFILE | grep qdepth`" != "" ]
 then
 	QDEPTHSTR="ql2xmaxqdepth=`cat $PARAMFILE | grep qdepth | awk '{print $3}'`"
 else
 	QDEPTHSTR=""
 fi

 # port down retry count

 if [ "`cat $PARAMFILE | grep port_down_retry_count`" != "" ]
 then
 	PDRCSTR="qlport_down_retry=`cat $PARAMFILE | grep port_down_retry_count | awk '{print $3}'`"
 else
 	PDRCSTR=""
 fi

 # login retry count

 if [ "`cat $PARAMFILE | grep login_retry_count`" != "" ]
 then
	 LRCSTR="ql2xloginretrycount=`cat $PARAMFILE | grep login_retry_count | awk '{print $3}'`"
 else
 	LRCSTR=""
 fi

 # failover

 if [ "`cat $PARAMFILE | grep failover`" != "" ]
 then
 	FAILOVERSTR="ql2xfailover=`cat $PARAMFILE | grep failover | awk '{print $3}'`"
 else
 	FAILOVERSTR=""
 fi
 
 # load balancing
 
 if [ "`cat $PARAMFILE | grep load_balancing`" != "" ]
 then
        LBSTR="ql2xlbType=`cat $PARAMFILE | grep load_balancing | awk '{print $3}'`"
 else
        LBSTR=""
 fi
 
 # auto restore 

 if [ "`cat $PARAMFILE | grep auto_restore`" != "" ]
 then
        ARSTR="ql2xautorestore=`cat $PARAMFILE | grep auto_restore | awk '{print $3}'`"
 else
        ARSTR=""
 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 $QDEPTHSTR $PDRCSTR $LRCSTR $FAILOVERSTR $LBSTR $ARSTR"
 echo "options $1 $OTHER_OPTIONS $QDEPTHSTR $PDRCSTR $LRCSTR $FAILOVERSTR $LBSTR $ARSTR" >> $MODCONF.new

}


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

edit_sysconfig () {

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

 KERNELFILE="/etc/sysconfig/kernel"

 # 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'`

 # We want to put the modules in one of two ways.  If there are no definitions for QLogic modules, we want
 # to put the module definitions at the end.  If there is a definition for QLogic modules, then we want to
 # put the definition at the beginning

 if [ "`echo $MODULES | grep qla`" != "" ]
 then
	# Set up a state variable as we don't want to add the QLogic module definitions
	# a second time
	ALLREADYADDED=n
	MODULES2=""

	for i in $MODULES
	do
		if [ "`echo $i | grep qla`" != "" ] && [ "$ALLREADYADDED" = "n" ]
		then
			# Append QLogic modules
			MODULES2=$MODULES2" qla2xxx_conf qla2xxx qla2300 qla2400"
			ALLREADYADDED="y"
		elif [ "`echo $i | grep qla`" != "" ] && [ "$ALLREADYADDED" = "y" ]
		then
			# Do nothing
			echo -n "" 
		else
			MODULES2=$MODULES2" ${i}"
		fi
	done
 else
 	# Append QLogic modules

 	MODULES2=$MODULES" qla2xxx_conf qla2xxx qla2300 qla2400"
 fi

 echo "New module order in $KERNELFILE is \"$MODULES2\""

 # 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=\"$MODULES2\"" >> $KERNELFILE.new
        fi
 done < $KERNELFILE

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

# Modify the /etc/modprobe.d/modules-renames by commenting out the QLogic
# adapters so that the firmware blob modules will load correctly

modify_module_renames () {
 FILE=/etc/modprobe.d/module-renames
 
 # Check to make sure file exists

 if test -f $FILE
 then
	echo "Modifying $FILE" 
	FILENEW=$FILE.new

 	# Initialze new file
 	echo -n "" > $FILENEW

 	# Loop through file and comment out QLogic lines

 	while read line
 	do
 		if [ "`echo $line | grep qla`" != "" ] && [ "`echo $line | grep qla | grep \#`" = "" ]
 		then	
			echo "#$line" >> $FILENEW
 		else
 			echo $line >> $FILENEW
 		fi
 	done < $FILE
 
 	# Move the new file into place

 	mv -f $FILENEW $FILE
 	chmod 644 $FILE
 fi
}

#
# script main
#

# check to see if we are SUSE or Red Hat

if test -f /etc/SuSE-release 
then
 # create temporary modprobe* file
 cat $MODCONF | grep -v "options qla2xxx" | grep -v "alias scsi_hostadapter.* qla2xxx" | grep -v "alias scsi_hostadapter.* qla2300" | grep -v "alias scsi_hostadapter.* qla2400" | grep -v "alias scsi_hostadapter.* qla6312" | grep -v "alias scsi_hostadapter.* qla2xxx_conf" > $MODCONF.new

 # We are SUSE, edit /etc/sysconfig/kernel is needed
 edit_sysconfig 
elif test -f /etc/redhat-release
then
 edit_modprobe
fi

# modify modprobe* to add load time parameters for QLogic driver

add_options qla2xxx

# add "remove qla2xxx" line to modprobe*

if [ "`cat $MODCONF.new | grep "remove qla2xxx"`" = "" ]
then
 echo "Adding \"remove qla2xxx\" line to $MODCONF"
 echo "remove qla2xxx /sbin/modprobe -r --first-time --ignore-remove qla2xxx && { /sbin/modprobe -r --ignore-remove qla2xxx_conf; }" >> $MODCONF.new
fi

# move new modprobe* into place

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

# Check /etc/modprobe.d/modules-rename to see if we need to comment out
# anything
modify_module_renames
