#!/bin/bash

# Name: edit_modconf.sh
# Copyright: (C) 2004 Hewlett-Packark Company
#
# Description: edits the /etc/modules.conf file to add
#              entries for the lpfcdd
#
# Modification History
#
# Chad Dupuis	03/09/04 Initial Development
# Chad Dupuis   03/15/05 Add multipulse to configuration files
# Chad Dupuis	03/22/05 Do not put lpfcmpl into config files;
#                        add options scsi_mod line to modules.conf
# Chad Dupuis	08/17/05 Add options for lpfc driver to modules.conf
# Chad Dupuis	09/14/05 Added lpfc option lpfc_inq_pqb_filter=1
# Chad Dupuis	09/16/05 Change scsi_allow_ghost_devices to 0
# Chad Dupuis	01/31/06 Put lpfc modules at the end of the INITRD
#                        line /etc/sysconfig/kernel instead of first
# Chad Dupuis	02/22/06 Fix bug where multiple instances of lpfc were
#                        getting put into the INITRD variable in
#                        /etc/sysconfig/kernel

#
# Defines
#

MODCONF="/etc/modules.conf"
SYSCONFIG="/etc/sysconfig/kernel" # initrd module list for SUSE
SCSIHOSTADAPTER="scsi_hostadapter"
PARAMFILE=/etc/hp_lpfc.conf

#
# Functions
#

# 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 lpfc modules to SYSCONFIG

edit_sysconfig () {

 # generate modules list

 MODLIST="lpfc"

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

 # remove previous definition for lpfc, lpfcdd

 MODULES2=""
 for i in $MODULES
 do
 	if [ "$i" != "lpfc" ] && [ "$i" != "lpfcdd" ]
	then
		MODULES2=${MODULES2}" "${i}
	fi
 done

 # create new module list

 MODULES=${MODULES2}" "${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
}

# This function adds option lines for the lpfc module and the scsi_mod
# module

add_options () {
 ### Add options for lpfc driver ###

 OTHER_OPTIONS=""
 DRVPARAMS="lpfc_nodev_tmo lpfc_lun_queue_depth lpfc_discovery_threads lpfc_inq_pqb_filter"
 
 ### get driver parameters from driver parameter file ###

 # nodev timeout value

 if [ "`cat $PARAMFILE | grep nodev_timeout`" != "" ]
 then
        NODEVTMO="lpfc_nodev_tmo=`cat $PARAMFILE | grep nodev_timeout | awk 'BEGIN {FS="="} {print $2}'`"
 else
        NODEVTMO=""
 fi

 # queue depth

 if [ "`cat $PARAMFILE | grep qdepth`" != "" ]
 then
        QDEPTH="lpfc_lun_queue_depth=`cat $PARAMFILE | grep qdepth | awk 'BEGIN {FS="="} {print $2}'`"
 else
        QDEPTH=""
 fi

 # discovery threads

 if [ "`cat $PARAMFILE | grep discovery_threads`" != "" ]
 then
        DISCTHREADS="lpfc_discovery_threads=`cat $PARAMFILE | grep discovery_threads | awk 'BEGIN {FS="="} {print $2}'`"
 else
        DISCTHREADS=""
 fi

 # peripheral qualifier bit filter

 if [ "`cat $PARAMFILE | grep pqb_filter`" != "" ]
 then
	PQBFILTER="lpfc_inq_pqb_filter=`cat $PARAMFILE | grep pqb_filter | awk 'BEGIN {FS="="} {print $2}'`"
 else
	PQBFILTER=""
 fi

 # get current options line from MODCONF

 OPTIONS=`cat $MODCONF | grep "options lpfc" | sed "s/options lpfc//"`

 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 lpfc $OTHER_OPTIONS $NODEVTMO $QDEPTH $DISCTHREADS $PQBFILTER"
 echo "options lpfc $OTHER_OPTIONS $NODEVTMO $QDEPTH $DISCTHREADS $PQBFILTER" >> $MODCONF.new

 ### add options for scsi_mod ###

 if [ "`cat $MODCONF | grep "options scsi_mod"`" = "" ] 
 then
 	echo "adding line to $MODCONF: options scsi_mod scsi_allow_ghost_devices=0 max_scsi_luns=256"
	echo "options scsi_mod scsi_allow_ghost_devices=0 max_scsi_luns=256" >> $MODCONF.new
 else
 	SCSIPARAMS=`cat $MODCONF.new | grep "options scsi_mod" | sed 's/options scsi_mod //g'`

 	for i in $SCSIPARAMS
 	do
        	if [ "`echo $i | grep scsi_allow_ghost_devices`" = "" ] && [ "`echo $i | grep max_scsi_luns`" = "" ]
        	then
                	SCSIPARAMS2=" "$i
        	fi
 	done

 	SCSIPARAMS2=$SCSIPARAMS2" scsi_allow_ghost_devices=0 max_scsi_luns=256"
 	echo "adding line to $MODCONF: options scsi_mod$SCSIPARAMS2"
 	echo "options scsi_mod$SCSIPARAMS2" >> $MODCONF.new
 fi 
}

#
# Script Main
#

# read /etc/hp_lpfc.conf

if test -f /etc/hp_lpfc.conf
then
 LPFCINSTALL=`cat /etc/hp_lpfc.conf | grep HPELXLPFC | awk 'BEGIN {FS="="} {print $2}'`
else
 # assume everything was installed
 LPFCINSTALL=y
fi

# create new temporary modules.conf

cat $MODCONF | grep -v "alias scsi_hostadapter.* lpfc" | grep -v "options scsi_mod" | grep -v "options lpfc" > $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 SUSE modules file
 edit_sysconfig
else
 # edit Red Hat modules file
 add_entry lpfc
fi

# add driver parameters to modules.conf

add_options

# 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

exit 0
