#!/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_start.sh 1.4 
#  
# 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_start.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
############################################################################
#
#  Name:  cl_wlm_start
#
#  This script is used to start or refresh the WLM using the HA config dir.
#  All necessary checks to determine whether the WLM should be started or
#  refreshed should be done externally _before_ this script is called.
#
# Arguments:    NONE 
#
# Returns:      0 - Success
#               1 - Failure
#
# Called by:    node_up and reconfig_resource_acquire
#
# Environment:  VERBOSE_LOGGING
#
############################################################################

############################################################################
# Function:   main script body
# Purpose:    primary control mechanism for the script
# Parameters: none
# Returns:    0 = successful WLM start/refresh
#             1 = failed WLM start/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=$?

# we must have an HA_prev_config_subdir file, so create one if we don't
if [[ ! -e "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir" ]]
then
        echo "" > "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir"
        if [ $? -ne 0 ]
        then
                cl_log 7414 \
                    "${PROGNAME}: Cannot write file HA_prev_config_subdir in directory /etc/wlm/${HA_WLM_CONFIG}" \
                    "${PROGNAME}" "HA_prev_config_subdir" \
                    "/etc/wlm/${HA_WLM_CONFIG}"
                exit 1
        fi

        # check if wlm is running and save config dir if it is
        if [[ "${WLM_IS_RUNNING}" -eq 0 ]]
        then
                ls -l /etc/wlm | awk '$9 ~ /current/ { print $11 }' >> \
                    "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir"
	        if [ $? -ne 0 ]
	        then
	                cl_log 7414 \
	                    "${PROGNAME}: Cannot write file HA_prev_config_subdir in directory /etc/wlm/${HA_WLM_CONFIG}" \
        	            "${PROGNAME}" "HA_prev_config_subdir" \
                	    "/etc/wlm/${HA_WLM_CONFIG}"
                	exit 1
        	fi
        fi
fi

# now start or refresh the WLM
if [[ "${WLM_IS_RUNNING}" -eq 0 ]]
then
        wlmcntrl -u -d "${HA_WLM_CONFIG}"
        if [ $? -ne 0 ]
        then
                cl_log 7420 "${PROGNAME}: Cannot start Workload Manager. Failure running wlmcntrl utility" "${PROGNAME}"
                exit 1
        fi
else
        wlmcntrl -a -d "${HA_WLM_CONFIG}"
        if [ $? -ne 0 ]
        then
                cl_log 7420 "${PROGNAME}: Cannot start Workload Manager. Failure running wlmcntrl utility" "${PROGNAME}"
                exit 1
        fi
fi

# need to give the user a rules file to use for reconfiguration
if [[ -e "/etc/wlm/${HA_WLM_CONFIG}/rules" ]]
then
        mv "/etc/wlm/${HA_WLM_CONFIG}/rules" \
           "/etc/wlm/${HA_WLM_CONFIG}/rules.active"
fi
if [[ -e "/usr/es/sbin/cluster/etc/wlm/rules" ]]
then
        cp "/usr/es/sbin/cluster/etc/wlm/rules" \
           "/etc/wlm/${HA_WLM_CONFIG}/rules" 
fi

exit 0
