#!/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_r720 src/43haes/usr/sbin/cluster/sa/wmq/sbin/cl_wmq_query.sh 1.3 
#  
# 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_query.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################
#
# NAME:
#       cl_wmq_query
#
# PURPOSE:
#       Queries the the WebSphere MQ Manager according to the i/p flags,
#		and arguments.
#
# Flags:
#		d : Discovers WebsphereMQ is installed or not.
#		l : Lists only the WebSphere MQ Manager instances, which are not 
#			managed my the HACMP.
# 
# ARGUMENTS:n/a
#
# RETURNS: n/a
#
###############################################################################
[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

#Load common variables.
. /usr/es/sbin/cluster/sa/wmq/sbin/cl_wmq_Globals

typeset Local_node;					# Local Node name
typeset APPLICATION_NAME;			# Smart Assist application name of the
									# WebSphere MQ server.

###############################################################################
# Function: list_mqm
#
# Purpose: Lists the MQMs which are not managed by the HACMP, on local node.
#
# Arguments: None.
#
# Returns: 0 for Sucess
#          1 for Failure
###############################################################################
function list_mqm {
	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
	typeset mqm_c	# to check MQMs are managed by HACMP.
	
	#Get the WMQ Manager instances on local node.
	Local_node=$(get_local_nodename)

	#KLIB_SA_logmsg INFO 1 18 wmq_sa.cat "Listing the configured MQMs on local \
	#node %1\$s\n" $Local_node
	
    MQMS=$(/usr/mqm/bin/dspmq |awk '{ FS="[(|)]"; print $2 }')
	MQMS=$(echo "$MQMS" |tr ' ' ' ')
	
	if [[ -n MQMS ]]; then
		for MQM in $MQMS ;do
		APPLICATION_NAME="$MQM"_app;
		clquerysaapp -a $APPLICATION_NAME > /dev/null 2>&1;
			if (( $? != 0 )) ; then
				echo $MQM;
				mqm_c=true
			fi
		done
		
		# checking wether all the MQMs are managed by HACMP or  not.
		if [[ $mqm_c != "true" ]]; then
			KLIB_SA_logmsg INFO 1 32 wmq_sa.cat "There were no Unconfigured \
												Queue Managers."
		fi
		
	else
		KLIB_SA_logmsg ERROR 1 19 wmq_sa.cat "There were no MQ managers \
created on local node %1\$s\n" $Local_node
	fi
exit 0
}

###############################################################################
#
# Function: discoveryy
#
# PURPOSE: Discovers WebsphereMQ is installed or not.
#
# ARGUMENTS: n/a
#
# RETURNS: n/a
#
###############################################################################
function discover_mqm {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
	typeset installed=1
	
	for fileset in "mqm.base.runtime mqm.server.rte"
	do
		/usr/bin/lslpp -l $fileset > /dev/null 2>&1
        (( $? != 0 )) && {
            installed=0
            break
        }
    done
	
	echo "WebSphere MQ Smart Assist:WMQ:Websphere MQ Server:\
WebSphere_MQ_SERVER:$installed"
exit 0
}

###############################################################################################
# FUNCTION: wmq_smit_add
#
# PURPOSE: it generates the argumenst for SMIT
#          add a WebSphere MQ to the cluster screens.
#
# ARGUMENTS:
#		  -A application_name.
#
# RETURNS: n/a
#  
#################################################################################################
function wmq_smit_add {
	
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
	. /usr/es/sbin/cluster/sa/wmq/sbin/cl_wmq_Globals

	typeset application_name
	typeset wmq_name
		
	application_name="$wmq_server_instance"_app
    application_name=$(echo $application_name |sed 's/\./_/g;s/\%/_/g;s/\//_/g') #Replacing the special characters ".,%,/", with "_"
																			 #which are supported in the MQ manager name.
	wmq_name="$wmq_server_instance"
	p_node=$(get_local_nodename) #Primary node on the SMIT screen.

	# o/p for SMIT.
	echo "#application_name:wmq_name:p_node"
	echo "$application_name:$wmq_name:$p_node"
	exit 0
}

###############################################################################################
# FUNCTION: wmq_smit_mod
#
# PURPOSE: it generates the argumenst for SMIT
#          Modify a WebSphere MQ to the cluster screens.
#
# ARGUMENTS:
#		  -m application_id.
#
# RETURNS: n/a
#  
#################################################################################################
function wmq_smit_mod {
	
	[[ "VERBOSE_LOGGING" == "high" ]] && set -x
	. /usr/es/sbin/cluster/sa/wmq/sbin/cl_wmq_Globals
	
	typeset mqm_name
	typeset p_node
	typeset to_nodes
	typeset service_ip
	
	application_name="$application_id"
	mqm_name=$(clodmget -q "name=MQM_NAME and application_id=$application_id" -f value -n HACMPsa_metadata)
	p_node=$(clodmget -q "name=PRI_NODE and application_id=$application_id" -f value -n HACMPsa_metadata)
	to_nodes=$(clodmget -q "name=TO_NODES and application_id=$application_id" -f value -n HACMPsa_metadata)
	service_ip=$(clodmget -q "name=SERVICE_IP and application_id=$application_id" -f value -n HACMPsa_metadata)
	
	# o/p for smit modify screen
	echo "#application_name:mqm_name:p_node:to_nodes:service_ip"
	echo "$application_name:$mqm_name:$p_node:$to_nodes:$service_ip"
	
	exit 0
}
	
	
###############################################################################
#MAIN
###############################################################################
typeset ACTION option

while getopts d,l,A:M: option
do
	case $option in
		d) # Discover the MQ is installed or not.
			ACTION="discover"
			;;
			
		l) # List the MQ instances.
			ACTION="list"
			;;
			
		A) # smit o/p for add a WebsSphere MQ instance to the cluster.
			ACTION="smit_add"
			wmq_server_instance=$OPTARG		
			;;
			
		M) # smit o/p for modify a WebSphere MQ instance name.
			ACTION="smit_mod"
			application_id=$OPTARG
			;;
			
		*) 
			;;
	esac
done

[[ "$ACTION" == "discover" ]] && discover_mqm
[[ "$ACTION" == "list" ]] && list_mqm
[[ "$ACTION" == "smit_add" ]] && wmq_smit_add
[[ "$ACTION" == "smit_mod" ]] && wmq_smit_mod
