#!/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_sapStopAS.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_sapStopAS.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM 
##
## NAME:    cl_sapStopAS
##
## PURPOSE:
##          Stop script for AS instance.
##
##
## ARGUMENTS:
##
##  Type:
##      -a Application ID
##
##
## OUTPUT:
## 
## 
## RETURNS:
##      0 on success
##      1 on failure
##
. /usr/es/lib/ksh93/func_include
typeset version='1.3.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 out SID inst_name virt_ip profile inst_nos
typeset instance_start_profile inst_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_name=$(clquerysaapp -a $APPLICATION_ID \
                 | awk -F"=" '/INSTANCE_NAME/  {print $2}' 2>/dev/null)

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

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

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

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

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

#If the instance is not running then startsap check will return 1
/usr/bin/su - $SAPADMNUSR -c "env LANG=C  $SAPEXEDIR/startsap check $inst_name $virt_ip >& /dev/null;exit $status"
(( $? == 1 )) && exit 0


#stop the sap AS instance along with it's sapstartsrv process

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

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

stopsapstartsrv $profile
(( $? == 0 )) && {
    user_msg 60 6 $inst_name
} || {
    user_msg 60 5 $PROGRAM $inst_name
}

exit 0
