#!/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_sapStartSCS.sh 1.1.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_sapStartSCS.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
##
## NAME:    cl_sapStartSCS
##
## PURPOSE:
##          Startup script for SCS 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_ips


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_ips=($(clquerysaapp -a $APPLICATION_ID \
                 | awk -F"=" '/VIRTUAL_IPS/  {print $2}' 2>/dev/null))

[[ -z $virt_ips ]] && {
    user_msg 30 32 $PROGRAM
    exit 1
}
#start the sap SCS instances

for count in "${!inst_names[@]}"; do
    out=$(/usr/bin/su - $SAPADMNUSR -c \
          "env LANG=C $SAPEXEDIR/startsap ${inst_names[$count]} ${virt_ips[$count]}" 2>/dev/null)
    (( $? == 0 )) &&  {
        user_msg 50 2 ${inst_names[$count]} ${virt_ips[$count]}
    } || {
        user_msg 50 1 $PROGRAM ${inst_names[$count]} ${virt_ips[$count]}
        exit 1
    }
done
exit 0
