#!/bin/bash
#
# Name: edit_conf
# Copyright: (c)2003 Hewlett-Packard
#
# 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

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

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


# 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

 # 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"
 echo "options $1 $OTHER_OPTIONS $QDEPTHSTR $PDRCSTR $LRCSTR $FAILOVERSTR $LBSTR" >> $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 | awk 'BEGIN {FS="="} {print $2}' | sed 's/\"//g'`

 # remove references to qla2xxx_conf, qla2300, qla6312, and qla2xxx so that we can
 # put them in the desired order

 for i in $MODULES
 do
	if [ "$i" != "qla2xxx_conf" ] && [ "$i" != "qla2300" ] && [ "$i" != "qla6312" ] && [ "$i" != "qla2xxx" ]
	then
		MODULES2=$MODULES2"$i "
	fi
 done

 # put qlogic modules in correct order

 MODULES=$MODULES2"qla2xxx_conf qla2xxx qla2300 qla6312"
 echo "New module order in $KERNELFILE is \"$MODULES\""

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

#
# script main
#

# create temporary modprobe* file

cat $MODCONF | grep -v "options qla2xxx" | grep -v "alias scsi_hostadapter.* qla2300" | grep -v "alias scsi_hostadapter.* qla6312" | grep -v "alias scsi_hostadapter.* qla2xxx_conf" > $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 
else
 # We are Red Hat, add entries directly to MODCONF

 add_entry qla2xxx_conf
 add_entry qla2300
 add_entry qla6312 
fi

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

add_options qla2xxx

# move new modprobe* into place

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