#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,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/rg_move_fence.sh 1.11 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2008 
# 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/rg_move_fence.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           rg_move_fence                                   #
#                                                                       #
#       Description:    This event script is called when a resource     #
#                       group needs to move.                            #
#                       This script calls the process_resources event   #
#                       script with the "ACQUIRE" argument.             #
#                       This will process any needed fencing for the    #
#                       rg_move.                                        #
#                                                                       #
#       Called by:      cluster manager                                 #
#                                                                       #
#       Calls to:       process_resource                                #
#                                                                       #
#       Arguments:      nodename rg_ID                                  #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

export PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../utilities/cl_get_path all)"
export PATH
eval export $(cllsparam -x)

[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.11'
export NODENAME=$1

set -u

if [ $# != 2 ]
then 
  cl_echo 6163 "Usage: $PROGNAME nodename RG_id\n" $PROGNAME
  exit 2
fi

set +u

if [[ -z "$RG_DEPENDENCIES" ]]
then
    RG_DEPENDENCIES="FALSE"
fi

if [[ "$RG_DEPENDENCIES" = "TRUE" ]]
then
    LOCAL_NODENAME=$LOCALNODENAME
    eval RGNAME=$(odmget -q"id=$2" HACMPgroup | egrep "group =" | awk '{print $3}')
    GROUPNAME="$RGNAME"
    group_state=\$RESGRP_${RGNAME}_${LOCAL_NODENAME//-/$HA_DASH_CHAR} 
    set +u
    export RG_MOVE_ONLINE=$(eval print $group_state)
    set -u
    RG_MOVE_ONLINE=${RG_MOVE_ONLINE:-TMP_ERROR}

    set -a
    clsetenvgrp_output=$(clsetenvgrp $LOCAL_NODENAME "rg_move" $RGNAME "")
    RC=$?
    eval "$clsetenvgrp_output"
    set +a
    if [ $RC -ne 0 ]
    then
    STATUS=1
    fi
fi


process_resources "FENCE"
: exit status of process_resources "FENCE" is: $?


if [[ "$RG_DEPENDENCIES" = "TRUE" ]]
then
    export EVENT_TYPE;
    echo "$EVENT_TYPE"
    if [[ -n "$SIBLING_RELEASING_GROUPS" ]]
    then
    #
    # Call the leave_cleanup replicated resource method if defined
    #
    for siblingGroup in $SIBLING_RELEASING_GROUPS
    do
	    set -a
	    eval $(clsetrepenv $siblingGroup)
	    set +a
	    export GROUPNAME=$siblingGroup

	    METHODS=$(cl_rrmethods2call leave_cleanup)
    
	    for method in $METHODS
	    do
	        if [[ -x $method ]]
	        then
		        if ! $method $PROGNAME $NODENAME
		        then
		            STATUS=1
		        fi
	        fi
	    done
    done
    fi
fi



exit $?
