#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,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_sapStopERS.sh 1.3.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2015 
# 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_sapStopERS.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM 
##
## NAME:    cl_sapStopERS
##
## PURPOSE:
##          Stop script for ERS instance(s).
##
##
## ARGUMENTS:
##
##  Type:
##      -a Application ID
##
##
## OUTPUT:
## 
## 
## RETURNS:
##      0 on success
##      1 on failure
##
. /usr/es/lib/ksh93/func_include
typeset version='1.1'

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

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

typeset PROGRAM=${0##*/}
PATH=$PATH:/usr/es/sbin/cluster/sa/sbin
PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)
export PATH

#----------------------------------------------------------------------------
typeset ret SID instances inst_names virt_ip inst_nos
typeset instance_start_profile inst_profile profile

if [[ -z $ODMDIR ]]; then
    ODMDIR=/usr/es/sbin/cluster/etc/objrepos/active/
fi
export ODMDIR

while getopts a: option
do
    case $option in
    a)
        APPLICATION_ID=$OPTARG
    ;;
    esac
done

[[ -z $APPLICATION_ID ]] && {
    user_msg 30 30 $PROGRAM
    exit 1
}

set -a
eval $(clquerysaapp -a $APPLICATION_ID | grep SAPSYSTEMNAME)
set +a

export SAPSYSTEMNAME

[[ -z $SAPSYSTEMNAME ]] && {
    user_msg 30 34 $PROGRAM
    exit 1
}

user_msg 40 10 $SAPSYSTEMNAME

setSAPGlobalEnv $SAPSYSTEMNAME
ret=$?
(( $ret != 0 )) && {
    user_msg 30 12 
    exit 1
}

inst_names=($(clquerysaapp -a $APPLICATION_ID \
                 | awk -F"=" '/INSTANCE_NAMES/  {print $2}' 2>/dev/null))

[[ -z $inst_names ]] && {
    user_msg 30 31 $PROGRAM
    exit 1
}

virt_ip=$(/usr/bin/hostname -s)

[[ -z $virt_ip ]] && {
    user_msg 30 32 $PROGRAM
    exit 1
}

inst_nos=($(clquerysaapp -a $APPLICATION_ID \
                 | awk -F"=" '/INSTANCE_NUMBERS/  {print $2}' 2>/dev/null))

[[ -z $inst_nos ]] && {
    user_msg 30 33 $PROGRAM
    exit 1
}

#stop the sap ERS instances along with it's sapstartsrv process

for count in "${!inst_names[@]}"; do
    #If the instance is not running then startsap check will return 1
    /usr/bin/su - $SAPADMNUSR -c "env LANG=C  $SAPEXEDIR/startsap check ${inst_names[$count]} $virt_ip >& /dev/null;exit $status"
    (( $? == 1 )) && continue

    out=$(/usr/bin/su - $SAPADMNUSR -c \
          "env LANG=C  $SAPEXEDIR/stopsap ${inst_names[$count]} $virt_ip" 2>/dev/null)
    (( $? == 0 )) && {
        user_msg 70 4 ${inst_names[$count]} $virt_ip
    } || {
        user_msg 70 3 $PROGRAM ${inst_names[$count]} $virt_ip
	var=$(/usr/bin/su - $SAPADMNUSR -c \
	      "env LANG=C  $SAPEXEDIR/cleanipc ${inst_nos[$count]} remove" 2>/dev/null)
    } 

   # Set START_PROFILE
        instance_start_profile="/usr/sap/"$SAPSYSTEMNAME"/SYS/profile/START_"${inst_names[$count]}"_"$virt_ip
   # Set INSTANCE_PROFILE
        inst_profile="/usr/sap/"$SAPSYSTEMNAME"/SYS/profile/"$SAPSYSTEMNAME"_"${inst_names[$count]}"_"$virt_ip
    if [ -f ${instance_start_profile} ]; then
      profile=${instance_start_profile};
    else
      profile=${inst_profile};
    fi

    stopsapstartsrv $profile
    (( $? == 0 )) && {
        user_msg 70 6 ${inst_names[$count]}
    } || {
        user_msg 70 5 $PROGRAM ${inst_names[$count]}
    }
done
exit 0
