#!/usr/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,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/sa/wmq/sbin/cl_wmq_manual.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 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/sa/wmq/sbin/cl_wmq_manual.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#################################################################################
# Name: cl_wmq_manual
#
# Purpose: To create PowerHA SystemMirror resources and Resource group for Managing
#          WebSphere MQ server, through the i/p XML file.
#
# Arguments: XML file path.
#
# Returns: 0 for Sucess
#          1 for Failure
################################################################################
if [[ $VERBOSE_LOGGING == high ]]
then
    set -x
    version='7d4c34b 43haes/usr/sbin/cluster/sa/wmq/sbin/cl_wmq_manual.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM'
fi

. /usr/es/sbin/cluster/sa/wmq/sbin/cl_wmq_Globals
. /usr/es/sbin/cluster/sa/wmq/sbin/cl_wmq_Utilities

KLIB_SA_logmsg INFO 1 55 wmq_sa.cat "configuring WebSphere MQ server using manual configuration\n"

typeset APP_NAME       # Smart Assist Application Name of the WebSphere MQ server.
typeset MQM_NAME       # WebSphere MQM Instance name.
typeset PRIMARY_NODE   # Primary node for the MQM RG.
typeset TO_NODES       # Take over nodes for the MQM RG.
typeset SERVICE_IP     # Service ip.
typeset PREFIX	       # Prefix for Service ip.
typeset ERR            # command o/p for validation.
typeset LISTENERS      # Listeners associated to MQM 

while getopts f: opt
do
        case $opt in

                f) # xml file path
                        XML_FILE=$OPTARG
                        ;;
        esac
done
#validate xml file
$SA_SBIN/clsaxmlutil -s -x $XML_FILE -m $WMQ_HA_CONFIG/cl_wmq_manual_config.xsd -t mqm_name
[[ $? != 0 ]] && {
					KLIB_SA_logmsg ERROR 1 68 wmq_sa.cat "The given XML file:%1\$s is invalid.\n" "$XML_FILE"
					exit 1
				}

MQM_NAME=$($SA_SBIN/clsaxmlutil -s -x $XML_FILE -m $WMQ_HA_CONFIG/cl_wmq_manual_config.xsd -t mqm_name |awk -F= '{print $2}' )
#validating MQM_NAME
/usr/mqm/bin/dspmq |grep $MQM_NAME |grep -v grep > /dev/null 2>&1
[[ $? != 0 ]] && {
					KLIB_SA_logmsg ERROR 1 67 wmq_sa.cat "The Specified Queue Manager: %1\$s is not exist/valid.\n" "$MQM_NAME"
					exit 1
				 }

APP_NAME="$MQM_NAME"_app
APP_NAME=$(echo $APP_NAME |sed 's/\./_/g;s/\%/_/g;s/\//_/g') #Replacing the special characters ".,%,/", with "_"
														 #which are supported in the MQ manager name.

ERR=$(/usr/es/sbin/cluster/utilities/clodmget -q "name=APPLICATION_NAME AND value=$APP_NAME" -f value HACMPsa_metadata 2>/dev/null)
[[ -n $ERR ]] && {
					KLIB_SA_logmsg ERROR 1 66 wmq_sa.cat "The specified Queue Manager: %1\$s is already configured.\n" "$MQM_NAME"
					exit 1
				 }
				 
validate_AppName "$APP_NAME"
[[ "$?" == "1" ]] && {
        KLIB_SA_logmsg ERROR 1 11 wmq_sa.cat "smart assist for WebSphere MQ Application id=%1\$s is not in the range [a-z][A-z][0-9]_\n" "$APP_NAME"
        exit 1
        }

PRIMARY_NODE=$($SA_SBIN/clsaxmlutil -s -x $XML_FILE -m $WMQ_HA_CONFIG/cl_wmq_manual_config.xsd -t PrimaryNode|awk -F= '{ print $2 }')
if [[ $? != 0 || -z $PRIMARY_NODE ]];then
        KLIB_SA_logmsg ERROR 1 56 wmq_sa.cat "No value is specified for Primary Node property in WebSphere MQ xml file\n"
        exit 1
fi

TO_NODES=$($SA_SBIN/clsaxmlutil -s -x $XML_FILE -m $WMQ_HA_CONFIG/cl_wmq_manual_config.xsd -t TakeoverNodes |awk -F= '{ print $2 }')

SERVICE_IP=$($SA_SBIN/clsaxmlutil -s -x $XML_FILE -m $WMQ_HA_CONFIG/cl_wmq_manual_config.xsd -t IPAddress_or_name |awk -F= '{ print $2 }')

PREFIX=$($SA_SBIN/clsaxmlutil -s -x $XML_FILE -m $WMQ_HA_CONFIG/cl_wmq_manual_config.xsd -t Prefix_or_Netmask |awk -F= '{ print $2 }')

LISTENERS=$($SA_SBIN/clsaxmlutil -s -x $XML_FILE -m $WMQ_HA_CONFIG/cl_wmq_manual_config.xsd -t Listeners |awk -F= '{ print $2 }')
# Check whether listeners are provided or not
if [[ -z "$LISTENERS" ]]; then
        KLIB_SA_logmsg ERROR 2 27 wmq_sa.cat "No Listeners were provided in WebSphere MQ xml file"
        exit 1
fi

$WMQ_HA_SBIN/cl_wmq_config -a "$APP_NAME" -m "$MQM_NAME" -p "$PRIMARY_NODE" -t "$TO_NODES" -s "$SERVICE_IP" -P "$PREFIX" -l "$LISTENERS"
exit $?
