#!/bin/ksh
#  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_server.sh 1.4.4.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2011 
# 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_server.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

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

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

# Including Availability metrics library file
. /usr/es/lib/ksh93/availability/cl_amlib

if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='%I%'
fi

STATUS=0

# Usage: stop_server [server-names...]

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

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
ALLSERVERS="All_servers"
if [ "$EMULATE" = "EMUL" ]
then
    cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
    echo "cl_RMupdate resource_releasing $ALLSERVERS $PROGNAME \n"
else
    cl_RMupdate resource_releasing $ALLSERVERS $PROGNAME
fi


### 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 server in $*
do
    #
    # First get the stop server script.
    # Then prepare the pattern which will be deleted from an app. server
    # status file to keep track of the running server.
    #
    STOP=$(cllsserv -cn $server | cut -d':' -f3)
    STOP_SCRIPT=$(echo $STOP | cut -d' ' -f1)

    PATTERN="$LOCALNODENAME $server"

    #
    # 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
        if [ "$EMULATE" = "EMUL" ]
        then
            cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
            echo "ODMDIR=/etc/es/objrepos $STOP\n"
	else
            # Format for consumption by cl_am utility
            amlog_trace $AM_APP_STOP_BEGIN "Stopping application controller|$server"
            ODMDIR=/etc/objrepos ${EXEC} ${STOP}
            rc=$?
	fi
        if (( rc != 0 ))
        then
            cl_log 312 "Failed to stop $server." $server
            amlog_err $AM_APP_STOP_FAILURE "Stopping application controller|$server"
            STATUS=1
            # update the resource manager with the results
            cl_RMupdate resource_error $server $PROGNAME
        else
            amlog_trace $AM_APP_STOP_END "Stopping application controller|$server" 
        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
	    if [ "$EMULATE" = "EMUL" ]
	    then
		    cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
		    echo "cl_RMupdate resource_error $server $PROGNAME \n"
	    else
            cl_RMupdate resource_error $server $PROGNAME
	    fi
    fi
done


# update the resource manager with the results
ALLNOERRSERV="All_nonerror_servers"
if [ "$EMULATE" = "EMUL" ]
then
    cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
    echo "cl_RMupdate resource_down $ALLNOERRSERV $PROGNAME \n"
else
    cl_RMupdate resource_down $ALLNOERRSERV $PROGNAME
fi

exit $STATUS

