#!/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/tsmadmin/sbin/cl_tsm_admin_query.sh 1.2.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# 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/tsmadmin/sbin/cl_tsm_admin_query.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################################
#
# NAME:
#       cl_tsm_admin_query
#
# PURPOSE:
#       Discover PowerHA SystemMirror configuration for TSM admin center. This script is used in SMIT
#       discovery and list screens.
#
# ARGUMENTS:
#       -n smit o/p for application name and primary node
#       -q Application_id
#
# RETURNS
#  0 on success
#  1 on Failure
#
#################################################################################################


[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
. /usr/es/sbin/cluster/sa/tsmadmin/sbin/cl_tsmadminGlobals
. /usr/es/sbin/cluster/sa/tsmadmin/sbin/cl_tsmadminUtilities

typeset application_id prim
while getopts q:n opt
do
        case $opt in
                q)#SMIT output for modifying TSM client Smart Assist
                        application_id=$OPTARG
                        ;;
                n)#SMIT output for application name and primary node
                        prim="y"
                        ;;
        esac
done

#Discovery for Application and local node name
[[ -n $prim ]] && {
                        echo "#application_name:primary"
                        local_node=$(get_local_nodename)
                        echo "tsmadmin_"$local_node":"$local_node
                        exit 0
                  }

#Discovery for modifying the smart assit application
[[ -n $application_id ]] && {

echo "#primary:takeover:serviceip:prefix:admuser"
rg=$(clodmget -q "name=RESOURCE_GROUP and application_id=$application_id" -f value -n HACMPsa_metadata)
[[ -z $rg ]]&& exit 1

nodes=$(clvt query resource_group $rg | grep -w NODES|awk -F= '{ print $2 }' | sed -e "s/\"//g")
serviceip=$(clvt query resource_group $rg | grep SERVICE_LABEL|awk -F= '{ print $2 }' | sed -e "s/\"//g")
prefix=$(clodmget -q "ip_label=$serviceip" -f netmask -n HACMPadapter)
user=$(clodmget -q "name=TSM_AC_USER and application_id=$application_id" -f value -n HACMPsa_metadata)

takeover=""
primary=""
for n in $nodes
do
        if [[ -z $primary ]]
        then
                primary=$n
        else
                takeover=$(echo "$takeover $n")
        fi
done

echo $primary":"$takeover":"$serviceip":"$prefix":"$user
exit 0
}
