#!/bin/sh

#ident "$script: DLMManager Build 5.91.0.1 07/03/08 12:30:00 $"

#----------------------------------------------------------------------------#
# PRODUCT :  HDLM Driver 0501                                                #
#----------------------------------------------------------------------------#
# NAME :     DLMManager                                                      #
# FUNCTION : To start dlmmanager                            #
# INSTANCE :                                                                 #
# MEMO :                                                                     #
# DATE :     2003/04/24                                                      #
# AUTHOR :   Bharvi Oak                                          	     #
# HISTORY :                                                                  #
#                                                                            #
#@ All Rights Reserved, Copyright (C) 2003,2007 Hitachi, Ltd.	             #
#----------------------------------------------------------------------------#

# REVAMP ACTIVITY PERFORMED. SO ALL PREVIOUS FDOCS & COMMENTS ARE REMOVED. PLEASE LOOKUP 
# PREVIOUS VERSIONS CODES FOR REFERENCE.

###############################################################################
#                                                                             #
#                        DLMManager Global defines start                      #
#                                                                             #
###############################################################################

ROOT="/"
cd $ROOT

# Source function library.
export NOLOCALE=1
HDLM_COMMON_KNL_CHK="/etc/sysconfig/hdlm-scripts/dlmcommonknlcheck"
TMP_DIR="/etc/opt/DynamicLinkManager"
CMD_ECHO="/bin/echo"
CMD_RM="/bin/rm"
CMD_TOUCH="/bin/touch"
CMD_UNAME="/bin/uname"
CMD_GAWK="/bin/gawk"
CMD_WC="/usr/bin/wc"
CMD_GREP="/bin/grep"
KERNEL_2_4=0
KERNEL_2_6=0

# DLMManager single instance Add Start 
export LOCKFILE="$TMP_DIR/.DLMManager.lock"
export REMOVE_LOCKFILE=0
# DLMManager single instance Add end 

VERSION=`$CMD_UNAME -r | $CMD_GAWK -F "." '{print $2 }'`
if [ $VERSION -eq 4 ] ; then
	KERNEL_2_4=1
else
	KERNEL_2_6=1
fi

# DLMManager mesaages
SUPPORT_JAP=$LANG
if [ "$SUPPORT_JAP" == "ja_JP.eucjp" ] || [ "$SUPPORT_JAP" == "ja_JP.eucJP" ] ; then
	DLMManagerid1="KAPL12901-E 󥹥ȡ뤵ƤHDLMбƤ륫ͥΥСȡưƤ륫ͥΥС󤬰פޤ"
	DLMManagerid2="KAPL12902-E HDLMΥե뤬Ǥե̾ = "
elif [ "$SUPPORT_JAP" == "ja_JP.UTF-8" -a $KERNEL_2_6 -eq 1 -a -f /etc/redhat-release ]; then
	DLMManagerid1="KAPL12901-E インストールされているHDLMが対応しているカーネルのバージョンと，現在動作しているカーネルのバージョンが一致しません。"
	DLMManagerid2="KAPL12902-E HDLMのファイルが不正です。ファイル名 = "
    else
	DLMManagerid1="KAPL12901-E The version of the kernel supported by the installed HDLM does not match the currently running kernel version."
	DLMManagerid2="KAPL12902-E The HDLM file is invalid. File name = "
fi



###############################################################################
#                                                                             #
#                        DLMManager Global defines end                        #
#                                                                             #
###############################################################################

###########################################################################
#Kernel Version Agreement Check
#For matching kernel version-START#
if [ "$1" != "status" -a "$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
				$CMD_ECHO $DLMManagerid1
			else
				$CMD_ECHO $DLMManagerid2 "$HDLM_COMMON_KNL_VER_FILE"
			fi
			if [ -f /etc/SuSE-release ]; then
				. /etc/rc.status
				rc_reset
				if [ "$1" == "start" ]
				then
					$CMD_ECHO -n $"Starting DLMManager: "
				else
					$CMD_ECHO -n $"Stopping DLMManager: "
				fi

			# Send the Failure notice 
				rc_failed 1
				rc_status -v
				rc_exit
			elif [ -f /etc/redhat-release ]; then
				. /etc/init.d/functions	
				if [ "$1" == "start" ]
				then
					$CMD_ECHO -n $"Starting DLMManager: "
				else
					$CMD_ECHO -n $"Stopping DLMManager: "
				fi
			# Send the Failure notice 
				failure 1
				$CMD_ECHO
				exit 1
			fi
		fi
	else
		$CMD_ECHO $DLMManagerid2 "$HDLM_COMMON_KNL_CHK"
		if [ -f /etc/SuSE-release ]; then
			. /etc/rc.status
			rc_reset
			if [ "$1" == "start" ]
			then
				$CMD_ECHO -n $"Starting DLMManager: "
			else
				$CMD_ECHO -n $"Stopping DLMManager: "
			fi
			rc_failed 1
			rc_status -v
			rc_exit
		elif [ -f /etc/redhat-release ]; then
			. /etc/init.d/functions	
			if [ "$1" == "start" ]
			then
				$CMD_ECHO -n $"Starting DLMManager: "
			else
				$CMD_ECHO -n $"Stopping DLMManager: "
			fi
			failure 1
			$CMD_ECHO
			exit 1
		fi
	fi
fi
#For matching kernel version-END#
##########################################################################

DlmManagerstoptimeoutMax=999999
DlmManagerstoptimeoutMin=5

#Set Default Value for timeout
DlmmgrDefaultstoptimeoutval=$DlmManagerstoptimeoutMax

if [ "$1" == "stop" ];then
        if [ "$2" == "-t" ] && [ $# == 3 ] ;then
        	if [ `$CMD_ECHO "$3" | $CMD_GREP -x "[0-9]*"` ]; then
				PARAM_LENGTH=`$CMD_ECHO "$3" | $CMD_WC -c`
				if [ $PARAM_LENGTH -le 7 ];then
                            DlmmgrDefaultstoptimeoutval=$3
                        	if [ $3 -lt $DlmManagerstoptimeoutMin ];then
                                	DlmmgrDefaultstoptimeoutval=$DlmManagerstoptimeoutMin
                        	elif [ $3 -gt $DlmManagerstoptimeoutMax ];then
                                	DlmmgrDefaultstoptimeoutval=$DlmManagerstoptimeoutMax
                        	fi
				fi
            fi
        fi
fi


###############################################################################
#                                                                             #
#                        DLMManager TMP_DIR check start                       #
#                                                                             #
###############################################################################

# Check existance of $TMP_DIR Add Start

# Check whether the /etc/opt/DynamicLinManager directory is present or not.

if [ ! -d $TMP_DIR ] && [ "$1" == "start" -o "$1" == "stop" -o "$1" == "shutdown" ]; then
	if [ -f /etc/SuSE-release ]; then
		. /etc/rc.status
		rc_reset
		if [ "$1" == "start" ]
		then
			$CMD_ECHO -n $"Starting DLMManager: "
		else
			$CMD_ECHO -n $"Stopping DLMManager: "
		fi

        # Send the Failure notice 
		rc_failed 1
		rc_status -v
		rc_exit
	elif [ -f /etc/redhat-release ]; then
		. /etc/init.d/functions	
		if [ "$1" == "start" ]
		then
			$CMD_ECHO -n $"Starting DLMManager: "
		else
			$CMD_ECHO -n $"Stopping DLMManager: "
		fi
        # Send the Failure notice 
		failure 1
		$CMD_ECHO
		exit 1
	fi
fi
# Check existance of $TMP_DIR Add End 

###############################################################################
#                                                                             #
#                        DLMManager TMP_DIR check end                         #
#                                                                             #
###############################################################################

###############################################################################
#                                                                             #
#                        DLMManager single instance start                     #
#                                                                             #
###############################################################################

# DLMManager single instance Add Start
check_lockfile_status() {
    if [ "$REMOVE_LOCKFILE" -eq 0 ]
    then
        $CMD_RM -f $LOCKFILE
    fi
}
if [ "$1" == "start" -o "$1" == "stop" -o "$1" == "shutdown" ]
then
    trap check_lockfile_status EXIT
    if [ -f $LOCKFILE ]
    then
            $CMD_ECHO "Another instance of DLMManager script is running"
            export REMOVE_LOCKFILE=1
            exit 1
    fi
    $CMD_TOUCH $LOCKFILE
fi
# DLMManager single instance Add end
###############################################################################
#                                                                             #
#                        DLMManager single instance end                       #
#                                                                             #
###############################################################################


###############################################################################
#                                                                             #
#                        DLMManager main start                                #
#                                                                             #
###############################################################################

###############################################################################
#                                                                             #
#                        DLMManager SuSE script start                         #
#                                                                             #
###############################################################################
#DISTRIBUTION SPECIFIC

if [ -f /etc/SuSE-release ]; then
    . /etc/rc.status
    # Reset status of this service
    rc_reset

    # See how we were called.
    case "$1" in

            start)
	        export NLSPATH="/opt/DynamicLinkManager/rc/%L/%N"
                $CMD_ECHO -n $"Starting DLMManager: "
	        /opt/DynamicLinkManager/bin/dlmmgr > /dev/null 2>&1 && rc_status || rc_failed 
                rc_status -v
	    ;;
            
	    stop)
		$CMD_ECHO -n $"Stopping DLMManager:"
		killproc dlmmgr -15  -t  $DlmmgrDefaultstoptimeoutval > /dev/null 2>&1 && rc_status || rc_failed  
                rc_status -v
	    ;;

            shutdown)
	        $CMD_ECHO -n $"Stopping DLMManager:"
	        killproc dlmmgr -15 > /dev/null 2>&1 && rc_status || rc_failed  
                rc_status -v
	    ;;

            status)
	        $CMD_ECHO -n $"dlmmgr is: "
	        checkproc /opt/DynamicLinkManager/bin/dlmmgr
	        rc_status -v  
	    ;;

            *)
	            $CMD_ECHO $"Usage: $0 {start|stop|status}"
	            exit 1
    esac
    rc_exit
    ###############################################################################
    #                                                                             #
    #                        DLMManager SuSE script end                           #
    #                                                                             #
    ###############################################################################

    ###############################################################################
    #                                                                             #
    #                        DLMManager RedHat script start                       #
    #                                                                             #
    ###############################################################################

elif [ -f /etc/redhat-release ]; then
    . /etc/init.d/functions

    # See how we were called.
    case "$1" in
            start)
                export NLSPATH="/opt/DynamicLinkManager/rc/%L/%N"
                $CMD_ECHO -n $"Starting DLMManager: "
                /opt/DynamicLinkManager/bin/dlmmgr > /dev/null 2>&1 && success $"Starting DLMManager: " || failure $"Starting DLMManager: "
                RETVAL=$?
                $CMD_ECHO
                exit $RETVAL
            ;;

	   stop)
                $CMD_ECHO -n $"Stopping DLMManager: "
                killproc dlmmgr -15 > /dev/null 2>&1 && success $"Stopping DLMManager: " || failure $"Stopping DLMManager: "
                RETVAL=$?

		PartSec=1		
		pid1=`pidof dlmmgr`
		while [ ! -z $pid1 ] && [ "$DlmmgrDefaultstoptimeoutval" -gt 0 ]
		do
		{
			sleep $PartSec
			DlmmgrDefaultstoptimeoutval=`expr $DlmmgrDefaultstoptimeoutval - $PartSec`			
		 	pid1=`pidof dlmmgr`			
                }
		done
		$CMD_ECHO
                exit $RETVAL
            ;;

	    shutdown)
	          $CMD_ECHO -n $" Stopping DLMManager: "
                  killproc dlmmgr -15 > /dev/null 2>&1 && success $"Stopping DLMManager: " || failure $"Stopping DLMManager: "
                  RETVAL=$?
         	  $CMD_ECHO
                  exit $RETVAL
            ;;
			
            status)
                status dlmmgr
            ;;

            *)
                $CMD_ECHO $"Usage: $0 {start|stop|status}"
                exit 1
    esac
fi

#DISTRIBUTION SPECIFIC
###############################################################################
#                                                                             #
#                        DLMManager RedHat script end                         #
#                                                                             #
###############################################################################


###############################################################################
#                                                                             #
#                        DLMManager main end                                  #
#                                                                             #
###############################################################################
