#!/bin/sh

#ident "$script: dlmlfk Build 5.90.0.8 07/02/09 14:41:00 $"
#----------------------------------------------------------------------------#
# PRODUCT :  HDLM Driver 0541                                                # 
#----------------------------------------------------------------------------#
# NAME :     dlmlfk                                                          #
# FUNCTION : dlmlfk script to support Lifkeeper with HDLM                    #
#  	     Usage: dlmlfk -c|-u                                             #
#            -c  option configures LifeKeeper to support LifeKeeper.         #
#            -u  option restores the orignal LifeKeeper configuration.       #
# INSTANCE:                                                                  #
# ATTR:      500                                                             #
# MEMO :     dlmlfk execution shell script                                   #
# DATE :     2004/08/05                                                      #
# AUTHOR :   Mihir Punjabi                                                   #
# HISTORY :                                                                  #
#                                                                            #
#@ All Rights Reserved. Copyright (C) 2004,2007 Hitachi, Ltd.                #
#----------------------------------------------------------------------------#

# Macro defining Standard Paths for commands
LOGGER=/usr/bin/logger
ECHO=/bin/echo
BASENAME=/bin/basename
RPM=/bin/rpm
GREP=/bin/grep
CP=/bin/cp
MKDIR=/bin/mkdir
AWK=/bin/awk
MV=/bin/mv
SED=/bin/sed
RM=/bin/rm
CHMOD=/bin/chmod
CAT=/bin/cat
LS=/bin/ls
CUT=/bin/cut
WC=/usr/bin/wc

##############################################################################
# Script Variables start here

DEFAULTLK=/etc/default/LifeKeeper
SYSLOG_TAG=dlmlfk
DISK_MAPPING_SH=/opt/DynamicLinkManager/bin/`$BASENAME $0`
SCRIPT=dlmlfk
LFK_VER_SUPPORT=4.4.3
TRUE=1
DLNKMGR_DIR="/etc/opt/DynamicLinkManager"
export LOCKFILE="$DLNKMGR_DIR/.dlmlfk.lock"
export REMOVE_LOCKFILE=0
export CLEANUP=0
export BACKUP_ABSENT=0
export ABORT=0
ERR_FLAG=0
ERR_MESSAGE=0
DIR_CREATE=0
HDLM_COMMON_KNL_CHK="/etc/sysconfig/hdlm-scripts/dlmcommonknlcheck"
# Script Variables end here

##############################################################################
# Messages start here

#Varibales for messges:
if [ "$1" == "-c" -o "$1" == "-u" ]; then
  OPTION="$1"
else
  OPTION=""
fi
DESTINATION="Destination"
ERRCODE="Errno"
ARG=$1
CURR_LANG=$LANG
if [ "$CURR_LANG" == "ja_JP.eucjp" -o "$CURR_LANG" == "ja_JP.eucJP" ]; then
Lksupport_I01="KAPL12001-I LifeKeeperݡȥƥƥ$OPTIONưޤ"
Lksupport_I02="KAPL12002-I LifeKeeperݡȥƥƥ$OPTIONｪλޤ"
Lksupport_I03="KAPL12003-I LifeKeeper򥵥ݡȤ뤿ѹϤǤ¸ߤƤޤե̾ = "
Lksupport_I04="KAPL12004-I LifeKeeperݡȥƥƥ$OPTIONμ¹Ԥ˼Ԥޤ"
Lksupport_I07="KAPL12005-I ǥ쥯ȥ꤬¸ߤޤ󡣥ǥ쥯ȥ = "
#Lksupport_I09="KAPL12006-I: ե뤬¸ߤޤ󡣥ե̾ "
Lksupport_I09="KAPL12006-I HDLMޥåԥ󥰾Lifekeeperdisk.mappingǥ쥯ȥ˸ĤޤǤ"
Lksupport_I10_ID="KAPL12007-I "
Lksupport_I10="ꥹȥ뤿"
I10_REMAINING="ѤƤ"
Lksupport_E16="KAPL12008-E Υեϥǥեȥȥ꤬ޤޤƤޤ󡣥ե̾ = "
Lksupport_E17="KAPL12009-E եιǤޤǤե̾ = "
Lksupport_I11="KAPL12010-I dlmlfk $OPTIONμ¹Ԥϥ桼ˤäǤޤ"
Lksupport_W03="KAPL12011-E dlmlfk $OPTIONǽ˼Ԥޤ"
Lksupport_E00="KAPL12012-E ʰLifeKeeperݡȥƥƥ¹ԤޤParameter = "
Lksupport_E01="KAPL12013-E LifeKeeperݡȥƥƥdlmlfkȤ̾ΤΤߤǼ¹ԤǤޤ"
Lksupport_E02="KAPL12014-E ɬפʾ뤳ȤǤޤǤե̾ = "
Lksupport_E03="KAPL12015-E LifeKeeperưǤ"
Lksupport_E04="KAPL12016-E dlmlfk¹Ԥ뤿ɬפʥե롢ޤϥǥ쥯ȥ꤬ĤޤǤFile or directory name = "
Lksupport_E05="KAPL12017-E եΥХååפ˼Ԥޤե̾ = "
Lksupport_E07="KAPL12018-E ǥ쥯ȥκ˼Ԥޤǥ쥯ȥ = "
Lksupport_W04="KAPL12019-W եιǤޤǤե̾ = "
Lksupport_E08="KAPL12020-E LifeKeeper󥹥ȡ뤵Ƥޤ"
Lksupport_E10="KAPL12021-E HDLMΥ饯ڥե뤬Ĥޤ󡣥ե̾ = "
Lksupport_E14="KAPL12022-E եμ¹Ը¤ޤ󡣥ե̾ = "
Lksupport_E15="KAPL12023-E LifeKeeperݡȥƥƥdlmlfkˤʣƱ˼¹ԤǤޤ"
Lksupport_W00="KAPL12024-W : /opt/DynamicLinkManager/bin/dlmlfk {-c| -u}"
Lksupport_W01="KAPL12025-W եκ˼Ԥޤǥ쥯ȥ = "
#Lksupport_W01="KAPL12025-W: եκ˼Ԥޤե̾ = "
Lksupport_E18="KAPL12026-E HDLMݡѤΥǥޥåԥ󥰤뤿˼¹Ԥdlmlfkμ¹Ԥ˼Ԥޤ"
Lksupport_E19="KAPL12051-E 󥹥ȡ뤵ƤHDLMбƤ륫ͥΥСȡưƤ륫ͥΥС󤬰פޤ"
Lksupport_E20="KAPL12052-E HDLMΥե뤬Ǥե̾ = "
else
Lksupport_I01="KAPL12001-I The utility for supporting LifeKeeper $OPTION started."
Lksupport_I02="KAPL12002-I The utility for supporting LifeKeeper $OPTION is successful."
Lksupport_I03="KAPL12003-I Support change already exist in "
Lksupport_I04="KAPL12004-I The utility for supporting LifeKeeper $OPTION failed."
Lksupport_I07="KAPL12005-I Directory does not exist. Directory = "
Lksupport_I09="KAPL12006-I HDLM mapping information could not be found in the Lifekeeper disk.mapping directory."
Lksupport_I10="KAPL12007-I Please use "
I10_REMAINING="to restore "
Lksupport_I11="KAPL12010-I Execution of the utility for supporting LifeKeeper $OPTION is aborted by user."
#F-Doc50801016 : KAPL Message Correction - start
#Lksupport_E00="KAPL12012-E Invalid parameter passed to the utility for supporting LifeKeeper. Parameter= "
Lksupport_E00="KAPL12012-E Invalid parameter passed to the utility for supporting LifeKeeper. Parameter = "
#F-Doc50801016 : KAPL Message Correction - end
Lksupport_E01="KAPL12013-E This script can only be executed with the name dlmlfk."
#F-Doc50801016 : KAPL Message Correction - start
#Lksupport_E02="KAPL12014-E Necessary information could not be obtained. File name= "
Lksupport_E02="KAPL12014-E Necessary information could not be obtained. File name = "
#F-Doc50801016 : KAPL Message Correction - end
Lksupport_E03="KAPL12015-E LifeKeeper is running."
Lksupport_E04="KAPL12016-E Necessary file or directory does not exist. File or directory name = "
Lksupport_E05="KAPL12017-E Failed to copy file. File name = "
Lksupport_E07="KAPL12018-E Failed to create directory. Directory = "
Lksupport_E08="KAPL12020-E LifeKeeper is not installed."
Lksupport_E10="KAPL12021-E Could not find HDLM character special file. File name = "
Lksupport_E14="KAPL12022-E File does not have executable permission. File name = "
Lksupport_E15="KAPL12023-E Multiple instances of the utility for supporting LifeKeeper cannot be executed concurrently."
#F-Doc50801016 : KAPL Message Correction - start
#Lksupport_E16="KAPL12008-E The file does not contain default entries. File name= "
Lksupport_E16="KAPL12008-E The file does not contain default entries. File name = "
#F-Doc50801016 : KAPL Message Correction - end
Lksupport_E17="KAPL12009-E Could not modify file. File name = "
Lksupport_E18="KAPL12026-E Execution of dlmlfk to configure disk.mapping files for HDLM support failed."
Lksupport_W00="KAPL12024-W Usage: $DISK_MAPPING_SH {-c| -u}"
#F-Doc50801016 : KAPL Message Correction - start
#Lksupport_W01="KAPL12025-W Failed to remove directory. Directory = "
Lksupport_W01="KAPL12025-W Failed to remove directory. Directory name = "
#F-Doc50801016 : KAPL Message Correction - end
Lksupport_W03="KAPL12011-E Abort processing for the utility for supporting LifeKeeper $OPTION completed with errors."
Lksupport_W04="KAPL12019-W Could not modify file. File name = "
Lksupport_E19="KAPL12051-E The version of the kernel supported by the installed HDLM does not match the currently running kernel version."
Lksupport_E20="KAPL12052-E The HDLM file is invalid. File name ="

fi

# Messages end here
##############################################################################

if [ "$MESSAGELOG_STATUS" == "" ]; then
MESSAGELOG_STATUS=0
fi

MESSAGES_SPEC="$Lksupport_E02 \
		$Lksupport_E03 \
		$Lksupport_W01 \
		$Lksupport_W04 \
		$Lksupport_I10 \
		$Lksupport_E14 \
		$Lksupport_E15"

MESSAGES_DLMLKSUPPORT="$Lksupport_E15"

MESSAGES_ALWAYS="$Lksupport_E00 \
		  $Lksupport_E01 \
		  $Lksupport_E02 \
		  $Lksupport_E03 \
		  $Lksupport_E04 \
		  $Lksupport_E05 \
		  $Lksupport_E07 \
		  $Lksupport_E08 \
		  $Lksupport_E10 \
		  $Lksupport_E14 \
		  $Lksupport_E15 \
		  $Lksupport_E17 \
		  $Lksupport_E18 \
		  $Lksupport_E19 \
		  $Lksupport_E20"

##############################################################################
# Method to log messages to /var/log/messages and console

logmsg(){
	if [ $MESSAGELOG_STATUS -eq 0 ]; then
	  $ECHO $MESSAGES_DLMLKSUPPORT | $GREP -s "$1" > /dev/null 2>&1
		if [ $? -ne 0 ]; then

			$LOGGER -t $SYSLOG_TAG "$1$2$3$4"
		fi
	else
		$LOGGER -t $SYSLOG_TAG "$1$2$3$4"

	fi
	if [ $MESSAGELOG_STATUS -eq 1 ]; then
	  $ECHO $MESSAGES_SPEC | $GREP -s "$1" > /dev/null 2>&1
		if [ $? -eq 0 ]; then

			 $ECHO "$1$2$3$4" 
		fi
	else
	
		$ECHO "$MESSAGES_ALWAYS" | $GREP -s "$1" > /dev/null 2>&1
		if [ $? -eq 0 ]; then
		        $ECHO "$1$2$3$4"
		else
		        if [ "$option" != "r" ]; then
		                $ECHO "$1$2$3$4"
		        fi
		fi
	fi
}

##############################################################################
# check for name of script

if [ `$BASENAME $0` != $SCRIPT ]
then
	logmsg "$Lksupport_E01"
	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 "$Lksupport_E19"
			else
				logmsg "$Lksupport_E20" "$HDLM_COMMON_KNL_VER_FILE"
			fi
			exit 1
		fi
	else
		logmsg "$Lksupport_E20" "$HDLM_COMMON_KNL_CHK"
		exit 1
	fi
fi
#For matching kernel version-END#
##########################################################################




# check for invalid options

getopts curv option > /dev/null 2>&1
if [ $# -ne 1 ] || [ $OPTIND -ne 2 ]
then
	$ECHO "$Lksupport_W00"
	if [ "$MESSAGELOG_STATUS" == "0" ]; then
		exit 1
	else
		exit -1
	fi
fi


if [ "$option" == "v" ]; then
	if [ $MESSAGELOG_STATUS -eq 0 ]; then
		logmsg "$Lksupport_E00" "$ARG"
		$ECHO "$Lksupport_W00"
		exit 1
	fi
else
	if [ "$option" != "c" ] && [ "$option" != "u" ] && [ "$option" != "r" ]; then
		logmsg "$Lksupport_E00" "$ARG"
		$ECHO "$Lksupport_W00"
		exit 1
	fi
fi

##############################################################################
# check for LFK version

LFK_VERSION=`$RPM -qi steeleye-lk | $AWK '/Version/ {print $3}'`
if [ -z $LFK_VERSION ]
then
	ERR_FLAG=1
	if [ "$1" == "-u" ]; then
		$RM -rf $DLNKMGR_DIR/DLMLFK > /dev/null 2>&1
	fi
	#exit 0
	if [ "$MESSAGELOG_STATUS" == "0" ]; then
		logmsg "$Lksupport_E08"
		exit 1
	else
		exit 0
	fi
fi

##############################################################################
# check for lifekeeper default file

if [ -f $DEFAULTLK ]
then
	source $DEFAULTLK
else
	logmsg "$Lksupport_E02" "$DEFAULTLK"
	if [ "$MESSAGELOG_STATUS" == "0" ]; then
		exit 1
	else
		exit 2
	fi
fi

##############################################################################
# Script Variables start here

DEVPTRN_DSK=$LKROOT/subsys/scsi/resources/disk/device_pattern
DEVPTRN_HST=$LKROOT/subsys/scsi/resources/hostadp/device_pattern
LKSCRIPT=$LKROOT/subsys/scsi/actions/lk_cspec
DISKMAPPING=$LKROOT/subsys/scsi/resources/disk/disk.mapping
BACKUP_DIR=$DLNKMGR_DIR/DLMLFK
TMP_DIR=$BACKUP_DIR/tmp
DISKMAPPING_BAK=$TMP_DIR/disk.mapping
DEVPTRN_TMP=$DLNKMGR_DIR/.device_pattern
DEVPTRN_TMP1=$DLNKMGR_DIR/.device_pattern1
LKSCRIPT_TMP=$DLNKMGR_DIR/.lk_cspec
BACKUP_DEVPTRN_DSK=$BACKUP_DIR/device_pattern_dsk
BACKUP_DEVPTRN_HST=$BACKUP_DIR/device_pattern_hst
BACKUP_LKSCRIPT=$BACKUP_DIR/lk_cspec
LKTEST=$LKROOT/bin/lktest
COMMENT_STRING=#HDLM-dlmlfk

# Script Variables end here
##############################################################################
# check for status of Lifekeeper

checklkstatus(){
	if [ -x $LKTEST ]; then
		$LKTEST > /dev/null 2>&1
		if [ $? == 0 ]
		then
			logmsg "$Lksupport_E03"
			exit 1
		fi
        else
	        logmsg "$Lksupport_E14" "$LKTEST"
		if [ "$MESSAGELOG_STATUS" == "0" ]; then
			exit 1
		else
			exit 2
		fi
	fi
}


##############################################################################
#Check if $DLNKMGR_DIR exists

if [ ! -d $DLNKMGR_DIR ]; then
	logmsg "$Lksupport_E04" "$DLNKMGR_DIR"
	if [ "$MESSAGELOG_STATUS" == "0" ]; then
		exit 1
	else
		exit 3
	fi
fi
##############################################################################

##############################################################################
#Check if single instance of lksupport script is running
check_lockfile_status() {

export EXIT_VAL=$?

	if [ "$REMOVE_LOCKFILE" -eq 0 ]; then

		#if [ $CLEANUP -eq 1 ]; then
			#if [ -d $DISKMAPPING_BAK ]; then

		case $CLEANUP in
 		1)
		 if [ -d $TMP_DIR ]; then
			$RM -rf $TMP_DIR > /dev/null 2>&1
		 fi
		;;
 		2)
		 if [ -d $BACKUP_DIR ]; then
		 	$RM -rf $BACKUP_DIR > /dev/null 2>&1
		 fi
		;;
 		*)
		 if [ -d $DISKMAPPING_BAK ]; then
	         	$RM -rf $DISKMAPPING_BAK > /dev/null 2>&1
		 fi
		;;
 		esac

		$RM -f $LOCKFILE > /dev/null 2>&1
    	fi

	if [ "$option" == "c" -o "$option" == "u" ]; then
	    	if [ $ABORT -eq 1 ]; then
			logmsg "$Lksupport_I11"
		fi
    		if [ $ABORT -eq 2 ]; then
    			if [ $EXIT_VAL -eq 0 ]; then
			logmsg "$Lksupport_I11"
    			else
    				logmsg "$Lksupport_W03"
			fi
    		fi
    	fi
if [ "$option" == "c" -o "$option" == "u" -o "$option" == "r" -o "$option" == "v" ]; then
 if [ $ABORT -ne 1 -o $ABORT -ne 2 ]; then

        if [ $EXIT_VAL -eq 0 -a $ERR_FLAG -eq 0 ]; then
          if [ $ERR_MESSAGE -eq 1 ]; then
	    logmsg "$Lksupport_I02"
	  fi
	else
	  if [ $ERR_MESSAGE -eq 1 ]; then
	    logmsg "$Lksupport_I04"
	  fi
	fi
 else
          if [ $ERR_MESSAGE -eq 1 ]; then
	    logmsg "$Lksupport_I04"
	  fi
 fi
fi
}

##############################################################################
#method to check existence of LCD files

checklcd()
{

LCD_CORRUPT=0

if [ ! -f $DEVPTRN_DSK ]; then
	logmsg "$Lksupport_E04" "$DEVPTRN_DSK"
	LCD_CORRUPT=1
fi

if [ ! -f $DEVPTRN_HST ]; then
	logmsg "$Lksupport_E04" "$DEVPTRN_HST"
	LCD_CORRUPT=1
fi

if [ ! -f $LKSCRIPT ]; then
	logmsg "$Lksupport_E04" "$LKSCRIPT"
	LCD_CORRUPT=1
fi

if [ $LCD_CORRUPT -eq 1 ]; then
	if [ "$MESSAGELOG_STATUS" == "0" ]; then
		exit 1
	else
		exit 2
	fi
fi

}

##############################################################################


###############################################################################
#Interrupt Handler for lifekeeper Start
#

###############################################################################
#Function to check the consistencey of modified files
#

lksupport_status()
{
	checklcd
	    $GREP "^\/dev\/sddlm\[a-p\]\[a-p\]$" $DEVPTRN_DSK > /dev/null 2>&1	    
	      if [ $? -eq 0 ]
	      then
		GREP1=0 
              else
                GREP1=1 
              fi 

	    $GREP "^\/dev\/sddlm\[a-p\]\[a-p\]$" $DEVPTRN_HST > /dev/null 2>&1	    
	      if [ $? -eq 0 ]
	      then
                GREP2=0 
              else
                GREP2=1 
              fi    

	   $GREP "^if( -x \"$DISK_MAPPING_SH\" ) {    $COMMENT_STRING$" $LKSCRIPT > /dev/null 2>&1
	      if [ $? -eq 0 ]
	      then
                GREP3=0 
              else
                GREP3=1 
              fi

# To check the configuration status of LifeKeeper for HDLM
           if [ $GREP1 -eq 0 -a $GREP2 -eq 0 -a $GREP3 -eq 0 ]; then
                PROCESSING=0  # LifeKeeper files configured for HDLM        
           else
                
              if [ $GREP1 -eq 1 -a $GREP2 -eq 1 -a $GREP3 -eq 1 ]; then
		PROCESSING=1  # LifeKeeper files not configured for HDLM    
              else
		PROCESSING=2  # LifeKeeper files are in inconsistent state
              fi
           fi

}

################################################################################
#Function for clean up of interrupted script 
#
trap_handler(){

export ABORT=2

	case $option in
	c)
	   lksupport_status > /dev/null 2>&1
	   if [ $PROCESSING -ne 0 ]; then

		if [ -f $LKSCRIPT_TMP$$ ]; then
	          $RM -f $LKSCRIPT_TMP$$ > /dev/null 2>&1
	        fi
	
	        if [ -f $DEVPTRN_TMP$$ ]; then
	          $RM -f $DEVPTRN_TMP$$ > /dev/null 2>&1
	        fi
	        if [ -f $DEVPTRN_TMP1$$ ]; then
		  $RM -f $DEVPTRN_TMP1$$ > /dev/null 2>&1
	        fi

		unconfigure > /dev/null 2>&1
	   fi

	   export ABORT=1
		;;
	u)

	   lksupport_status > /dev/null 2>&1 

	   if [ $PROCESSING -eq 2 ]; then

	        if [ -f $LKSCRIPT_TMP$$ ]; then
	        $RM -f $LKSCRIPT_TMP$$ > /dev/null 2>&1
	        fi
	
	        if [ -f $DEVPTRN_TMP$$ ]; then
	        $RM -f $DEVPTRN_TMP$$ > /dev/null 2>&1
	        fi

	        if [ -f $DEVPTRN_TMP1$$ ]; then
		  $RM -f $DEVPTRN_TMP1$$ > /dev/null 2>&1
	        fi

		configure > /dev/null 2>&1
	   fi

      	   export ABORT=1
		;;
	r)	
		if [ -d $DISKMAPPING_BAK ]; then
                   $CP -f $DISKMAPPING_BAK/* $DISKMAPPING/ > /dev/null 2>&1
		    ERR=$?
		    if [ $ERR -ne 0 ]; then
		      logmsg "$Lksupport_E05" "$DISKMAPPING_BAK/*," " $ERRCODE = $ERR" > /dev/null 2>$1
		   fi
		else
                      logmsg "$Lksupport_E04" "$DISKMAPPING_BAK" > /dev/null 2>$1
                      export ABORT=1
			if [ "$MESSAGELOG_STATUS" == "0" ]; then
				exit 1
			else
				exit -1
			fi 
		fi

   	        export ABORT=1
		;;
	v)	

		export ABORT=1
		;;
	*)

		export ABORT=1
		;;
	esac

	exit 1
}

#Exit for cases where -u option is run from the prompt and for -c option
#if LifeKeeper is already running
if [ "$option" == "c" -o "$option" == "u" -a "$MESSAGELOG_STATUS" == "0" ];then
	checklkstatus
fi

#single instance handler defined
trap check_lockfile_status EXIT
if [ "$option" == "c" -o "$option" == "u" ];then
	if [ -f $LOCKFILE ];then
                logmsg "$Lksupport_E15"
                export REMOVE_LOCKFILE=1
		if [ "$MESSAGELOG_STATUS" == "0" ]; then
			exit 1
		else
			exit 3
		fi
	fi
        $ECHO "$option" > $LOCKFILE
fi

#trap handler defined
trap "trap_handler" 2
if [ "$option" != "r" -a "$option" != "v" ]; then
ERR_MESSAGE=1
logmsg "$Lksupport_I01"
fi


#############################################################################
#Method to check the LifeKeeper configuration status for HDLM
#
#The new option -v added to handle various Install/Uninstall and Upgrade senarios
#Lksupport -v exit values are as follows:  1.If LifeKeeper is stoped exit value is 0.
#		            	           2.If LifeKeeper is running exit value is 1.
#					   3.If lktest script is absent or it does not have execute permissions
#                                            then return value is 2.

#Modifcation of return values for dlmlksupport -u option:
#return value 0: Normal exit Uninstallatoin/Upgradation would proceed. 
#retrun value 1: The Uninstallation/Upgradation would fail at this return value. 
#retrun value 2: The Upgradation would proceed irrespective of error. 
#return value 3: If Normal Uninstall exit.
#		 If Install section of Upgrade exit.
#		 If Uninstall section of Upgrade proceed.
hdlm_configuration_status() {

        if [ -f $LOCKFILE ];then
                logmsg "$Lksupport_E15"
                export REMOVE_LOCKFILE=1
		if [ "$MESSAGELOG_STATUS" == "0" ]; then
			exit 1
		else
			exit 3
		fi
	fi
	$ECHO "$option" > $LOCKFILE
	export REMOVE_LOCKFILE=0
ERR_MESSAGE=1
logmsg "$Lksupport_I01"
	lksupport_status
	if [ $PROCESSING -eq 0 ]; then
	checklkstatus > /dev/null 2>&1
	fi
}

##############################################################################
# method to take backup of files
backup(){
	
		$CP -pf $1 $2 > /dev/null 2>&1
		ERR=$?
		if [ $ERR -ne 0 ]
		then
			# backup failed
			logmsg "$Lksupport_E05" "$1," " $ERRCODE = $ERR"
			$RM -rf $BACKUP_DIR > /dev/null 2>&1
			if [ "$MESSAGELOG_STATUS" == "0" ]; then
				exit 1
			else
				exit -1
			fi
		fi	

}

##############################################################################

##############################################################################
# method to configure the device_pattern files for HDLM

configptrn(){
	$GREP "^\/dev\/sddlm\[a-p\]\[a-p\]$" $1 > /dev/null 2>&1
	if [ $? -ne 0 ]
	then

		# if sd then replace by sddlm

		$SED "/^\/dev\/sd\[a-z\]$/d" $1 > $DEVPTRN_TMP1$$ 2> /dev/null
		ERR=$?
		#check for success
		#if [ $? -ne 0 ]
		if [ $ERR -ne 0 ]
		then
			# could not make required changes
			$RM -f $DEVPTRN_TMP1$$ > /dev/null 2>&1
			logmsg "$Lksupport_E17" "$1," " $ERRCODE = $ERR"
			unconfigure
			if [ "$MESSAGELOG_STATUS" == "0" ]; then
				exit 1
			else
				exit -1
			fi
		fi
		$SED "/^\/dev\/sd\[a-z\]\[a-z\]/s/sd\[a-z\]\[a-z\]/sddlm\[a-p\]\[a-p\]/" $DEVPTRN_TMP1$$ > $DEVPTRN_TMP$$ 2> /dev/null
		ERR=$?
		if [ $ERR -ne 0 ]
		then
			# could not make required changes
			$RM -f $DEVPTRN_TMP$$ > /dev/null 2>&1
			$RM -f $DEVPTRN_TMP1$$ > /dev/null 2>&1
			logmsg "$Lksupport_E17" "$1," " $ERRCODE = $ERR"
			unconfigure
			if [ "$MESSAGELOG_STATUS" == "0" ]; then
				exit 1
			else
				exit -1
			fi
		else
			$CHMOD --reference=$1 $DEVPTRN_TMP$$ > /dev/null 2>&1
			$MV -f $DEVPTRN_TMP$$ $1 > /dev/null 2>&1
			$RM -f $DEVPTRN_TMP1$$ > /dev/null 2>&1
		fi
	else
		logmsg "$Lksupport_I03" "$1"
	fi
}

##############################################################################
# Method to add entries to lk_cspec

addentrylkscript(){
	$ECHO "$2    $COMMENT_STRING" >> $LKSCRIPT_TMP$$ 2> /dev/null
	ERR=$?
	if [ $ERR -ne 0 ]
	then
		# could not make required changes

		$RM -f $LKSCRIPT_TMP$$ > /dev/null 2 >&1

		logmsg "$Lksupport_E17" "$1," " $ERRCODE = $ERR"

		unconfigure
		if [ "$MESSAGELOG_STATUS" == "0" ]; then
			exit 1
		else
			exit -1
		fi
	fi
}

##############################################################################
# method to configure the lk_cspec file for HDLM

configlkscript(){
	# check the entry in $LKSCRIPT
	$GREP "^if( -x \"$DISK_MAPPING_SH\" ) {    $COMMENT_STRING$" $1 > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		#take backup of original file
		$CP -pf $1 $LKSCRIPT_TMP$$ > /dev/null 2>&1
		# add new entry in script
		addentrylkscript $1 "if( -x \"$DISK_MAPPING_SH\" ) {"
		addentrylkscript $1 "	 system(\"$DISK_MAPPING_SH\", \"-r\");"
		addentrylkscript $1 "	 if( \"\$?\" != 0 ) {"
		addentrylkscript $1 "	     system(\"$LOGGER\",\"-t\", \"$SYSLOG_TAG\", \"$Lksupport_E18\");"
		addentrylkscript $1 "	     system(\"$ECHO\", \"$SYSLOG_TAG:\", \"$Lksupport_E18\");"
		addentrylkscript $1 "	 } else {"
		addentrylkscript $1 "	 }"
		addentrylkscript $1 "} else {"
		addentrylkscript $1 "	 system(\"$LOGGER\",\"-t\", \"$SYSLOG_TAG\", \"$Lksupport_E14 $DISK_MAPPING_SH\");"
		addentrylkscript $1 "	 system(\"$ECHO\", \"$SYSLOG_TAG:\", \"$Lksupport_E14 $DISK_MAPPING_SH\");"
		addentrylkscript $1 "}"
		$CHMOD --reference=$1 $LKSCRIPT_TMP$$ > /dev/null 2>&1
		$MV -f $LKSCRIPT_TMP$$ $1 > /dev/null 2>&1
	else
		logmsg "$Lksupport_I03" "$1"
	fi
}

##############################################################################
# method to configure Lifekeeper for HDLM

configure(){
		lksupport_status
		if [ $PROCESSING -eq 0 ]; then
			exit 0
		fi
		
		#check if the backup directory exists
		if [ ! -d $BACKUP_DIR ]
		then
			# create directory if it does not exist
			$MKDIR $BACKUP_DIR > /dev/null 2>&1
			ERR=$?
			DIR_CREATE=1
			if [ $ERR -ne 0 ]
			then
				logmsg "$Lksupport_E07" "$BACKUP_DIR, $ERRCODE = $ERR"
				if [ "$MESSAGELOG_STATUS" == "0" ]; then
					exit 1
				else
					exit -1
				fi
			fi
		fi
		if [ "$option" == "c" ]; then 
			$GREP "^\/dev\/sd\[a-z\]$" $DEVPTRN_DSK > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				backup $DEVPTRN_DSK $BACKUP_DEVPTRN_DSK
			else
				logmsg "$Lksupport_E16" "$DEVPTRN_DSK"
				IS_EMPTY=`$LS $BACKUP_DIR |$WC -l`
				if [ $IS_EMPTY -eq 0 ]
				then
					$RM -rf $BACKUP_DIR > /dev/null 2>&1
				fi
				if [ $DIR_CREATE -eq 1 ]
				then
					$RM -rf $BACKUP_DIR > /dev/null 2>&1
				fi 
				if [ "$MESSAGELOG_STATUS" == "0" ]; then
					exit 1
				else
					exit -1
				fi
			fi
			$GREP "^\/dev\/sd\[a-z\]$" $DEVPTRN_HST > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				backup $DEVPTRN_HST $BACKUP_DEVPTRN_HST
			else
				logmsg "$Lksupport_E16" "$DEVPTRN_HST"
				$GREP "^\/dev\/sddlm\[a-p\]\[a-p\]$" $DEVPTRN_HST > /dev/null 2>&1
				if [ $? -ne 0 ]
				then
					$RM -rf $BACKUP_DIR > /dev/null 2>&1
				fi
				
				if [ $DIR_CREATE -eq 1 ]
				then
					$RM -rf $BACKUP_DIR > /dev/null 2>&1
				fi 

				if [ "$MESSAGELOG_STATUS" == "0" ]; then
					exit 1
				else
					exit -1
				fi
			fi
			$GREP "^if( -x \"$DISK_MAPPING_SH\" ) {    $COMMENT_STRING$" $LKSCRIPT > /dev/null 2>&1
			if [ $? -ne 0 ]
			then
				backup $LKSCRIPT $BACKUP_LKSCRIPT
			else
				if [ $DIR_CREATE -eq 1 ]
				then
				$RM -rf $BACKUP_DIR > /dev/null 2>&1
				fi 
			fi
			
		fi	
		configptrn $DEVPTRN_DSK
		configptrn $DEVPTRN_HST
		configlkscript $LKSCRIPT
}

##############################################################################
# method to unconfigure the device_pattern files

unconfigptrn(){
	# check the entries in pattern file
	$GREP "^\/dev\/sddlm\[a-p\]\[a-p\]$" $1 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		# check Lifekeeper is not running. If it is,throw error.
		checklkstatus

		# replace sddlm by sd
		$SED "/^\/dev\/sddlm\[a-p\]\[a-p\]/s/sddlm\[a-p\]\[a-p\]/sd\[a-z\]\\n\/dev\/sd\[a-z\]\[a-z\]/" $1 > $DEVPTRN_TMP$$ 2> /dev/null
		ERR=$?
		if [ $ERR -ne 0 ]
		then
			# failed to restore
			$RM -f $DEVPTRN_TMP$$ > /dev/null 2>&1
			logmsg "$Lksupport_W04" "$1," " $ERRCODE=$ERR"
			if [ $1 = $DEVPTRN_DSK ]
			then
				if [ "$CURR_LANG" == "$JAP_LANG" ]; then
					$LOGGER -t $SYSLOG_TAG "$Lksupport_I10_ID" "$BACKUP_DEVPTRN_DSK" "$Lksupport_I10" "$DEVPTRN_DSK" "$I10_REMAINING"
					$ECHO "$Lksupport_I10_ID" "$BACKUP_DEVPTRN_DSK" "$Lksupport_I10" "$DEVPTRN_DSK" "$I10_REMAINING"
				else
					logmsg "$Lksupport_I10" "$BACKUP_DEVPTRN_DSK " "$I10_REMAINING" "$DEVPTRN_DSK"
				fi
			else				
				if [ "$CURR_LANG" == "$JAP_LANG" ]; then
					$LOGGER -t $SYSLOG_TAG "$Lksupport_I10_ID" "$BACKUP_DEVPTRN_HST" "$Lksupport_I10" "$DEVPTRN_HST" "$I10_REMAINING"
					$ECHO "$Lksupport_I10_ID" "$BACKUP_DEVPTRN_HST" "$Lksupport_I10" "$DEVPTRN_HST" "$I10_REMAINING"
				else	
					logmsg "$Lksupport_I10" "$BACKUP_DEVPTRN_HST " "$I10_REMAINING" "$DEVPTRN_HST"
				fi
			fi
			UNCONFIG_ERROR=$TRUE
		else

			$CHMOD --reference=$1 $DEVPTRN_TMP$$ > /dev/null 2>&1
			$MV -f $DEVPTRN_TMP$$ $1 > /dev/null 2>&1
		fi
	fi
}

##############################################################################
# method to unconfigure the lk_cspec file

unconfiglkscript(){

	# check the entries in lk_cspec file
	$GREP "^if( -x \"$DISK_MAPPING_SH\" ) {    $COMMENT_STRING$" $1> /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		# check Lifekeeper is not running. If it is,throw error.
		checklkstatus

		# delete the entries

		$SED "/$COMMENT_STRING$/d" $1 > $LKSCRIPT_TMP$$ 2> /dev/null
		ERR=$?
		if [ $ERR -ne 0 ]
		then
			# failed to restore

			$RM -f $LKSCRIPT_TMP$$ > /dev/null 2>&1
			logmsg "$Lksupport_W04" "$1," " $ERRCODE=$ERR"
			if [ "$CURR_LANG" == "$JAP_LANG" ]; then
			$LOGGER -t $SYSLOG_TAG  "$Lksupport_I10_ID" "$BACKUP_LKSCRIPT " "$Lksupport_I10" "$LKSCRIPT" "$I10_REMAINING"
			$ECHO "$Lksupport_I10_ID" "$BACKUP_LKSCRIPT " "$Lksupport_I10" "$LKSCRIPT" "$I10_REMAINING"
			else
			logmsg "$Lksupport_I10" "$BACKUP_LKSCRIPT " "$I10_REMAINING" "$LKSCRIPT"
			fi
			UNCONFIG_ERROR=$TRUE
		else
			$CHMOD --reference=$1 $LKSCRIPT_TMP$$ > /dev/null 2>&1
			$MV -f $LKSCRIPT_TMP$$ $1 > /dev/null 2>&1
		fi
	fi
}

##############################################################################
# method to unconfigure Lifekeeper for HDLM

unconfigure(){

	# check if the files exist
	checklcd
		unconfigptrn $DEVPTRN_DSK
		unconfigptrn $DEVPTRN_HST
		unconfiglkscript $LKSCRIPT
		# remove backup
		if [ -d $BACKUP_DIR ]
		then
			if [ "$UNCONFIG_ERROR" != "$TRUE" ]
			then
				$RM -rf $BACKUP_DIR > /dev/null 2>&1
				ERR=$?
				if [ $ERR -ne 0 ]
				then
					logmsg "$Lksupport_W01" "$BACKUP_DIR, $ERRCODE = $ERR"
				fi
			fi
		else
			logmsg "$Lksupport_I07" "$BACKUP_DIR"
		fi
}

##############################################################################
# method to configure the disk.mapping files

disk_mapping_config(){

	$CAT /proc/$PPID/cmdline | $GREP $LKSCRIPT > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		logmsg "$Lksupport_E00" "-$option"
		$ECHO "$Lksupport_W00"
		if [ "$MESSAGELOG_STATUS" == "0" ]; then
			exit 1
		else
			exit -1
		fi
	fi

        if [ -f $LOCKFILE ];then
		LOCKFILE_DATA=`$CAT $LOCKFILE`
                export REMOVE_LOCKFILE=1
		if [ "$LOCKFILE_DATA" != "r" ]; then
			logmsg "$Lksupport_E15"
			exit 1
		else
			exit 0
		fi
	fi

	$ECHO "$option" > $LOCKFILE
	export REMOVE_LOCKFILE=0
	ERR_MESSAGE=1
	logmsg "$Lksupport_I01"
	if [ -d $DISKMAPPING ]
	then
		cd $DISKMAPPING
		$LS $DISKMAPPING/sddlm* > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			logmsg "$Lksupport_I09"
			exit 0
			# exit 0 is done so lk_cspec does not throw an error
			# saying that lksupport has failed
                else
               	  if [ -d $DISKMAPPING_BAK ]; then
               	  	$RM -rf $DISKMAPPING_BAK
                  fi

		  if [ ! -d $BACKUP_DIR ]; then
		  	export BACKUP_ABSENT=1
		  else
		  	if [ ! -d $TMP_DIR ]; then
		  	      export BACKUP_ABSENT=2
		  	fi       
 	          fi	  	
			  $MKDIR -p $DISKMAPPING_BAK > /dev/null 2>&1
			  ERR=$?
			  if [ $ERR -eq 0 ]; then
				case $BACKUP_ABSENT in
	 			1)
				 export CLEANUP=2
				 #delete backup dir
				;;
	 			2)
				 export CLEANUP=1
				 #delete tmp dir
				;;
	 			*)
		 		 export CLEANUP=3
		 		 #delete disk.mapping dir
				;;
	 			esac

				 $CP -f sddlm* $DISKMAPPING_BAK > /dev/null 2>&1
				 ERR=$?
				 if [ $ERR -ne 0 ]; then
		                   logmsg "$Lksupport_E05" "$DISKMAPPING," " $ERRCODE = $ERR" > /dev/null 2>&1
				   if [ "$MESSAGELOG_STATUS" == "0" ]; then
					exit 1
				   else
					exit -1
				   fi
			         fi

                          else
		          logmsg "$Lksupport_E07" "$DISKMAPPING_BAK, $ERRCODE = $ERR"> /dev/null 2>&1
                             if [ "$MESSAGELOG_STATUS" == "0" ]; then
					exit 1
			     else
					exit -1
			     fi
		          fi

		fi 

	else
		logmsg "$Lksupport_E04" "$DISKMAPPING"
		if [ "$MESSAGELOG_STATUS" == "0" ]; then
			exit 1
		else
			exit -1
		fi
	fi

	# for all sddlm* files in disk.mapping
	for HDLMFILE in `$LS sddlm*`
	do
		SG_TEMP=`$ECHO $HDLMFILE | $CUT -c6-` > /dev/null 2>&1
		SGDLM_DEVICE=/dev/sgdlm$SG_TEMP

		if [ ! -c $SGDLM_DEVICE ]
		then
			logmsg "$Lksupport_E10" "$SGDLM_DEVICE"
			if [ "$MESSAGELOG_STATUS" == "0" ]; then
				exit 1
			else
				exit -1
			fi
		fi
	done


	#for all sddlm files in disk.mapping
	for HDLMFILES in `$LS sddlm*`
	do
		#check the entries
		$GREP "^\/dev\/sgdlm\[a-p\]\[a-p\]$" $HDLMFILES > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			SG_TEMP=`$ECHO $HDLMFILES | $CUT -c6-` > /dev/null 2>&1
			SGDLM_DEVICE=/dev/sgdlm$SG_TEMP

			# over-write sg entries with sgdlm
			$ECHO $SGDLM_DEVICE > $HDLMFILES 2> /dev/null
			ERR=$?
			if [ $ERR -ne 0 ]
			then
				logmsg "$Lksupport_E17" "$HDLMFILES," " $ERRCODE = $ERR"
			fi
		else
			logmsg "$Lksupport_I03" "$HDLMFILES"
		fi

	done
}

##############################################################################
# main

case $option in
 c)
	
	configure
	;;
 u)
	
	unconfigure
	;;
 r)
	disk_mapping_config
	;;
 v)    	
	hdlm_configuration_status
	;;
 *)
	;;
esac
exit 0
