#!/bin/bash
#
# Name: set_parm
# Copyright: (c)Copyright 2005 Hewlett-Packard Development Company, L.P. 
#
# Description: Sets HP specific parameters for lpfc driver
#
# Modification History
#
# CD		08/17/05 Initial Development
# CD		09/14/05 Set value for pqb_filter in parameter file

#
# Defines
#

CONFFILE=/etc/hp_lpfc.conf

#
# Functions
#

# This function prints a help message

print_help () {
 echo "Usage: $0 [-q|h|i]"
 echo ""
 echo "-q: set queue depth"
 echo "-i: \"y\" to overwrite current lpfc module during installation"
 echo "    \"n\" to leave current module installed during installation"
 echo "-h: print help message"
 exit 0
}

# This function sets the default values of the parameters that
# get written out to the configuration file

set_default_values () {
 LPFCINSTALLED=y
 QDEPTH=8
 NODEVTMO=30
 DISCTHREADS=1
 PQBFILTER=1
}

# This function gets the parameters that are currently in the 
# configuration file

get_conf_values () {
 if [ "`cat $CONFFILE | grep HPELXLPFC`" != "" ]
 then
 	LPFCINSTALLED="`cat $CONFFILE | grep HPELXLPFC | awk 'BEGIN {FS="="} {print $2}'`"
 fi 
 if [ "`cat $CONFFILE | grep qdepth`" != "" ]
 then 
 	QDEPTH="`cat $CONFFILE | grep qdepth | awk 'BEGIN {FS="="} {print $2}'`"
 fi
}

# This function parses parameters passed in from the command line

read_args () {

 if [ $# -gt 0 ]
 then
        # check arguments

        MODULEFLAG=0
        HELPFLAG=0

        # parse command line into arguments

        getopt q:i:h $* 1>/dev/null 2>/dev/null

        # check result of parsing

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

        set -- `getopt q:i:h $*`

        while [ $1 != -- ]
        do
                case $1 in
                        -q) QDEPTH=$2
                            shift;;
                        -h) HELPFLAG=1;;
			-i) if [ "$2" != "y" ] && [ "$2" != "n" ]
			    then
				echo "-i flag argument needs to be \"y\" or \"n\""
				exit 1
			    fi
			    LPFCINSTALLED=$2
			    shift;;
                        *) echo "$1 is an illegal argument"
                           exit 1;;
                esac
                shift   # next flag
        done

        shift   # skip --

        if [ $HELPFLAG -eq 1 ]
        then
                print_help
                exit 0
        fi
 fi
}

# This function actually creates (or recreates) the configuration
# file

write_conf () {
 rm -f $CONFFILE
 echo "HPELXLPFC=$LPFCINSTALLED" > $CONFFILE
 echo "nodev_timeout=$NODEVTMO" >> $CONFFILE
 echo "qdepth=$QDEPTH" >> $CONFFILE
 echo "discovery_threads=$DISCTHREADS" >> $CONFFILE
 echo "pqb_filter=$PQBFILTER" >> $CONFFILE
}

#
# Script Main
#

# get default values

set_default_values

# get configuration file values

if test -f $CONFFILE
then
 get_conf_values
fi

# read command line values

read_args $*

# write configuration file

echo -n "Writing $CONFFILE..."
write_conf
echo "OK"


