#!/bin/bash

# Name: edit_modconf.sh
# Copyright: (C) 2003-2005 Hewlett-Packard Company
#
# Description: edits the /etc/modules.conf file to add
#              entries for the qla2300
#
# Modification History
#
# Chad Dupuis	05/20/03 Initial Development
# Chad Dupuis   10/04/03 Added sg driver
# Chad Dupuis   11/14/03 Added qla2200, qla2200_conf to /etc/modules.conf
#                        Made script save current options for qla2200 and
#                        qla2300 drivers
# Chad Dupuis   01/20/04 Removed adding qla2200_conf module to /etc/modules.conf
# Chad Dupuis   03/30/04 Do not add qla2200 line in /etc/modules.conf for
#                        RHAS 2.1 32-bit
# Chad Dupuis   05/17/04 Added code to check the IMAGESIZE variable of
#                        32-bit AS 2.1 systems and increase the size
#                        if necessary
# Chad Dupuis   06/15/04 Do not add qla2200 to module list if the 
#                        architecture is x86_64
# Chad Dupuis	04/21/05 Added ql2xlbType and ql2xexcludemodel flalgs
# Chad Dupuis	08/03/05 change /etc/issue to /etc/redhat-release
 
MODCONF="/etc/modules.conf"
SYSCONFIG="/etc/sysconfig/kernel" # initrd module list for SUSE
PARAMFILE="/etc/hp_qla2x00.conf"
SCSIHOSTADAPTER="scsi_hostadapter"
DRVPARAMS="qlport_down_retry qlogin_retry_count ql2xfailover ql2xmaxqdepth ql2xlbType ql2xexcludemodel"

# function declarations

# adds an entry into MODCONF 

add_entry () {

 # get the scsi_hostadapterxx number

 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="qlogin_retry_count=`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

 # static load balancing

 if [ "`cat $PARAMFILE | grep load_balancing`" != "" ]
 then
        LBSTR="ql2xlbType=`cat $PARAMFILE | grep load_balancing | awk '{print $3}'`"
 else
        LBSTR=""
 fi

 # array model exclude

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

}

# adds qla2200, qla2300, qla2300_conf modules to SYSCONFIG

edit_sysconfig () {

 # generate modules list

 if [ "`uname -m`" = "ia64" ] || [ "`uname -m`" = "x86_64" ]
 then
 	MODLIST="qla2300_conf qla2300 sd_mod sg"
 else
  	MODLIST="qla2200 qla2300_conf qla2300 sd_mod sg"
 fi

 # create empty file

 echo -n "" > $SYSCONFIG.new

 # get list of modules currently in kernel file

 MODULES=`cat $SYSCONFIG | grep INITRD_MODULES | awk 'BEGIN {FS="="} {print $2}' | sed 's/\"//g' | sed "s/qla2300_conf //" | sed "s/qla2200 //" | sed "s/qla2300 //" | sed "s/sg //" | sed "s/sd_mod //" | sed "s/qla2200//g" | sed "s/qla2300//g" | sed "s/sg//g" | sed "s/sd_mod//g"`

 # create new module list

 MODULES=${MODULES}" "${MODLIST}
 echo "adding modules $MODLIST to $SYSCONFIG"

  # create new kernel file

 while read line
 do
 	if [ "`echo $line | grep INITRD_MODULES`" != "" ]
 	then
  		echo "INITRD_MODULES=\"${MODULES}\"" >> $SYSCONFIG.new
 	else
  		echo $line >> $SYSCONFIG.new
 	fi
 done < $SYSCONFIG

 # move new kernel file to original

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


# edits the mkinitrd file on RHAS 2.1 to increase the size of the IMAGESIZE variable if needed

edit_mkinitrd ()
{
 if [ "`uname -m`" != "ia64" ] && [ "`cat /etc/redhat-release 2>/dev/null | grep "Red Hat" | grep "2.1"`" != "" ]
 then
 	# we are AS 2.1, 32-bit so check /sbin/mkinitrd

	MKINITRD=/sbin/mkinitrd
	TMPMKINITRD=/tmp/mkinitrd
	NEWSIZE=4000

	# get original size of IMAGESIZE

	echo "Checking IMAGESIZE variable in $MKINITRD"

	CNT=0
	while read line
	do
 		# break when we hit the first IMAGESIZE reference

 		if [ "`echo $line | grep IMAGESIZE=`" != "" ]
 		then
 			# we want to get the second IMAGESIZE variable, so break on that and
			# not the first one

			if [ $CNT -eq 1 ]
			then
				break
			else
				CNT=`expr $CNT + 1`
			fi 	
 		fi
	done < $MKINITRD

	# check what the old IMAGESIZE is

	ORIGSIZE=`echo $line | awk 'BEGIN {FS="="} {print $2}'`

	if [ $ORIGSIZE -lt 4000 ]
	then
		# make change to IMAGESIZE variable

 		echo "IMAGESIZE of $ORIGSIZE is too small, will change to $NEWSIZE"
 		cat $MKINITRD | sed "s/IMAGESIZE=$ORIGSIZE/IMAGESIZE=$NEWSIZE/" > $TMPMKINITRD
 		mv -f $MKINITRD $MKINITRD.old
 		mv -f $TMPMKINITRD $MKINITRD
 		chmod 755 $MKINITRD
 		chown root:root $MKINITRD
 		echo "Change done. Old $MKINITRD saved as $MKINITRD.old" 
	else
 		# IMAGESIZE variable is big enough, make no change

 		echo "IMAGESIZE of $ORIGSIZE is greater than or equal to $NEWSIZE"
 		echo "No change needed"
	fi 
 else
 	# if we are not AS 2.1, 32-bit do nothing
	return
 fi
}


#
# Script Main
#

# create new temporary modules.conf

cat $MODCONF | grep -v "alias scsi_hostadapter.* qla2200" | grep -v "options qla2200" | grep -v "alias scsi_hostadapter.* qla2300" | grep -v "alias scsi_hostadapter.* qla2300_conf" | grep -v "options qla2300" | grep -v sg > $MODCONF.new

# test for SYSCONFIG, if it exists we are SUSE, if it does not, assume we are Red Hat

if [ -f $SYSCONFIG ]
then
 edit_sysconfig
else
 # check IMAGESIZE variables on AS 2.1 systems

 edit_mkinitrd

 # add entries for qla adapter modules

 if [ "`uname -m`" != "ia64" ] && [ "`uname -m`" != "x86_64" ] && [ "`cat /etc/redhat-release 2>/dev/null | grep "Red Hat" | grep "2.1"`" = "" ]
 then
 	add_entry qla2200
 fi

 add_entry qla2300_conf
 add_entry qla2300
 add_entry sg
fi

# add driver options for qla2200, qla2300

if [ "`uname -m`" != "ia64" ] && [ "`uname -m`" != "x86_64" ]
then
 add_options qla2200
fi
add_options qla2300

# add post-remove commands for qla2300, qla2300_conf modules

if [ `grep -c "post-remove qla2300" ${MODCONF}.new` -eq 0 ]
then
 echo "post-remove qla2300 rmmod qla2300_conf" >> ${MODCONF}.new
fi

# move new MODCONF into place

cp -f $MODCONF $MODCONF.old # save old version of modules.conf
mv -f $MODCONF.new $MODCONF
chmod 644 $MODCONF
chown root:root $MODCONF



