#!/bin/sh
#ident "$script: dlmsetopt Build 5.91.0.3 07/03/13 11:30:00 $"
#-----------------------------------------------------------------------------------------------#
# PRODUCT :	HDLM Driver 05-71                                                               #
#-----------------------------------------------------------------------------------------------#
# NAME :	dlmsetopt                                                                       #
# FUNCTION : dlmsetopt script for adding option for filter driver                               #
#		Usage: dlmsetopt -r <retrycount> | -h  |  -inqt <Inquiry timeout> | -inqr <Inquiry Retry>    # 
#			-r  option to set memory allocation retry count.                        #
#			-h  option for displaying help message                                  #
#			-inqt option for setting inquiry timeout count.							#
#			-inqr option for setting inquiry retry count.							#
# INSTANCE:                                                                                     #
# ATTR:			500                                                                     #
# MEMO :	dlmsetopt execution shell script                                                #
# DATE :	2005/11/03                                                                      #
# AUTHOR :	Mohit Mewada                                                                    #
# HISTORY :                                                                                     #
#                                                                                               #
# All Rights Reserved. Copyright (C) 2005, 2007, Hitachi, Ltd.                                        #
#-----------------------------------------------------------------------------------------------#
#Method to check whether AS4 kernel is used.
check()
{
	if [ -f /etc/redhat-release ]; then
		if [ ! -z "`$CMD_CAT /etc/redhat-release | $CMD_GREP -e "$AS4_RELEASE_STRING" -e "$ES4_RELEASE_STRING"`" ]; then
		   AS4_FLAG=1
		fi
	fi
}
# Method to declare all the messages with KAPL id
message_txt()
{
	SUPPORT_JAP=$LANG
	if [ "$SUPPORT_JAP" == "ja_JP.UTF-8" -a $AS4_FLAG -eq 1 ]; then
		dlmsetoptid1="KAPL12551-E HDLMドライバオプション設定ユティリティはdlmsetoptという名称のみで実行できます。"
		dlmsetoptid2="KAPL12552-E HDLMドライバオプション設定ユティリティ（dlmsetopt）は複数同時に実行できません。"
		dlmsetoptid3="KAPL12553-W 使用例: /opt/DynamicLinkManager/bin/dlmsetopt {-r retrycount | -inqt InquiryTimeout | -inqr InquiryRetry | -h}"
		dlmsetoptid4="KAPL12554-I HDLMドライバオプション設定ユティリティを起動しました。"
		dlmsetoptid5="KAPL12555-I HDLMドライバオプション設定ユティリティが正常終了しました。"
		dlmsetoptid6="KAPL12556-I HDLMドライバオプション設定ユティリティの実行に失敗しました。"
		dlmsetoptid7="KAPL12557-I HDLMドライバオプション設定ユティリティの実行がユーザによって中断されました。"
		dlmsetoptid8="KAPL12558-I オプション設定を有効にするためシステムを再起動してください。"
		dlmsetoptid9="KAPL12559-E パラメタに不正な値が指定されました。オプション = $OPTION, パラメタ = $PARAM"
		dlmsetoptid10="KAPL12560-E オプションが指定されていません。"
		dlmsetoptid11="KAPL12561-E 不正なオプションが指定されました。オプション = $OPTION"
		dlmsetoptid12="KAPL12562-E 必要なファイルまたはディレクトリが見つかりませんでした。ファイルまたはディレクトリ名 = "
		dlmsetoptid13="KAPL12563-E ディレクトリの作成に失敗しました。ディレクトリ = "
		dlmsetoptid14="KAPL12564-E ファイルのバックアップに失敗しました。ファイル名 = "
		dlmsetoptid15="KAPL12565-E ファイルの更新ができませんでした。ファイル名 = "
		dlmsetoptid16="KAPL12566-E HDLMドライバオプション設定ユティリティの実行が前回不正に停止されました。"
		dlmsetoptid17="KAPL12567-E インストールされているHDLMが対応しているカーネルのバージョンと，現在動作しているカーネルのバージョンが一致しません。"
		dlmsetoptid18="KAPL12568-E HDLMのファイルが不正です。ファイル名 = "
	elif [ "$SUPPORT_JAP" == "ja_JP.eucjp" -a $AS4_FLAG == 0 ] || [ "$SUPPORT_JAP" == "ja_JP.eucJP" -a $AS4_FLAG == 0 ]; then 
		dlmsetoptid1="KAPL12551-E HDLMɥ饤ХץƥƥdlmsetoptȤ̾ΤΤߤǼ¹ԤǤޤ"
		dlmsetoptid2="KAPL12552-E HDLMɥ饤ХץƥƥdlmsetoptˤʣƱ˼¹ԤǤޤ"
		dlmsetoptid3="KAPL12553-W : /opt/DynamicLinkManager/bin/dlmsetopt {-r retrycount | -inqt InquiryTimeout | -inqr InquiryRetry | -h}"
		dlmsetoptid4="KAPL12554-I HDLMɥ饤Хץƥƥưޤ"
		dlmsetoptid5="KAPL12555-I HDLMɥ饤Хץƥƥｪλޤ"
		dlmsetoptid6="KAPL12556-I HDLMɥ饤Хץƥƥμ¹Ԥ˼Ԥޤ"
		dlmsetoptid7="KAPL12557-I HDLMɥ饤Хץƥƥμ¹Ԥ桼ˤäǤޤ"
		dlmsetoptid8="KAPL12558-I ץͭˤ뤿᥷ƥƵưƤ"
		dlmsetoptid9="KAPL12559-E ѥ᥿ͤꤵޤץ = $OPTION, ѥ᥿ = $PARAM"
		dlmsetoptid10="KAPL12560-E ץ󤬻ꤵƤޤ"
		dlmsetoptid11="KAPL12561-E ʥץ󤬻ꤵޤץ = $OPTION"
		dlmsetoptid12="KAPL12562-E ɬפʥեޤϥǥ쥯ȥ꤬ĤޤǤեޤϥǥ쥯ȥ̾ = "
		dlmsetoptid13="KAPL12563-E ǥ쥯ȥκ˼Ԥޤǥ쥯ȥ = "
		dlmsetoptid14="KAPL12564-E եΥХååפ˼Ԥޤե̾ = "
		dlmsetoptid15="KAPL12565-E եιǤޤǤե̾ = "
		dlmsetoptid16="KAPL12566-E HDLMɥ饤Хץƥƥμ¹Ԥߤޤ"
		dlmsetoptid17="KAPL12567-E 󥹥ȡ뤵ƤHDLMбƤ륫ͥΥСȡưƤ륫ͥΥС󤬰פޤ"
		dlmsetoptid18="KAPL12568-E HDLMΥե뤬Ǥե̾ = "
else
		dlmsetoptid1="KAPL12551-E The utility for setting HDLM driver option can be executed with the name dlmsetopt only."
		dlmsetoptid2="KAPL12552-E Multiple instances of the utility for setting HDLM driver option (dlmsetopt) cannot execute concurrently."
		dlmsetoptid3="KAPL12553-W Usage: /opt/DynamicLinkManager/bin/dlmsetopt {-r retrycount | -inqt InquiryTimeout | -inqr InquiryRetry | -h}"
		dlmsetoptid4="KAPL12554-I The utility for setting HDLM driver option has started."
		dlmsetoptid5="KAPL12555-I The utility for setting HDLM driver option completed normally."
		dlmsetoptid6="KAPL12556-I An attempt to execute the utility for setting HDLM driver option has failed."
		dlmsetoptid7="KAPL12557-I The user stopped the utility for setting HDLM driver option."
		dlmsetoptid8="KAPL12558-I Please restart the computer so that the option settings take effect."
		dlmsetoptid9="KAPL12559-E An invalid value was specified in the parameter. Option = $OPTION, Parameter = $PARAM"
		dlmsetoptid10="KAPL12560-E An option is not specified."
		dlmsetoptid11="KAPL12561-E An invalid option was specified. Option = $OPTION"
		dlmsetoptid12="KAPL12562-E A necessary file or directory was not found. File or directory name = "
		dlmsetoptid13="KAPL12563-E An attempt to create a directory has failed. Directory = "
		dlmsetoptid14="KAPL12564-E An attempt to back up a file has failed. File name = "
		dlmsetoptid15="KAPL12565-E Could not modify file. File name = "
		dlmsetoptid16="KAPL12566-E The utility for setting HDLM driver option was executed and ended abnormally last time."
		dlmsetoptid17="KAPL12567-E The version of the kernel supported by the installed HDLM does not match the currently running kernel version."
		dlmsetoptid18="KAPL12568-E The HDLM file is invalid. File name = "
	fi
}
# Method to display usage message
usage()
{
	logmsg "$dlmsetoptid3"
}
#Methid to display help message
help()
{
        $CMD_ECHO "Usage: dlmsetopt {-r retrycount | -inqt InquiryTimeout | -inqr InquiryRetry | -h}"
        $CMD_ECHO "       retrycount: -1           = Infinite"
        $CMD_ECHO "                   0            = No Retry"
        $CMD_ECHO "                   1-2147483646 = Retry Count"
        $CMD_ECHO "       InquiryTimeout: -1       = Default Timeout(30(s))"
        $CMD_ECHO "                       1-3600   = Inquiry Timeout Value"
        $CMD_ECHO "       InquiryRetry: -1         = Default Retry(1)"
        $CMD_ECHO "                     0          = No Retry"
        $CMD_ECHO "                     1-2147483646 = Inquiry Retry Count"				
}
# Method to log messages to /var/log/messages and console
logmsg()
{
	if [ "$OPT_U_FLAG" != "$TRUE" ] || [ "$HIDDEN_OPT" = "0" ]; then
		$CMD_LOGGER -t "$SYSLOG_TAG" "$*"
		$CMD_ECHO $*
	fi
}
#Method to check the file name with "_0" and if it exists then try with "_1" upto "_9"
CheckExistingFileName(){
	FILENAME="$1"
	COUNTER=0

	while [ $COUNTER -lt 10 ]
	do	
		if [ -f "$FILENAME""_""$COUNTER" ]; then
			let "COUNTER = COUNTER + 1"
		else
			TEMP_EXTN_COUNTER=$COUNTER
			return 0
		fi
	done

	return 1
}
# Method to handle various signals
signal_handler()
{
	if [ "$EDITED" = "1" ]; then
		#Set the proper security context for SE Linux
		if [ "$SE_LINUX" = "1" ] ; then
			CheckExistingFileName $CNFFILE_TMP
			ERR=$?
			if [ "$ERR" != "0" ]; then
				logmsg "$dlmsetoptid14" "$CNFFILE"
			else
				CNFFILE_TMP="$CNFFILE_TMP""_""$TEMP_EXTN_COUNTER"
				$CMD_MV $BCKFILE $CNFFILE_TMP
				$CMD_CHCON --reference=$CNFFILE $CNFFILE_TMP > /dev/null 2>&1
				$CMD_CHOWN --reference=$CNFFILE $CNFFILE_TMP
				$CMD_MV $CNFFILE_TMP $CNFFILE        	
			fi
		else
			$CMD_CHOWN --reference=$CNFFILE $BCKFILE
			$CMD_MV $BCKFILE $CNFFILE
		fi
	fi
	logmsg "$dlmsetoptid7"
	if [ "$MESSAGE_DISPLAYED" = "$TRUE" ]; then
		logmsg "$dlmsetoptid6"
	fi
	exit 1
}
# Method for clean up on exit
cleanup()
{
	# delete back-up files only when it is created by this script
	if [ "$OTHERSCRIPT" = "$FALSE" ] ; then
		$CMD_RM -f $LOCKFILE			 # remove the lock file
		if [ "$CNFFILE_BAK" != "$CNFFILE""_dlm" ]; then
			$CMD_RM -f $CNFFILE_BAK		 # remove the backup file (if present)
		fi
		if [ "$REMOVE_BACKUP" = "$TRUE" ]; then
			$CMD_RM -rf $BCKFILEDIR		 # remove backup dir created in /etc/opt/DynamicLinkManager/
		fi
	fi
}
# Method to set the retry option in modules.conf as entered by user
# $1 is Parameter string	(e.g.: allocretrycount)
# $2 is Parameter value	(e.g.: 1234)
set_option()
{
	EDITED=0
	GOT=$FALSE
	CURRLINENO=1
	TOTAL_LINES=`$CMD_CAT $CNFFILE | $CMD_WC -l`
	STR=`$CMD_ECHO "$1=$2"`
	while read line; do
		RET_VAL=`$CMD_ECHO "$line" | $CMD_GREP -E "^options[[:space:]]+sddlmfdrv[[:space:]]" | $CMD_WC -l`
		if [ $RET_VAL -eq 1 ]; then				# option line for Filter driver got
			GOT=$TRUE
			$CMD_CAT $CNFFILE | $CMD_HEAD -n $CURRLINENO > $CNFHEADFILE 
			$CMD_CAT $CNFFILE | $CMD_TAIL -n `$CMD_EXPR $TOTAL_LINES - $CURRLINENO` > $CNFTAILFILE
			$CMD_SED '/^[[:space:]]*options[[:space:]]\+sddlmfdrv[[:space:]].*/d' $CNFHEADFILE >$CNFHEADFILE.1
			ERR=$?
			if [ $ERR -ne 0 ]; then
				logmsg "$dlmsetoptid15" "$CNFFILE"
				return 1
			fi
			$CMD_MV $CNFHEADFILE.1 $CNFHEADFILE
			RET_VAL=`$CMD_ECHO "$line" | $CMD_GREP -E "[[:space:]]$1[[:space:]]*=" | $CMD_WC -l`
			if [ $RET_VAL -eq 1 ]; then			# option allocretry got, edit the option
				NEWLINE=`$CMD_ECHO "$line" | $CMD_SED 's,[[:space:]]\+'$1'[[:space:]]*[=][[:space:]]*[^ ]*, '$STR',g'`
				ERR=$?
				if [ $ERR -ne 0 ]; then
					logmsg "$dlmsetoptid15" "$CNFFILE"
					return 1
				fi
			else	#option allocretry not got, add the option at the end of the line
				NEWLINE=`$CMD_ECHO "$line $STR"`
			fi
			$CMD_ECHO $NEWLINE >> $CNFHEADFILE
			$CMD_CAT "$CNFTAILFILE" >> $CNFHEADFILE
			break
		fi
		let "CURRLINENO=$CURRLINENO+1"
	done < $CNFFILE
	EDITED=1
	if [ $GOT = $FALSE ];then			# no option for filter driver, add the new option
		$CMD_ECHO "$MODULES_STRING_COMMON $1=$2" >>$CNFFILE
	else
		$CMD_CHMOD --reference $CNFFILE $CNFHEADFILE
		#Set the proper security context for SE Linux
		if [ $SE_LINUX -eq 1 ] ; then
			CheckExistingFileName $CNFFILE_BAK
			ERR=$?
			if [ $ERR -ne 0 ]; then
				logmsg "$dlmsetoptid14" "$CNFFILE"
				return 1
			fi
			CNFFILE_BAK="$CNFFILE_BAK""_""$TEMP_EXTN_COUNTER"
			$CMD_MV $CNFHEADFILE $CNFFILE_BAK
			$CMD_CHCON --reference=$CNFFILE $CNFFILE_BAK > /dev/null 2>&1
			$CMD_CHOWN --reference=$CNFFILE $CNFFILE_BAK
			$CMD_MV $CNFFILE_BAK $CNFFILE
		else
			$CMD_CHOWN --reference $CNFFILE $CNFHEADFILE
			$CMD_MV $CNFHEADFILE $CNFFILE
		fi
	fi
	return 0
}
# Method to delete entries from conf file during uninstallation
uninstall()
{
	CheckExistingFileName $CNFFILE_BAK
	ERR=$?
	if [ $ERR -ne 0 ]; then
		return 1
	fi
	CNFFILE_BAK="$CNFFILE_BAK""_""$TEMP_EXTN_COUNTER"
	$CMD_SED '/^[[:space:]]*options[[:space:]]\+sddlmfdrv[[:space:]].*/d' $CNFFILE >$CNFFILE_BAK
	ERR=$?
	if [ $ERR -ne 0 ]; then
		return 1
	else
		$CMD_CHMOD --reference $CNFFILE $CNFFILE_BAK
		$CMD_CHOWN --reference $CNFFILE $CNFFILE_BAK
		#Set the proper security context for SE Linux
		if [ $SE_LINUX -eq 1 ] ; then
			$CMD_CHCON --reference=$CNFFILE $CNFFILE_BAK > /dev/null 2>&1
		fi
		$CMD_MV $CNFFILE_BAK $CNFFILE
	fi
	return 0
}
# Method to check conf file existance and back up creation
chkcnffile()
{
	# check whether conf file exists
	if [ ! -f $CNFFILE ]; then
		if [ "$OPTION" != "-u" ]; then
			logmsg "$dlmsetoptid12" "$CNFFILE"
		fi
		exit 1
	fi
	# backup modules.conf file
	# backup modprobe.conf file for kernel 2.6
	if [ -f $CNFFILE ]; then
		if [ ! -d "$BCKFILEDIR" ]; then
			$CMD_MKDIR $BCKFILEDIR
			ERR=$?
			if [ $ERR != 0 ]; then
				logmsg "$dlmsetoptid13" "$BCKFILEDIR"
				exit 1
			fi
		else
			if [ -f $BCKFILE ]; then
				logmsg "$dlmsetoptid16"
				REMOVE_BACKUP=$FALSE
				exit 1
			fi
		fi
		$CMD_CP  $CNFFILE $BCKFILE
		ERR=$?
		if [ $ERR != 0 ]; then
			logmsg "$dlmsetoptid14" "$CNFFILE"
			exit 1
		fi
	fi
}
# Method to validate the arguments
#if valid:0 else 1
validate()
{
	# Tests whether *entire string* is numerical.
	if [ $# -eq 1 ]; then
		PARAM_LENGTH=`$CMD_ECHO "$1" | $CMD_WC -c`
		CURRENT=`$CMD_ECHO "$1" | $CMD_CUT -c 1`
		if [ "$CURRENT" == "-" ]; then			
			MINUS=YES
			INDEX=2
		else
			MINUS=NO
			INDEX=1
		fi
		RETRY_COUNT=0
		while let "LOOP_COUNTER=1"; do
			CURRENT=`$CMD_ECHO "$1" | $CMD_CUT -c $INDEX`
			case $CURRENT in
				*[!0-9]*|"")
					return 1
				;;
				*)
					let "RETRY_COUNT=$RETRY_COUNT*10 + $CURRENT"
					let "INDEX=$INDEX + 1"
					if [ $INDEX -ge $PARAM_LENGTH ]; then
						if [ $MINUS == YES ]; then
							let "RETRY_COUNT= 0 - $RETRY_COUNT"
						fi
				 		if [ $RETRY_COUNT -lt -1 ] || [ $RETRY_COUNT -gt 2147483646 ]; then
							return 1
						fi
				 		return 0
					fi
				;;
			esac
		done
	else
		return 1
	fi
}

# Method to validate the arguments for inquiry timeout
#if valid:0 else 1
validate_timeout()
{
	# Tests whether *entire string* is numerical.
	if [ $# -eq 1 ]; then
		PARAM_LENGTH=`$CMD_ECHO "$1" | $CMD_WC -c`
		CURRENT=`$CMD_ECHO "$1" | $CMD_CUT -c 1`
		if [ "$CURRENT" == "-" ]; then			
			MINUS=YES
			INDEX=2
		else
			MINUS=NO
			INDEX=1
		fi
		INQTIMEOUT_COUNT=0

		while let "LOOP_COUNTER=1"; do
			CURRENT=`$CMD_ECHO "$1" | $CMD_CUT -c $INDEX`
			case $CURRENT in
				*[!0-9]*|"")
					return 1
				;;
				*)
					
						let "INQTIMEOUT_COUNT=$INQTIMEOUT_COUNT*10 + $CURRENT"
						let "INDEX=$INDEX + 1"
						if [ $INDEX -ge $PARAM_LENGTH ]; then
							if [ $MINUS == YES ]; then
								let "INQTIMEOUT_COUNT= 0 - $INQTIMEOUT_COUNT"
							fi
							if [ $INQTIMEOUT_COUNT -eq 0 ]; then
								return 1
							fi
					 		if [ $INQTIMEOUT_COUNT -lt -1 ] || [ $INQTIMEOUT_COUNT -gt 3600 ]; then
								return 1
							fi
					 		return 0
						fi
					
				
				;;
			esac
		done
	else
		return 1
	fi
}

#Method for parsing the command line arguments
arg_parser()
{
	CURR_OPTION=""
        for ARG in $@
        do
           case $ARG in
			-r)
				if [ $OPT_R_FLAG == $TRUE ] || [ $OPT_H_FLAG == $TRUE ] || [ $OPT_U_FLAG == $TRUE ] || [ $OPT_INQT_FLAG == $TRUE ] || [ $OPT_INQR_FLAG == $TRUE ]; then
					return 1
				fi
				OPT_R_FLAG=$TRUE
				CURR_OPTION="r"
				continue
				;;
			-h)
				if [ $OPT_R_FLAG == $TRUE ] || [ $OPT_H_FLAG == $TRUE ] || [ $OPT_U_FLAG == $TRUE ] || [ $OPT_INQT_FLAG == $TRUE ] || [ $OPT_INQR_FLAG == $TRUE ]; then
					return 1
				fi
				OPT_H_FLAG=$TRUE
				CURR_OPTION="h"
				continue
				;;
			-u)
				if [ $OPT_R_FLAG == $TRUE ] || [ $OPT_H_FLAG == $TRUE ] || [ $OPT_U_FLAG == $TRUE ] || [ $OPT_INQT_FLAG == $TRUE ] || [ $OPT_INQR_FLAG == $TRUE ]; then
					return 1
				fi
				OPT_U_FLAG=$TRUE
				CURR_OPTION="u"
				continue
				;;
			-inqt)
				if [ $OPT_R_FLAG == $TRUE ] || [ $OPT_H_FLAG == $TRUE ] || [ $OPT_U_FLAG == $TRUE ] || [ $OPT_INQT_FLAG == $TRUE ] || [ $OPT_INQR_FLAG == $TRUE ]; then
					return 1
				fi
				OPT_INQT_FLAG=$TRUE
				CURR_OPTION="inqt"
				continue
				;;
			-inqr)
				if [ $OPT_R_FLAG == $TRUE ] || [ $OPT_H_FLAG == $TRUE ] || [ $OPT_U_FLAG == $TRUE ] || [ $OPT_INQT_FLAG == $TRUE ] || [ $OPT_INQR_FLAG == $TRUE ]; then
					return 1
				fi
				OPT_INQR_FLAG=$TRUE
				CURR_OPTION="inqr"
				continue
				;;
			-[!0-9]*)
				OPTION=$ARG
				message_txt
				logmsg "$dlmsetoptid11"
				return 1
				;;
			*)
				if [ "$CURR_OPTION" != "" ]; then
					case $CURR_OPTION in
						r) 	
							R_PARAM_LIST=`$CMD_ECHO "$R_PARAM_LIST$ARG "`
							;;
						h) 
							H_PARAM_LIST=`$CMD_ECHO "$H_PARAM_LIST$ARG "`
							;;
						u)
							U_PARAM_LIST=`$CMD_ECHO "$U_PARAM_LIST$ARG "`
							;;

						inqt)   
							INQT_PARAM_LIST=`$CMD_ECHO "$INQT_PARAM_LIST$ARG "`
							;;

						inqr)
							INQR_PARAM_LIST=`$CMD_ECHO "$INQR_PARAM_LIST$ARG "`
							;;
					esac
				else
					OPTION=$ARG
					message_txt
					logmsg "$dlmsetoptid11"
					return 1
				fi
				;;
		esac
	done
	return 0
}


#############################   MAIN   ######################################

PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"
export PATH

# Command variable start here
CMD_ECHO=/bin/echo
CMD_BASENAME=/bin/basename
CMD_UNAME=/bin/uname
CMD_GREP=/bin/grep
CMD_CP=/bin/cp
CMD_MKDIR=/bin/mkdir
CMD_MV=/bin/mv
CMD_EXPR=/usr/bin/expr
CMD_SED=/bin/sed
CMD_RM=/bin/rm
CMD_TOUCH=/bin/touch
CMD_WC=/usr/bin/wc
CMD_HEAD=/usr/bin/head
CMD_TAIL=/usr/bin/tail
CMD_CAT=/bin/cat
CMD_CHMOD=/bin/chmod
CMD_CHOWN=/bin/chown
if [ -f /etc/redhat-release ]; then
	CMD_LOGGER=/usr/bin/logger
	CMD_CUT=/bin/cut
elif [ -f /etc/SuSE-release ]; then
	CMD_LOGGER=/bin/logger
	CMD_CUT=/usr/bin/cut
fi
# Command Variables end here

# Script Variables start here
FILENAME=dlmsetopt
BCKFILEDIR=/etc/opt/DynamicLinkManager/DLMSETOPT/
SYSLOG_TAG=dlmsetopt
MODULES_STRING_COMMON="options sddlmfdrv"
MODULES_STRING_RETRY="allocretrycount"
MODULES_STRING_INQTIMEOUT="inquirytimeout"
MODULES_STRING_INQRETRY="inquiryretries"
TMPDIR=/etc/opt/DynamicLinkManager
EDITED=0
SCRIPT_NAME=`$CMD_BASENAME $0`
LOCKFILE=$TMPDIR/.dlm_modules.conf.lock
AS4_RELEASE_STRING="Red Hat Enterprise Linux AS release 4"
ES4_RELEASE_STRING="Red Hat Enterprise Linux ES release 4"
AS4_FLAG=0
HDLM_COMMON_KNL_CHK="/etc/sysconfig/hdlm-scripts/dlmcommonknlcheck"

KERNEL_VERSION=`$CMD_UNAME -r |$CMD_CUT -d "." -f2`
if [ "$KERNEL_VERSION" = "4" ]; then 
	CNFFILE=/etc/modules.conf
	BCKFILE=$BCKFILEDIR/modules.conf
	CNFHEADFILE=$BCKFILEDIR/modules.conf.head
	CNFTAILFILE=$BCKFILEDIR/modules.conf.tail
elif [ "$KERNEL_VERSION" = "6" ]; then
	CNFFILE=/etc/modprobe.conf
	BCKFILE=$BCKFILEDIR/modprobe.conf
	CNFHEADFILE=$BCKFILEDIR/modprobe.conf.head
	CNFTAILFILE=$BCKFILEDIR/modprobe.conf.tail
fi
if [ "$KERNEL_VERSION" = "6" ] && [ -f /etc/redhat-release ] ; then
	SE_LINUX=1
	CMD_CHCON=/usr/bin/chcon
else
	SE_LINUX=0
fi
TEMP_EXTN_COUNTER=""
CNFFILE_BAK="$CNFFILE""_dlm"
CNFFILE_TMP="$CNFFILE""_tmp_dlm"
RETRY_COUNT=-1
# Script Variables end here

#Flag and List declaration start
TRUE=0
FALSE=1
OPT_R_FLAG=$FALSE
OPT_H_FLAG=$FALSE
OPT_U_FLAG=$FALSE
OPT_INQT_FLAG=$FALSE
OPT_INQR_FLAG=$FALSE
R_PARAM_LIST=""
U_PARAM_LIST=""
H_PARAM_LIST=""
INQT_PARAM_LIST=""
INQR_PARAM_LIST=""
PARAM=""
OTHERSCRIPT=$TRUE
MESSAGE_DISPLAYED=$FALSE
REMOVE_BACKUP=$TRUE
#Flag and List declaration ends

# Handle the following signals :
# 1: SIGHUP, 2: SIGINT, 3: SIGQUIT, 15: SIGTERM.
trap "cleanup" EXIT
trap "signal_handler" 1 2 3 15

#Method to check whether AS4 kernel is used.
check

# check for name of script
message_txt
if [ "$SCRIPT_NAME" != "$FILENAME" ]; then
	logmsg "$dlmsetoptid1"
	exit 1
fi

###########################################################################
#Kernel Version Agreement Check
#For matching kernel version-START#
if [ "$FROM_SPEC" != 1 ]; then
	if  [ -f $HDLM_COMMON_KNL_CHK ]; then
		. $HDLM_COMMON_KNL_CHK
		if [ "$HDLM_COMMON_RESULT" != "MATCH" ]; then
			if [ "$HDLM_COMMON_RESULT" == "UNMATCH" ]; then
				logmsg $dlmsetoptid17
			else
				logmsg $dlmsetoptid18 "$HDLM_COMMON_KNL_VER_FILE"
			fi
			exit 1
		fi
	else
		logmsg $dlmsetoptid18 "$HDLM_COMMON_KNL_CHK"
		exit 1
	fi
fi

#For matching kernel version-END#
##########################################################################

# Check when no option is specified (Ex: ./dlmsetopt )
if [ $# -eq 0 ]; then
	logmsg "$dlmsetoptid10"
	usage
	exit 1
fi
#Check for uninstallation
if [ "$HIDDEN_OPT" == "" ]; then
	HIDDEN_OPT=0
fi

#Check for Option and their parameter
arg_parser $@
ERROR_FLAG=$?
#check the error flag
if [ "$ERROR_FLAG" = "1" ] || [ `$CMD_ECHO "$H_PARAM_LIST" | $CMD_WC -w` -ne 0 ] || [ `$CMD_ECHO "$U_PARAM_LIST" | $CMD_WC -w` -ne 0 ]; then
	usage
	exit 1
fi

#Check for uninstallation
if [ $OPT_U_FLAG == $TRUE ]; then
	if [ "$HIDDEN_OPT" == 0 ]; then
		OPTION="-u"
		message_txt
		logmsg "$dlmsetoptid11"
		usage 
		exit 1
	fi
fi
# Check for option is not "-h"
if [ $OPT_H_FLAG == $TRUE ]; then		# option processing "h"
	OPTION="-h"
	message_txt
	MESSAGE_DISPLAYED=$TRUE
	logmsg "$dlmsetoptid4"
	help
	logmsg "$dlmsetoptid5"
	exit 0
fi

# check whether single instance of script is running
if [ -f $LOCKFILE ]; then
	logmsg "$dlmsetoptid2"
	exit 1
else
	if [ ! -d $TMPDIR ]; then
		logmsg "$dlmsetoptid12" "$TMPDIR"
		exit 1
	fi
	$CMD_TOUCH $LOCKFILE
	OTHERSCRIPT=$FALSE
fi

#Check the CNF file status and create back up file and directories.
chkcnffile

if  [ $OPT_R_FLAG == $TRUE ]; then		# option processing "r"
	OPTION="-r"
	PARAM=`$CMD_ECHO "$R_PARAM_LIST"`
	message_txt
	MESSAGE_DISPLAYED=$TRUE
	logmsg "$dlmsetoptid4"
	if [ `$CMD_ECHO "$PARAM" | $CMD_WC -c` -gt 12 ]; then #check the length of PARAM
		PARAM=`$CMD_ECHO "${PARAM:0:10}..."`
		message_txt
		logmsg "$dlmsetoptid9"
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	validate $PARAM
	RET_VAL=$?
	if [ "$RET_VAL" = "1" ]; then
		logmsg "$dlmsetoptid9"
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	PARAM=$RETRY_COUNT
	set_option $MODULES_STRING_RETRY $PARAM
	ERR=$?
	if [ $ERR -ne 0 ]; then
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	logmsg "$dlmsetoptid5"
	logmsg "$dlmsetoptid8"
fi

if  [ $OPT_INQR_FLAG == $TRUE ]; then		# option processing "inqr"
	OPTION="-inqr"
	PARAM=`$CMD_ECHO "$INQR_PARAM_LIST"`
	message_txt
	MESSAGE_DISPLAYED=$TRUE
	logmsg "$dlmsetoptid4"
	if [ `$CMD_ECHO "$PARAM" | $CMD_WC -c` -gt 12 ]; then #check the length of PARAM
		PARAM=`$CMD_ECHO "${PARAM:0:10}..."`
		message_txt
		logmsg "$dlmsetoptid9"
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	validate $PARAM
	RET_VAL=$?
	if [ "$RET_VAL" = "1" ]; then
		logmsg "$dlmsetoptid9"
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	PARAM=$RETRY_COUNT
	if [ $PARAM -lt 0 ]; then
		PARAM=-1    #Inquiry retry default value
	fi	
	set_option $MODULES_STRING_INQRETRY $PARAM
	ERR=$?
	if [ $ERR -ne 0 ]; then
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	logmsg "$dlmsetoptid5"
	logmsg "$dlmsetoptid8"
fi

if  [ $OPT_INQT_FLAG == $TRUE ]; then		# option processing "inqt"
	OPTION="-inqt"
	PARAM=`$CMD_ECHO "$INQT_PARAM_LIST"`
	message_txt
	MESSAGE_DISPLAYED=$TRUE
	logmsg "$dlmsetoptid4"
	if [ `$CMD_ECHO "$PARAM" | $CMD_WC -c` -gt 6 ]; then #check the length of PARAM
		PARAM=`$CMD_ECHO "${PARAM:0:4}..."`
		message_txt
		logmsg "$dlmsetoptid9"
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	validate_timeout $PARAM
	RET_VAL=$?
	if [ "$RET_VAL" = "1" ]; then
		logmsg "$dlmsetoptid9"
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	PARAM=$INQTIMEOUT_COUNT
	if [ $PARAM -lt 0 ]; then
		PARAM=-1    #Inquiry timeout default value
	fi	
	set_option $MODULES_STRING_INQTIMEOUT $PARAM
	ERR=$?
	if [ $ERR -ne 0 ]; then
		logmsg "$dlmsetoptid6"
		exit 1
	fi
	logmsg "$dlmsetoptid5"
	logmsg "$dlmsetoptid8"
fi

if [ $OPT_U_FLAG == $TRUE ]; then		# option processing "u"
	uninstall
	RET_VAL=$?
	if [ $RET_VAL -ne 0 ]; then
		exit 1
	fi
fi

exit 0
