#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,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/sap/sbin/cl_listsapinstances.sh 1.2.1.9 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2014 
# 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/sap/sbin/cl_listsapinstances.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
##
## NAME:    cl_listsapinstances
##
## PURPOSE:
##          Produces the output suitable for parsing by SMIT screen's 
##          command to list. The output contains the list of running SAP 
##          instances of different type.
##
##
## ARGUMENTS:
##
##  Type:
##      -t AS/SCS/ERS/DB
##
##
## OUTPUT:
## 
## 
## RETURNS:
##      0 on success
##      1 on failure
##
. /usr/es/lib/ksh93/func_include
version='1.2.1.9'


#----------------------------------------------------------------------------
# Global Definitions
#----------------------------------------------------------------------------

. /usr/es/sbin/cluster/sa/sap/etc/SAPGlobals
. /usr/es/sbin/cluster/sa/sap/sbin/SAPUtilities


PATH=$PATH:/usr/es/sbin/cluster/sa/sbin
PATH=$PATH:/usr/es/sbin/cluster/utilities

#----------------------------------------------------------------------------


#----------------------------------------------------------------------------
# Function:
#   allLocalSAPASInstances
#
# Purpose:
#   To list all local running SAP Application instances.
#
# Output:
#   List of SAP Application Server Instances.
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function allLocalSAPASInstances {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
     typeset -i ret count
    typeset allASInstances

    ## this loop searches for SID whose mount directories are available instead
    ## of exiting if first SID directories are not available

    for SID in $( grep -w "^LIBPATH" "/usr/sap/sapservices" | awk -F"/" '{ print $4 }' | sort | uniq) ; do
        SAPMNTDIR=/sapmnt/$SID
        if  grep -qw "$SAPMNTDIR" "/var/hacmp/log/sapmount_output" ;then
            setSAPGlobalEnv  $SID
            ret=$?
            (( $ret != 0 )) && continue
            getAllRunningASInstances allASInstances
            for count in "${!allASInstances[@]}"
            do
             echo ${allASInstances[$count]}
            done
        else
          continue
        fi
    done
  rm /var/hacmp/log/sapmount_output
}
#----------------------------------------------------------------------------
# Function:
#   sapListASInstances
#
# Purpose:
#   List different SAP Application Server instances running in a SAP System
#   for SMIT screen's command to list. 
#
# Output:
#   List of SAP Application Server Instances.
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function sapListASInstances {
    [[ "VERBOSE_LOGGING" == "high" ]] && set -x
    typeset nodes=$*

    [[ -z $LOCALNODENAME ]] && {
        allLocalSAPASInstances
        return 0
    }
    for node in $nodes; do
        [[ "$node" == "$LOCALNODENAME" ]] && {
            allLocalSAPASInstances
        } || {
            cl_rsh $node \
            "/usr/es/sbin/cluster/sa/sap/sbin/cl_listsapinstances -t AS -I"
        }
    done
    return 0
}


#----------------------------------------------------------------------------
# Function:
#   allLocalSAPSCSInstances
#
# Purpose:
#   To list all local running SAP SCS instances.
#
# Output:
#   List of SAP SCS Instances.
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function allLocalSAPSCSInstances {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset -i ret count
    typeset allSCSInstances

    ## this loop searches for SID whose mount directories are available instead
    ## of exiting if first SID directories are not available
  
    for SID in $( grep -w "^LIBPATH" "/usr/sap/sapservices" | awk -F"/" '{ print $4 }' | sort | uniq) ; do
        SAPMNTDIR=/sapmnt/$SID
        if  grep -qw "$SAPMNTDIR" "/var/hacmp/log/sapmount_output" ;then
            setSAPGlobalEnv  $SID
             ret=$?
             (( $ret != 0 )) && continue
             getAllRunningSCSInstances allSCSInstances
             for count in "${!allSCSInstances[@]}"
             do
             echo ${allSCSInstances[$count]}
             done
        else
           continue
        fi
    done
  rm /var/hacmp/log/sapmount_output
}


#----------------------------------------------------------------------------
# Function:
#   sapListSCSInstances
#
# Purpose:
#   List different SAP Central Services (SCS) instances running in a SAP System
#   for SMIT screen's command to list. 
#
# Output:
#   List of SAP Central Services Instances. At anytime this list will have 
#   max. of one entry as in any HA SAP system one can have SCS for Java, SCS for
#   ABAP or both, however HACMP manages both these instances with a single RG.
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function sapListSCSInstances {
    [[ "VERBOSE_LOGGING" == "high" ]] && set -x
    typeset nodes=$*

    [[ -z $LOCALNODENAME ]] && {
        allLocalSAPSCSInstances
        return 0
    }
    for node in $nodes; do
        [[ "$node" == "$LOCALNODENAME" ]] && {
            allLocalSAPSCSInstances
        } || {
            cl_rsh $node \
            "/usr/es/sbin/cluster/sa/sap/sbin/cl_listsapinstances -t SCS -I"
        }
    done
    return 0
}

#----------------------------------------------------------------------------
# Function:
#   allLocalSAPERSInstances
#
# Purpose:
#   To list all local running SAP ERS instances.
#
# Output:
#   List of SAP ERS Instances.
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function allLocalSAPERSInstances {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset -i ret count out
    typeset allERSInstances
 
    ## this loop searches for SID whose mount directories are available instead
    ## of exiting if first SID directories are not available

    for SID in $( grep -w "^LIBPATH" "/usr/sap/sapservices" | awk -F"/" '{ print $4 }' | sort | uniq) ; do
        SAPMNTDIR=/sapmnt/$SID
        if  grep -qw "$SAPMNTDIR" "/var/hacmp/log/sapmount_output" ;then
            setSAPGlobalEnv  $SID
            ret=$?
            (( $ret != 0 )) && continue
            getAllRunningERSInstances allERSInstances
            for count in "${!allERSInstances[@]}"
            do
              echo ${allERSInstances[$count]}
            done
        else
          continue
        fi
    done
   rm /var/hacmp/log/sapmount_output
}


#----------------------------------------------------------------------------
# Function:
#   sapListERSInstances
#
# Purpose:
#   List different SAP Enqueue replication server instances running in a SAP System
#   for SMIT screen's command to list. 
#
# Output:
#   List of SAP Enqueue replication server instances. At anytime this list will 
#   hava max. of one entry. User can setup ERS instance either for Java SCS 
#   or for ABAP SCS or may be both if he is running ABAP+Java installation.
#   However, even if a SAP system has two ERS instances the HACMP manages both in
#   a single RG.
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function sapListERSInstances {
    [[ "VERBOSE_LOGGING" == "high" ]] && set -x
    typeset nodes=$*

    [[ -z $LOCALNODENAME ]] && {
        allLocalSAPERSInstances
        return 0
    }
    for node in $nodes; do
        [[ "$node" == "$LOCALNODENAME" ]] && {
            allLocalSAPERSInstances
        } || {
            cl_rsh $node \
            "/usr/es/sbin/cluster/sa/sap/sbin/cl_listsapinstances -t ERS -I"
        }
    done
    return 0
}


#----------------------------------------------------------------------------
# Main
#----------------------------------------------------------------------------

LOCALNODENAME=$(get_local_nodename 2>/dev/null)

typeset TYPE nodes

while getopts t:IL option; do
    case $option in
    t)
        TYPE=$OPTARG
    ;;
    I)
        # List the accessible instances on this node
        nodes=$LOCALNODENAME
        ;;
    L)
        # List instances on all nodes
        typeset -A list
        KLIB_HACMP_list_nodes list
        nodes=${list[*]}
        ;;        
    *) 
    ;;
    esac
done


# No type specified, abort immediately
 [[ -z $TYPE ]] && exit 1

# If -L or -I options are not used, abort immediately
 [[ -z $nodes ]] && exit 1

mount > /var/hacmp/log/sapmount_output

typeset -A listInstances
listInstances=(
    [AS]="sapListASInstances"
    [SCS]="sapListSCSInstances"
    [ERS]="sapListERSInstances"
)

if [[ -z ${listInstances[$TYPE]} ]]; then
    exit 1
fi

${listInstances[$TYPE]} $nodes | sort -u
exit $?
