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

# defines

MODCONF=/etc/modprobe.conf.local
PARAMFILE=/etc/hp_qla2x00.conf
DRVPARAMS="qlport_down_retry ql2xloginretrycount ql2xfailover ql2xmaxqdepth"

# functions

# 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 qla2300 or qla6312 in kernel file

 if [ "`cat $KERNELFILE | grep qla2300`" = "" ] || [ "`cat $KERNELFILE | grep qla6312`" = "" ]
 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 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 qla2300 and qla6312 exist in $KERNELFILE already"
 fi
}

#
# script main
#

# create temporary modprobe* file

cat $MODCONF | grep -v "options qla2xxx" > $MODCONF.new

# check to see if we are SUSE or Red Hat

if test -f /usr/src/linux/README.SUSE 
then
 # edit /etc/sysconfig/kernel is needed
 
 edit_sysconfig 
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
