#!/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. 
#  
# 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_sapdiscover.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
##
## NAME:    cl_sapdiscover
##
## PURPOSE:
##          Produces the Smart Assistant output suitable for parsing by the
##          application discovery clquerysa command.  Shows the user what
##          types of instances are available on the local node.
##
##
## ARGUMENTS:
##
##  Type:
##      -t GFS/AS/SCS/ERS/DB
##
##
## OUTPUT:
## -t GFS
## SAP Smart Assist:SAPNW:SAP NetWeaver Global Filesystem:SAPNW_SAPGFS:{0|1}
## -t SCS
## SAP Smart Assist:SAPNW:SAP NetWeaver (A)SCS Instance:SAPNW_SCSINSTANCE:{0|1}
## -t ERS
## SAP Smart Assist:SAPNW:SAP NetWeaver ERS Instance:SAPNW_ERSINSTANCE:{0|1}
## -t AS
## SAP Smart Assist:SAPNW:SAP NetWeaver AS Instance:SAPNW_ASINSTANCE:{0|1}
## -t DB
## SAP Smart Assist:SAPNW:SAP Database Instance:SAPNW_DBINSTANCE:{0|1}
## 
## RETURNS:
##      0 on success
##      1 on failure
##
. /usr/es/lib/ksh93/func_include
version='1.2.1.16 $Source: 61haes_r711 43haes/usr/sbin/cluster/sa/sap/sbin/cl_sapdiscover.sh 1$'


#----------------------------------------------------------------------------
# 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
#----------------------------------------------------------------------------
typeset NODE

#----------------------------------------------------------------------------
# Function:
#   sapIsGFSInstalled
#
# Purpose:
#   Reports to clquerysa (Application Discovery) whether SAP Global Filesystem
#   is accessible & configurable.
#
# Output:
#   SAP Smart Assist:SAPNW:1.SAP NW Global Filesystem:SAPNW_SAPGFS:[0|1]
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function sapIsGFSInstalled {
    typeset ret sapmntdir saptransdir gfs_type gfs_type1 gfs_type2
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    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/sap_mount_output" ;then
            setSAPGlobalEnv $SID
        else 
            continue
        fi

        ret=$?
        (( $ret != 0 )) && print -n "0"
        print -n "$NODE:"
        /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
        print -n ":SAPNW:"
        /usr/bin/dspmsg -s 21 sapsa.cat 2 "SAP NetWeaver Global Filesystem"
        print -n ":SAPNW_SAPGFS:"
        [[ -h $SAPMNTDIR ]] && { 
            print -n "0:$SID\n"
            continue
        }
      
     # Checking whether saptransdir is nfs3 (or) nfs4, if not continue for next SID
     # Here the logic illustrates that there is possibilty we get more than one value when
     # we try to grep using $SAPTRANSDIR since it is same for all SIDs as below example
     # mount | grep "/usr/sap/trans"
     # naruto   /export/usr/sap/trans /usr/sap/trans   nfs3   Mar 25 05:24
     # prop     /export1/usr/sap/trans /usr/sap/trans   nfs3   Mar 25 05:45

        gfs_type1=$(grep -w "$SAPTRANSDIR"  "/var/hacmp/log/sap_mount_output" | grep -w "nfs3")
        gfs_type2=$(grep -w "$SAPTRANSDIR"  "/var/hacmp/log/sap_mount_output" | grep -w "nfs4")
         if [[ -z $gfs_type1 ]];then
            if [[ -z $gfs_type2 ]];then
               print -n "0:$SID\n"
            continue
            fi
         fi

     # Checking whether sapmntdir is nfs3 (or) nfs4, if not continue for next SID
    
        gfs_type=$(grep -w "$SAPMNTDIR" "/var/hacmp/log/sap_mount_output" | grep -w "$SID" | awk '{print $4}')    
        [[ $gfs_type == "nfs3" || $gfs_type == "nfs4" ]] || {
            print -n "0:$SID\n"
            continue
        }
        checkIsSAPVersionOK
        ret=$?
        if (( $ret != 0 ));then
             print -n "0:$SID"
        else
             print -n "1:$SID\n"
        fi
    done
       if (( $valid_version == 1 ));then
           exit -1
       fi
    rm /var/hacmp/log/sap_mount_output
}

#----------------------------------------------------------------------------
# Function:
#   sapIsASInstalled
#
# Purpose:
#   Reports to clquerysa (Application Discovery) whether SAP AS instances
#   are installed on the local system.
#
# Output:
#   SAP Smart Assist:SAPNW:SAP NetWeaver AS Instance:SAPNW_ASINSTANCE:[0|1]
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function sapIsASInstalled {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset count=0
    typeset ret
    ## 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/sap_mount_output" ;then
            setSAPGlobalEnv  $SID
             ret=$?
            (( $ret != 0 )) && continue
            checkIsSAPVersionOK
             ret=$?
            (( $ret == 1 )) && continue
            typeset allASInstances

            getAllRunningASInstances allASInstances

            if (( ${#allASInstances[*]} > 0 )); then
                if [[ $count == 0 ]];then
                    /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
                    print -n ":SAPNW:"
                    /usr/bin/dspmsg -s 21 sapsa.cat 5 "SAP NetWeaver AS Instance"
                    print -n ":SAPNW_ASINSTANCE:"

                    print -n "1\n"
                    count=1
                fi
            else
                  if [[ "$SAP_FORCE_DETECT" == "true" ]]; then
                        if [[ $count == 0 ]];then
                             /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
                             print -n ":SAPNW:"
                             /usr/bin/dspmsg -s 21 sapsa.cat 5 "SAP NetWeaver AS Instance"
                             print -n ":SAPNW_ASINSTANCE:"
                             print -n "1\n"
                             count=1
                        fi
                  fi
             fi
    
       else
         continue
       fi
   done
    rm /var/hacmp/log/sap_mount_output
     
     if (( $valid_version == 1 ));then
           exit -1
     fi

    ##we execute this if condition when none of the SID mount points
    ## are available
    if [[ $count == 0 ]];then
         /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
         print -n ":SAPNW:"
         /usr/bin/dspmsg -s 21 sapsa.cat 5 "SAP NetWeaver AS Instance"
         print -n ":SAPNW_ASINSTANCE:"
         print -n "0\n"
    fi
    ## we execute this if condition when none of the SAP executables are
    ## found.
    if [[ $exe_check != 0 ]]
    then
          echo "No SAP executables are found "
          exit -1
    fi
  
}

#----------------------------------------------------------------------------
# Function:
#   sapIsSCSInstalled
#
# Purpose:
#   Reports to clquerysa (Application Discovery) whether SAP SCS instances
#   are installed on the local system.
#
# Output:
#   SAP Smart Assist:SAPNW:SAP NetWeaver (A)SCS Instance:SAPNW_SCSINSTANCE:[0|1]
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function sapIsSCSInstalled {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset count=0
    typeset ret
    ## 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/sap_mount_output" ;then
            setSAPGlobalEnv  $SID
            ret=$?
            (( $ret != 0 )) && continue
            checkIsSAPVersionOK
            ret=$?
            (( $ret == 1 )) && continue
            typeset allSCSInstances

            getAllRunningSCSInstances allSCSInstances

            if (( ${#allSCSInstances[*]} > 0 )); then
                if [[ $count == 0 ]];then
                    /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
                    print -n ":SAPNW:"
                    /usr/bin/dspmsg -s 21 sapsa.cat 3 "SAP NetWeaver SCS Instance"
                    print -n ":SAPNW_SCSINSTANCE:"

                    print -n "1\n"
                    count=1
                fi
            else
                  if [[ "$SAP_FORCE_DETECT" == "true" ]]; then
                        if [[ $count == 0 ]];then
                             /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
                             print -n ":SAPNW:"
                             /usr/bin/dspmsg -s 21 sapsa.cat 3 "SAP NetWeaver SCS Instance"
                             print -n ":SAPNW_SCSINSTANCE:"
                             print -n "1\n"
                             count=1
                        fi
                  fi
             fi

       else
         continue
       fi
   done
    rm /var/hacmp/log/sap_mount_output

    if (( $valid_version == 1 ));then
           exit -1
     fi

    ##we execute this if condition when none of the SID mount points
    ## are available
    if [[ $count == 0 ]];then
         /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
         print -n ":SAPNW:"
         /usr/bin/dspmsg -s 21 sapsa.cat 3 "SAP NetWeaver SCS Instance"
         print -n ":SAPNW_SCSINSTANCE:"
         print -n "0\n"
    fi
    ## we execute this if condition when none of the SAP executables are
    ## found.
    if [[ $exe_check != 0 ]]
    then
          echo "No SAP executables are found "
          exit -1
    fi

}

#----------------------------------------------------------------------------
# Function:
#   sapIsERSInstalled
#
# Purpose:
#   Reports to clquerysa (Application Discovery) whether SAP ERS instances
#   are installed on the local system.
#
# Output:
#   SAP Smart Assist:SAPNW:SAP NetWeaver ERS Instance:SAPNW_ERSINSTANCE:[0|1]
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function sapIsERSInstalled {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset count=0
    typeset ret
    ## 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/sap_mount_output" ;then
            setSAPGlobalEnv  $SID
            ret=$?
            (( $ret != 0 )) && continue
            checkIsSAPVersionOK
            ret=$?
             (( $ret == 1 )) && continue
            typeset allERSInstances

            getAllRunningERSInstances allERSInstances

            if (( ${#allERSInstances[*]} > 0 )); then
                if [[ $count == 0 ]];then
                    /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
                    print -n ":SAPNW:"
                    /usr/bin/dspmsg -s 21 sapsa.cat 4 "SAP NetWeaver ERS Instance"
                    print -n ":SAPNW_ERSINSTANCE:"
                    print -n "1\n"
                    count=1
                fi
            else
                  if [[ "$SAP_FORCE_DETECT" == "true" ]]; then
                        if [[ $count == 0 ]];then
                             /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
                             print -n ":SAPNW:"
                             /usr/bin/dspmsg -s 21 sapsa.cat 4 "SAP NetWeaver ERS Instance"
                             print -n ":SAPNW_ERSINSTANCE:"
                             print -n "1\n"
                             count=1
                        fi
                  fi
             fi

       else
         continue
       fi
   done
    rm /var/hacmp/log/sap_mount_output

    if (( $valid_version == 1 ));then
           exit -1
     fi

    ##we execute this if condition when none of the SID mount points
    ## are available
    if [[ $count == 0 ]];then
         /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
         print -n ":SAPNW:"
         /usr/bin/dspmsg -s 21 sapsa.cat 4 "SAP NetWeaver ERS Instance"
         print -n ":SAPNW_ERSINSTANCE:"
         print -n "0\n"
    fi

    ## we execute this if condition when none of the SAP executables are
    ## found.
    if [[ $exe_check != 0 ]]
    then
          echo "No SAP executables are found "
          exit -1
    fi

}

#----------------------------------------------------------------------------
# Function:
#   sapIsDBInstalled
#
# Purpose:
#   Reports to clquerysa (Application Discovery) whether DB2 RDBMS instances
#   are installed on the local system.
#
# Output:
#   SAP Smart Assist:SAPNW:SAP Database Instance:SAPNW_DBINSTANCE:[0|1]
#
# Returns:
#   0 on success
#   1 on failure
#
function sapIsDBInstalled {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
     typeset db2out
    /usr/bin/dspmsg -s 21 sapsa.cat 1 "SAP Smart Assist"
    print -n ":SAPNW:"
    /usr/bin/dspmsg -s 21 sapsa.cat 6 "SAP Database Instance"
    print -n ":SAPNW_DBINSTANCE:"
    db2out=$(/usr/es/sbin/cluster/sa/db2/sbin/cl_db2smquery -S | awk -F":" '{print $NF}')
    print -n $db2out
}


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

typeset TYPE 
while getopts t:n: option; do
    case $option in
    t)
        TYPE=$OPTARG
    ;;
    n)
        NODE=$OPTARG
    ;;
    *) 
    ;;
    esac
done

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

# commenting this call as we learned that more than one AS 
# be defined for a single SID
#check_sapservices

mount > /var/hacmp/log/sap_mount_output
typeset -A installedMethod
installedMethod=(
    [GFS]="sapIsGFSInstalled"
    [AS]="sapIsASInstalled"
    [SCS]="sapIsSCSInstalled"
    [ERS]="sapIsERSInstalled"
    [DB]="sapIsDBInstalled"
)

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

${installedMethod[$TYPE]}
exit $?
