#!/bin/bash

# name: set_parm.sh
# copyright: (c)2003-2005 Hewlett-Packard Company
#
# description: prompts user to set driver parameters to be
#              put in /etc/hp_qla2x00.conf
#
# Chad Dupuis 01/15/03 Adding code to build a new initrd when the
#                      parameters are changed
# Chad Dupuis 03/14/04 Removed setting queue depth to 4 for
#                      failover configs
# Chad Dupuis 04/08/04 Added detection of where execution
#                      directory is
# Chad Dupuis 04/12/04 run edit_conf whenever we make a change to
#                      to the config instead of just when we create
#                      a new initrd
# Chad Dupuis 08/30/04 set port down retry count to 3 for failover
#                      configs; set default parameters for failover
# Chad Dupuis 04/21/05 add setting load balancing type and exclude model
#                      parameters; default port down retry count is 30

PARAMFILE=/etc/hp_qla2x00.conf

if [ -x /opt/hp/hp_qla2x00/set_parm ]
then
 SRCDIR=/opt/hp/hp_qla2x00
else
 SRCDIR=/opt/hp/src/hp_qla2x00src
fi

# this parameter sets whether the script asks the user if they
# want to write out the changes to the initrd

MAKE_NEW_INITRD=1

# set default parameters

if [ -a $PARAMFILE ]
then
 # read values from file

 QDEPTH=`cat $PARAMFILE | grep qdepth | awk '{print $3}'`
 PDRC=`cat $PARAMFILE | grep port_down_retry_count | awk '{print $3}'`
 LRC=`cat $PARAMFILE | grep login_retry_count | awk '{print $3}'`
 FAILOVER=`cat $PARAMFILE | grep failover | awk '{print $3}'`
 LB=`cat $PARAMFILE | grep load_balancing | awk '{print $3}'`
 EXMOD=`cat $PARAMFILE | grep excludemodel | awk '{print $3}'`

else
 # set defaults here

 QDEPTH=16
 PDRC=3
 LRC=16
 FAILOVER=1
 LB=1
 EXMOD=0x0
fi

# wait for user to press enter

pause () {
 echo ""
 echo -n "---Press ENTER to continue---"
 read garbage
}

# main menu

main_menu () {
 clear
 echo ""
 echo ""
 echo "   Driver Parameters"
 echo "-----------------------"
 echo ""
 echo "1) XP/MSA/EVA Failover"
 echo "2) Secure Path"
 echo "3) Single Path"
 echo "4) Set Paramters Manually"
 echo "5) Quit"
 echo ""
 echo -n "Choice: "
}

# prints out a help message

print_help () {
 echo "Usage: set_parm [-xmsh]"
 echo ""
 echo "-x: setup $PARAMFILE for XP/MSA/EVA failover"
 echo "-m: setup $PARAMFILE for Secure Path failover"
 echo "-s: setup $PARAMFILE for single path"
 echo "-h: prints out this help menu"
}

# writes out /etc/hp_qla2x00.conf

write_conf () {
 echo -n "Writing new $PARAMFILE..."
 echo -n "" > $PARAMFILE
 echo "qdepth = $QDEPTH" >> $PARAMFILE
 echo "port_down_retry_count = $PDRC" >> $PARAMFILE
 echo "login_retry_count = $LRC" >> $PARAMFILE
 echo "failover = $FAILOVER" >> $PARAMFILE
 echo "load_balancing = $LB" >> $PARAMFILE
 echo "excludemodel = $EXMOD" >> $PARAMFILE
 echo "done"
 
 # write config changes to /etc/modules.conf if driver 
 # RPM is installed

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
  cd $SRCDIR
  ./edit_conf
 fi
}

# creates a new initrd with the changed parameters

write_initrd () {

 echo "" 
 echo -n "Would you like to create a new initrd to reflect the changes in the parameters (Y/n)? "
 read ANS

 if [ "$ANS" = "n" ] || [ "$ANS" = "N" ]
 then
 	return
 fi

 cd $SRCDIR
 ./make_initrd

 if [ $INTERACTIVE -eq 1 ]
 then
 	pause
 fi	
}
 
# sets parameters for XP/EVA/MSA failover

xp_failover () {
 PDRC=30
 LRC=16
 FAILOVER=1
 LB=1
 
 # export MSA1000 and HSV110 devices if Secure Path
 # is installed

 if test -d /etc/CPQswsp
 then
  EXMOD=0x06
 else
  EXMOD=0x0
 fi

 write_conf

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
 	write_initrd
 fi
}

# sets parameters for Secure Path

sp_failover () {
 PDRC=1
 FAILOVER=0
 LB=0
 EXMOD=0x0

 write_conf

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
        write_initrd
 fi
} 

# set parameters for single path

single_path () {
 PDRC=64
 FAILOVER=0
 LB=0
 EXMOD=0x0

 write_conf

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
        write_initrd
 fi
}

# lets user manually set parameters

manual () {
 echo ""
 echo "Default values in []."
 echo "If you are not sure what values you should use, accept the defaults."
 echo ""
 echo -n "queue depth [$QDEPTH]: "
 read ANS

 if [ "$ANS" != "" ]
 then
 	QDEPTH=$ANS
 fi

 echo -n "port down retry count [$PDRC]: "
 read ANS

 if [ "$ANS" != "" ]
 then
 	PDRC=$ANS
 fi

 echo -n "login retry count [$LRC]: "
 read ANS

 if [ "$ANS" != "" ]
 then
 	LRC=$ANS
 fi

 echo -n "failover [$FAILOVER]: "
 read ANS
 
 if [ "$ANS" != "" ]
 then
	FAILOVER=$ANS
 fi
 
 echo -n "static load balancing [$LB]: "
 read ANS

 if [ "$ANS" != "" ]
 then
        LB=$ANS
 fi

 echo -n "exported array models [$EXMOD]: "
 read ANS

 if [ "$ANS" != "" ]
 then
        EXMOD=$ANS
 fi

 write_conf

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
        write_initrd
 fi
}

#
# script main
#

# determine if we should to to interactive mode or command
# line mode

if [ $# -eq 0 ]
then
 # interactive mode
 DONE=0
 INTERACTIVE=1

 while [ $DONE -ne 1 ]
 do
 	main_menu
 	read CHOICE

 	case $CHOICE in
 		1) xp_failover;;
 		2) sp_failover;;
 		3) single_path;;
 		4) manual;;
		5) exit 0;;
 		*) echo ""
    	   	   echo "Invalid Choice!!!"
    	   	   sleep 2;;
 	esac
 done
else
 # command line mode

 INTERACTIVE=0
 XPFLAG=0
 SPFLAG=0
 SINGLEFLAG=0
 HELPFLAG=0

 # test first argument to make sure that it has a "-" in it

 if [ `echo $1 | grep -c "-"` -eq 0 ]
 then
 	echo "Bad argument"
	exit 1
 fi

 # parse command line into arguments

 getopt xmsh $* 1>/dev/null 2>/dev/null

 # check result of parsing

 if [ $? != 0 ]
 then
  echo "Bad argument or missing argument"
  exit 1
 fi

 set -- `getopt xmsh $*`

 while [ $1 != -- ]
 do
 	case $1 in
  		-x) XPFLAG=1;;
  		-m) SPFLAG=1;;
  		-s) SINGLEFLAG=1;;
  		-h) HELPFLAG=1;;
	esac
 	shift   # next flag
 done

 shift   # skip --
 
 # execute correct function based on flag

 if [ $HELPFLAG -eq 1 ]
 then
 	print_help
 elif [ $XPFLAG -eq 1 ]
 then
 	xp_failover
 elif [ $SPFLAG -eq 1 ]
 then
	sp_failover
 elif [ $SINGLEFLAG -eq 1 ]
 then
 	single_path
 fi
fi
