#!/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

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

# 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

 # 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"
 echo "options $1 $OTHER_OPTIONS $QDEPTHSTR $PDRCSTR $LRCSTR $FAILOVERSTR" >> $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 qla2xxx_conf, qla2300 or qla6312 in kernel file

 if [ "`cat $KERNELFILE | grep qla2300`" = "" ] || [ "`cat $KERNELFILE | grep qla6312`" = "" ] || [ "`cat $KERNELFILE | grep qla2xxx_conf`" = "" ]
 then
 	# 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'`

	# insert entry for qla2xxx_conf

	if [ "`cat $KERNELFILE | grep qla2xxx_conf`" = "" ]
        then
                echo "Adding entry for qla2xxx_conf to $KERNELFILE"
                MODULES="qla2xxx_conf "$MODULES
        fi

	# insert entry for qla2300

	if [ "`cat $KERNELFILE | grep qla2300`" = "" ]
	then
		echo "Adding entry for qla2300 to $KERNELFILE"
		MODULES="qla2300 "$MODULES
	fi

	# insert entry for qla6312

	if [ "`cat $KERNELFILE | grep qla6312`" = "" ]
	then
		echo "Adding entry for qla6312 to $KERNELFILE"
		MODULES="qla6312 "$MODULES
	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 "Entries for qla2xxx_conf, qla2300, qla6312 exist in $KERNELFILE already"
 fi
}

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