#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/stop_udresource.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/events/stop_udresource.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#												#
#	Name:		stop_udresource							#
#												#
#	Description:	This script is called to stop the userdefined	#
#			resource(s) when a local node leaves the 			#
#			cluster or a remote node joins the cluster.     	#
#												#
#	Called by:	node_down_local							#
#												#
#	Calls to:	None                   						#
#												#
#	Arguments:	[userdefined-resource-names...]				#
#												#
#	Returns:	0	success							#
#			1	failure							#
#												#
#########################################################################

typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='%I%'
fi

STATUS=0

# Usage: stop_udresource [userdefined-resource-names...]
PROC_RES=false

# if JOB_TYPE is set, and it doesn't equal to "GROUP", then
# we are processing for process_resources
if [[ ${JOB_TYPE:-0} != 0 && $JOB_TYPE != "GROUP" ]]; then
   PROC_RES=true
fi

typeset WPARNAME WPARDIR EXEC
WPARDIR=""
EXEC=""

typeset -i rc=0
WPARNAME=$(clwparname $GROUPNAME)
rc=$?

set -u

# update the resource manager with this operation
ALLUDRESOURCES="All_userdefined_resources"
cl_RMupdate resource_releasing $ALLUDRESOURCES $PROGNAME


### Handling for WPAR-enabled RG
if (( $rc == 0 )) && [[ -n ${WPARNAME} ]]
then
    WPARDIR=$(clwparroot ${WPARNAME})
    if [[ $? != 0 ]]
    then
        echo "$0 ERROR: Failed to get the directory for WPAR '${WPARNAME}'"
        exit 1
    fi

    EXEC="clwparexec ${WPARNAME}"
fi

for each_udres in $*
do
    #
    # First get the stop resource script.
    # Then prepare the pattern which will be deleted from an app. server
    # status file to keep track of the running server.
    #
    udres_type=$(cludres -l -n $each_udres -ch | awk ' FS=":" { print $2 }')
    STOP=$(cludrestype -l -n $udres_type| awk ' FS="=" { if( $1 == "STOP_METHOD" ) print $2 }' )
    STOP_SCRIPT=$(echo $STOP | cut -d' ' -f1 | /usr/bin/sed s/\"//g )

    PATTERN="$LOCALNODENAME $each_udres"

    #export resource specific attributes as env variables
    export `cludres -l -n $each_udres`

    #
    # If the script is executable, stop the server,
    # and delete the pattern from the status file.
    # If not executable, echo error message.
    #
    if [[ ( -n ${WPARNAME} && -x ${WPARDIR}/${STOP_SCRIPT} ) || \
          ( -z ${WPARNAME} && -x ${STOP_SCRIPT} ) ]]
    then
	    	ODMDIR=/etc/objrepos ${EXEC} ${STOP} ${each_udres}
        if [ $? -ne 0 ]
        then
                cl_log 312 "Failed to stop $each_udres." $each_udres
                STATUS=1
                # update the resource manager with the results
                cl_RMupdate resource_error $each_udres$PROGNAME
        fi
    else
        cl_log 310 "$STOP does not exist or is not executable." $STOP
        STATUS=1

	    if [[ $PROC_RES = true ]]; then
		STATUS=11  #shows that the resource manager got updated
	    fi	    

            # update the resource manager with the results
            cl_RMupdate resource_error $each_udres$PROGNAME
    fi
done


# update the resource manager with the results
ALLNOERRUDRES="All_nonerror_userdefined_resources"
cl_RMupdate resource_down $ALLNOERRUDRES $PROGNAME

exit $STATUS

