#!/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/utils/cl_wlm_stop.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2001 
# 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/utils/cl_wlm_stop.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
############################################################################
#
#  Name:  cl_wlm_stop
#
#  This script is used to return the WLM configuration to the state it was
#  in before cluster services were started on the local node.
#
# Arguments:    none
#
# Returns:      0 - Success
#               1 - Failure
#
# Called by:    node_up and node_down and reconfig_resource_acquire
#
# Environment:  VERBOSE_LOGGING
#
############################################################################

############################################################################
# Function:   main script body
# Purpose:    primary control mechanism for the script
# Parameters: none
# Returns:    0 = successful WLM stop/refresh
#             1 = failed WLM stop/refresh
############################################################################

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

# name of specified HACMP wlm configuration
HA_WLM_CONFIG=`clwlmruntime -l | awk 'BEGIN { FS = ":" } $1 !~ /^#.*/ { print $1 }'`
if [[ -z "$HA_WLM_CONFIG" ]]
then
    cl_log 7416 \
    	"${PROGNAME}: Cannot determine name of HA WLM configuration\n" \
	${PROGNAME}
    exit 1
fi

# current wlm status
wlmcntrl -q
WLM_IS_RUNNING=$?
# the important configuration files
WLM_CONFIG_FILES="classes shares limits rules"
# the non-HA WLM config directory
PREV_WLM_CONFIG=""
# did HA start or refresh the WLM?
HA_STARTED_WLM="false"

if [[ -e "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir" ]]
then
        PREV_WLM_CONFIG=`cat "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir"`
        HA_STARTED_WLM="true"
        rm -f "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir"
fi

# clean up all previous file changes
for one_file in ${WLM_CONFIG_FILES}
do
        # restore the previous HA configuration files
        if [[ -e "/etc/wlm/${HA_WLM_CONFIG}/${one_file}.prev" ]]
        then
		# if there were any previous files, and we've got
		# any config files now, move the current ones out
		# of the way
        	if [[ -e "/etc/wlm/${HA_WLM_CONFIG}/${one_file}" ]]
        	then
        	        mv "/etc/wlm/${HA_WLM_CONFIG}/${one_file}" \
        	           "/etc/wlm/${HA_WLM_CONFIG}/${one_file}.hacmp"
        	fi

        	# move al *.prev files back to original file names
                mv "/etc/wlm/${HA_WLM_CONFIG}/${one_file}.prev" \
                   "/etc/wlm/${HA_WLM_CONFIG}/${one_file}"
        fi
done

# if the WLM was running outside of HA, set it back to its old configuration
if [[ -n "${PREV_WLM_CONFIG}" ]]
then
        if [[ "${WLM_IS_RUNNING}" -eq 0 ]]
        then
                cl_log 7431 "${PROGNAME}: Attempting to restart Workload Manager with previous configuration" "${PROGNAME}"
                wlmcntrl -u -d "${PREV_WLM_CONFIG:##*/}"
                if [ $? -ne 0 ]
                then
                        cl_log 7420 "${PROGNAME}: Cannot start Workload Manager. Failure running wlmcntrl utility" "${PROGNAME}"
                        exit 1
                fi
	elif [[ "${WLM_IS_RUNNING}" -ne 1 ]]
	then
		# running wlmcntrl to query status failed, report the error
                cl_log 7420 "${PROGNAME}: Cannot start Workload Manager. Failure running wlmcntrl utility" "${PROGNAME}"
		exit 1
        fi

# if the WLM was _not_ running outside of HA, turn it off
elif [[ "true" = "${HA_STARTED_WLM}" ]]
then
        if [[ "${WLM_IS_RUNNING}" -eq 0 ]]
        then
                wlmcntrl -o
                if [ $? -ne 0 ]
                then
                        cl_log 7430 "${PROGNAME}: Cannot stop Workload Manager. Failure running wlmcntrl utility" "${PROGNAME}"
                        exit 1
                fi
	elif [[ "${WLM_IS_RUNNING}" -ne 1 ]]
	then
		# running wlmcntrl to query status failed, report the error
		cl_log 7430 "${PROGNAME}: Cannot stop Workload Manager. Failure running wlmcntrl utility" "${PROGNAME}"
		exit 1
        fi
fi

exit 0

