#!/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. 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,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/server_restart_complete.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM 
#########################################################################
#
# Questions ? Comments ? Suggestions ? mailto:hafeedbk@us.ibm.com
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           server_restart_complete                         #
#                                                                       #
#       Description:    This event script is called when an application #
#			server has been successfully restarted.         #
#                                                                       #
#       Called by:      resource manager                                #
#                                                                       #
#       Calls to:       None                                            #
#                                                                       #
#       Arguments:      nodename, application server resource id        #
#									#
#                       nodename - name of the node where the	        # 
#			 	   application failed. 		        #
#									#
#                       application server resource id:  the id         #
#                                  of the HACMPresource object          #
#                                  containing the application server.   #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

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

#
# Set the Run-Time Parameter values and export them
# to all successive scripts.
#
set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a

#
# The following lines create the appropriate trace output
#
[[ $VERBOSE_LOGGING == high ]] && set -x
[[ $VERBOSE_LOGGING == high ]] && version='1.19 $Source: 61haes_r711 43haes/usr/sbin/cluster/events/server_restart_complete.sh 1$'

MONITOR=""
STATUS=0
EXIT_STATUS=0

#
# Check for minimal invocation
#
if [ $# -gt 3 ]
then
    cl_log  10408 "Usage: $PROGNAME nodename resource_id [monitor name]\n"  $PROGNAME
    exit 2
fi

#
# args are the event node, id of the server and an optional monitor name
#
NODENAME=$1
ID=$2
[[ $# = 3 ]] && MONITOR=$3

#
# check to see if the LOCALNODENAME is the event node
#
if [ $NODENAME != $LOCALNODENAME ]
then
    # remote event - all done
    exit 0
fi

typeset WPARNAME EXEC WPARDIR groupName

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

groupName=$(odmget -q"id=$ID" HACMPresource | egrep "group =" | cut -f2 -d\")
[[ -z "$GROUPNAME" ]] && export GROUPNAME=$groupName

WPARNAME=$(clwparname $groupName)

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

    EXEC="clwparexec ${WPARNAME}"
fi


set -u

#
# Find what kind of resource this "server" is
#
typeset RESOURCE=$(odmget -q"id=$ID" HACMPresource | egrep "name =" \
                | cut -f2 -d\")

#
# Find is it is USERDEFINED resource type
#
typeset RESTYPE=$(odmget -q"id=$ID" HACMPresource | egrep "type =" \
                | cut -f2 -d\")

#
# check the type - only app servers and user defined resources 
# are supported by this script
#
if [[ -z $RESOURCE || ( $RESOURCE != APPLICATIONS && $RESTYPE != USERDEFINED ) ]]
then
    cl_log 6161 "Bad resource id $ID. Neither an application server nor an user defined resource. \n" $ID
    exit 2
fi

#
# Find the application name
#
APPNAME=$(odmget -q"id=$ID" HACMPresource | egrep "value =" | cut -f2 -d\")

#
# A resource can have multiple monitors associated with it - get the list
#
if [[ -z $MONITOR ]]; then
    MONITORS=$(odmget -q"name = RESOURCE_TO_MONITOR AND value = $APPNAME" HACMPmonitor |
        grep "monitor =" | cut -f2 -d'"')
else
    # if passed a specific monitor, only run methods for that one
    MONITORS=$MONITOR
fi

for monitor in $MONITORS
do
    #
    # Lookup any user defined restart method and execute it
    #

    RESTART=$(odmget -q"monitor=$monitor and name=RESTART_METHOD" HACMPmonitor \
	 | egrep "value =" | cut -d'"' -f2)

    # seperate script name from any args
    RESTART_SCRIPT=$(echo $RESTART | cut -d' ' -f1)

    if [[ ( -n ${WPARNAME} && -n $RESTART_SCRIPT &&      \
            -x ${WPARDIR}/$RESTART_SCRIPT           ) || \
          ( -z ${WPARNAME} && -n $RESTART_SCRIPT && -x $RESTART_SCRIPT ) ]]
    then
        dspmsg scripts.cat 9335 "$PROGNAME: Calling user specified restart method $RESTART_SCRIPT\n" $PROGNAME $RESTART_SCRIPT
        ${EXEC} $RESTART &
        STATUS=$?
        if [ $STATUS -ne 0 ]
        then
            cl_log 6162 "Failure in user-defined script $RESTART_SCRIPT.\n" $RESTART_SCRIPT
            # save the exit status and continue to process methods for the next monitor
            EXIT_STATUS=$STATUS
        fi
    fi
done


# If we haven't exited already, exit with success:

exit $EXIT_STATUS
