#!/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_r721 src/43haes/usr/sbin/cluster/events/node_up_local_complete.sh 1.2.9.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2016 
# 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/node_up_local_complete.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           node_up_local_complete                          #
#                                                                       #
#	Description:	This script is called when the node_up_local	#
#			script successfully completes.			#
#			It starts the application servers.		#
#									#
#       Called by:      node_up_complete, reconfig_resource_complete,   #
#                       rg_move_complete                                #
#                                                                       #
#       Calls to:       start_server					#
#                                                                       #
#       Arguments:      None                                            #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

#########################################################################
# 
# Set status of resource in resource location DB.  Note that we only set this
# if NFS_resource is "FALSE" or nonexistent,  because this means that we 
# are actually taking the resource. 
#
#########################################################################
set_resource_status()
{
    typeset PS4_FUNC="set_resource_status"
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    set +u
    eval TEMPNFS=\$"NFS_$GROUPNAME"
    set -u

    if [ "$TEMPNFS" = "" -o "$TEMPNFS" = "FALSE" ]
    then
	RGSTATE=$(clRGinfo -s $GROUPNAME | awk -F: '{ if ($3 =="'$LOCALNODENAME'") print $2 }')
    if [ "$1" = "ERROR" -o "$RGSTATE" = "ACQUIRING" -o "$FOLLOWER_ACTION" = "ACQUIRE_SECONDARY" ]
	then
	    if [ "$EMULATE" = "EMUL" ]
	    then
		cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
		echo	"clchdaemons -d clstrmgr_scripts -t resource_locator -n \""$LOCALNODENAME"\" -o \""$GROUPNAME"\" -v \"$1\"\n"
	    else
		clchdaemons -d clstrmgr_scripts -t resource_locator -n "$LOCALNODENAME" -o "$GROUPNAME" -v "$1"
		if [ $? -ne 0 ]
		then
			cl_log 655 "$PROGNAME: Problem with resource location database in HACMPdaemons ODM." $PROGNAME
			STATUS=1
		fi
	    fi
            #
            # Resource Manager Updates
            #
            if [ "$1" = "UP" ]
            then
                if [ "$PRINCIPAL_ACTION" = "ACQUIRE" ]
                then
                    cl_RMupdate rg_up $GROUPNAME $PROGNAME
                    [ $? -ne 0 ] && STATUS=1
                elif [ "$FOLLOWER_ACTION" = "ACQUIRE_SECONDARY" ]
                then
                    cl_RMupdate rg_up_secondary $GROUPNAME $PROGNAME
                    [ $? -ne 0 ] && STATUS=1
                fi
            else
                if [ "$PRINCIPAL_ACTION" = "ACQUIRE" ]
                then    
                    cl_RMupdate rg_error $GROUPNAME $PROGNAME
                    [ $? -ne 0 ] && STATUS=1
                elif [ "$FOLLOWER_ACTION" = "ACQUIRE_SECONDARY" ]
                then
                    cl_RMupdate rg_error_secondary $GROUPNAME $PROGNAME
                    [ $? -ne 0 ] && STATUS=1
                else
                    cl_RMupdate rg_error $GROUPNAME $PROGNAME
                    [ $? -ne 0 ] && STATUS=1
                fi
            fi
	fi
    fi
}

#############################################################################
#
# Main Starts Here
#
#############################################################################

typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"

if [[ $VERBOSE_LOGGING == "high" ]]; then
    eval export $(cllsparam -x)
    set -x
    version='%I%'
fi

. /usr/es/sbin/cluster/events/reconfig_udresources

STATUS=0

if [ $# -ne 0 ]
then
   cl_log 1035 "Usage: $PROGNAME\n" $PROGNAME
	exit 2
fi

if [ ! -n "$EMULATE" ]
then
        EMULATE="REAL"
fi

if [ -n "$APPLICATIONS" ] 
then

    clmanageroha -o acquire -s -l "${APPLICATIONS// /,}" 3>&2
    if (( $? > 0 )) ; then
        STATUS=2
    else
        if [ "$EMULATE" = "EMUL" ] ; then
	        cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
		    echo "clcallev start_server "$APPLICATIONS"\n"
	    else
		    clcallev start_server "$APPLICATIONS"
                    RC=$?
                    : exit status of start_server $APPLICATIONS is: $RC
		    if [ $RC -ne 0 -a $STATUS -eq 0 ] ; then
                STATUS=2
            fi
        fi
    fi
fi

acquire_udresources AFTER_APPLICATION
RC=$?
: exit status of acquire_udresources is: $RC
if (( $RC != 0 ))
then
   echo "Failed to Start userdefined resources for '${GROUPNAME}' "
   (( $STATUS == 0 )) && STATUS=2
fi

# 
# Set final resource status
#
if [ $STATUS -ne 0 ] ; then
    set_resource_status "ERROR"
else
    set_resource_status "UP"
fi



if [ $STATUS -eq 2 ] ; then
    if [[ -z $CLUSTER_MAJOR || -z $CLUSTER_MINOR ]] ; then
	echo "Resource Group Allocation Failure disabled on $LOCALNODENAME for $GROUPNAME, version not found."
	exit 1  # terminal error, we don't know what version we are running
    fi
    if [ $CLUSTER_MAJOR -gt 44 -o $CLUSTER_MAJOR -eq 44 -a $CLUSTER_MINOR -eq 1 ] ; then
	echo "Resource Group Allocation Failure enabled on $LOCALNODENAME for $GROUPNAME."
	exit 0  # non-terminal exit status for RG acquisition failure
    else
	echo "Resource Group Allocation Failure disabled on $LOCALNODENAME for $GROUPNAME, PowerHA SystemMirror 4.4.0 or earlier version exists."
	exit 1  # terminal error, HACMP 4.4.0 or earlier release
    fi
else
    exit $STATUS
fi