#!/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 
#  
# COPYRIGHT International Business Machines Corp. 2013 
# 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_modifysapattributes.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
##
## NAME:
##  cl_modifysapattributes
##
## PURPOSE:
## Modifies the existing HACMP configurations for different SAP instances attributes
## types such as sap instance profile, instances executables directory, wait for start timeout, delay
## wait for stop timeout, delay, ENS monitor retries, monitor time out
##
## ARGUMENTS:
##
##  Modifying SAP Global Attribute passed along with the instance attributes
##      :== -t AS/SCS/ERS  
##          -N <Is NFS> 
##          -E <Exports directory>
##          -I <NFS IP>        
##          -S <Notification Script>
##          -u <SAPADMUSER>
##          -l <Logger Logfile>
##
##  Modifying Application Server Instance
##      :== -t AS
##          -s SAP System ID
##          -i <instance name>
##          -a <application name>
##          -p <profile> 
##          -e <executable directory>        
##          -o <Start timeout> 
##          -d <Start delay> 
##          -O <Stop timeout> 
##          -D <Stop delay> 
##          -y <ENS Sync> 
##          -L <NOTIFICATION_LEVEL>
##          -x <SA_SAP_XPLATFORM_LOGGING>
##          -f <EC_START_FAILED>
##          -g <EC_START_SERVICE_FAILED>
##          -n <EC_START_NFS_FAILED>
##          -M <EC_MONITOR_SAPSTARTSRV_UNAVAILABLE>
##          -G <EC_MONITOR_FAILOVER_ON_GW_OUTAGE>
##
##  Modifying SCS Instance
##      :== -t SCS
##          -s SAP System ID
##          -i <instance name>
##          -a <application name>
##          -p <profile> 
##          -e <executable directory>        
##          -o <Start timeout> 
##          -d <Start delay> 
##          -O <Stop timeout> 
##          -D <Stop delay> 
##          -y <ENS Sync> 
##          -r <IS_ERS_ENABLED>
##          -L <NOTIFICATION_LEVEL>
##          -x <SA_SAP_XPLATFORM_LOGGING>
##          -f <EC_START_FAILED>
##          -g <EC_START_SERVICE_FAILED>
##          -n <EC_START_NFS_FAILED>
##          -M <EC_MONITOR_SAPSTARTSRV_UNAVAILABLE>
##          -G <EC_MONITOR_FAILOVER_ON_GW_OUTAGE>
##
##  Modifying ERS Instance
##      :== -t ERS
##          -s SAP System ID
##          -i <instance name>
##          -a <application name>
##          -p <profile> 
##          -e <executable directory>        
##          -o <Start timeout> 
##          -d <Start delay> 
##          -O <Stop timeout> 
##          -D <Stop delay> 
##          -y <ENS Sync> 
##          -L <NOTIFICATION_LEVEL>
##          -x <SA_SAP_XPLATFORM_LOGGING>
##          -f <EC_START_FAILED>
##          -g <EC_START_SERVICE_FAILED>
##          -n <EC_START_NFS_FAILED>
##          -M <EC_MONITOR_SAPSTARTSRV_UNAVAILABLE>
##          -G <EC_MONITOR_FAILOVER_ON_GW_OUTAGE>
##
## RETURNS
##  0 on success
##  1 on failure
##

. /usr/es/lib/ksh93/func_include
version='1.8 $Source: 61haes_r711 43haes/usr/sbin/cluster/sa/sap/sbin/cl_modifysapattributes.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 TYPE
typeset SMARTASSIST_ID="SAPNW"
typeset APPLICATION_ID   # Application ID for App Discovery
typeset SAP_GLOBALS="SAP_GLOBALS"
typeset SAPSID
typeset INSTANCE
typeset INSTANCE_NO

typeset PROFILE
typeset EXECUTABLE
typeset START_TIMEOUT
typeset START_DELAY
typeset STOP_TIMEOUT
typeset STOP_DELAY
typeset ENS_SYNC
typeset IS_ERS_ENABLED
typeset NOTIFICATION_LEVEL
typeset SA_SAP_XPLATFORM_LOGGING
typeset EC_START_FAILED
typeset EC_START_SERVICE_FAILED
typeset EC_START_NFS_FAILED
typeset EC_MONITOR_SAPSTARTSRV_UNAVAILABLE
typeset EC_MONITOR_FAILOVER_ON_GW_OUTAGE

# SAP Globals 
typeset IS_NFS
typeset EXPORTS
typeset NFS_IP
typeset NOTIFY 
typeset SAPADMUSER
typeset LOGGER_LOGFILE
typeset OSCON_ONOFF_CS
typeset OSCON_ONOFF_ERS
typeset OSCON_ONOFF_APP

#----------------------------------------------------------------------------
# Function:
#   checkMinMax
#
# Purpose:
#   Used to check the allowed range of the variable.
#
# Arguments:
#   Minimum, Maximum and variables that needs to be verified.
#
# Returns:
#   0 For success
#   1 For failure
#----------------------------------------------------------------------------

function checkMinMax {
   [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    typeset -i MIN=$1
    shift 
    typeset -i MAX=$1
    shift

    for i in $@ ; do 
        eval "value=\${$i}"
        if [[ -n "$value" ]]; then 
            if [[ $value < $MIN || $value > $MAX ]] then 
                KLIB_SAP_SA_logmsg ERROR 120 19 sapsa.cat "ERROR: The value entered for %s is not valid" $i
                InternalErrorAbort
            fi 
        fi
    done 
}

#----------------------------------------------------------------------------
# Function:
#   checkFilePermissions
#
# Purpose:
#   Validates the file/directory permissions.
#
# Arguments:
#   None
#
# Returns:
#   0 For success
#   1 For failure
#----------------------------------------------------------------------------
function checkFilePermissions {
   [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    if [[ -n $NOTIFY && ! -x $NOTIFY ]]; then 
        KLIB_SAP_SA_logmsg ERROR 40 13 sapsa.cat "ERROR: Specified Notification Script does not have executable permissions."
        InternalErrorAbort
    fi  

    if [[ -n $PROFILE  && ! -s $PROFILE ]]; then 
        KLIB_SAP_SA_logmsg ERROR 40 14 sapsa.cat "ERROR: Specified SAP Profile is empty."
        InternalErrorAbort
    fi 

    if [[ -n $EXECUTABLE && -d $EXECUTABLE ]]; then
        if [[ ! -f "$EXECUTABLE/sapstartsrv" ]]; then 
            KLIB_SAP_SA_logmsg ERROR 40 15 sapsa.cat "ERROR: Specified Executable directory does not have SAP executables in it."
            InternalErrorAbort
        fi
    elif [[ -n $EXECUTABLE ]]; then
        KLIB_SAP_SA_logmsg ERROR 40 16 sapsa.cat "ERROR: Specified SAP Executables path is not a directory."
        InternalErrorAbort
    fi 

    if [[ -n $LOGGER_LOGFILE && ! -w $LOGGER_LOGFILE  ]]; then 
        KLIB_SAP_SA_logmsg ERROR 40 17 sapsa.cat "ERROR: Unable to write to specified SAP Logger log file."
        InternalErrorAbort
    fi 

    return 0
}
#----------------------------------------------------------------------------
# Function:
#   changeODMAttributes
#
# Purpose:
#   To Modify HACMPsa_metadata variable values of a SAP and SAP globals.
#   Modify the attributes values if there is any change.
#
# Arguments:
#   Application_id and Attribute names are passed as arguments.
#
# Returns:
#   0 For success
#   1 For failure
#----------------------------------------------------------------------------

function changeODMValue {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    typeset appid=$1   
    shift
    typeset value 
    
    for name in $@  ; do
        eval "value=\${$name}"
        [[ -n "$value" ]] && {
                 print "HACMPsa_metadata: value = $value " | odmchange  -o HACMPsa_metadata -q  "application_id=$appid and name=$name" 
        }
    done

    return 0
}
#----------------------------------------------------------------------------
# Function:
#   modifySAPASAttributes
#
# Purpose:
#   To Modify HACMP SAP instance attributes.
#   Validate and change the odm values of particular SAP attribute
#   and SAP Globals
#   Ex:  profile, Executables directory, start timeout, delay
#   stop timeout, delay, ENS Sync time and monitor time out
#
# Arguments:
#   None.
#
# Returns:
#   0 For success
#   1 For failure
#----------------------------------------------------------------------------

function modifySAPASAttributes {
   [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    checkMinMax "0" "1" "IS_ERS_ENABLED" "EC_START_FAILED" "EC_START_SERVICE_FAILED" "EC_START_NFS_FAILED" \
    "EC_MONITOR_SAPSTARTSRV_UNAVAILABLE" "EC_MONITOR_FAILOVER_ON_GW_OUTAGE" "IS_NFS" "OSCON_ONOFF_APP" \
    "OSCON_ONOFF_ERS" "OSCON_ONOFF_CS"

    checkMinMax "0" "99" "START_TIMEOUT" "START_DELAY" "STOP_TIMEOUT" "STOP_DELAY"

    checkMinMax "0" "20" "ENS_SYNC"

    checkMinMax "0" "8" "NOTIFICATION_LEVEL"

    checkMinMax "0" "3" "SA_SAP_XPLATFORM_LOGGING"

    checkFilePermissions

    changeODMValue $SAP_GLOBALS "IS_NFS" "EXPORTS" "NFS_IP" "NOTIFY" "SAPADMUSER" "LOGGER_LOGFILE" "OSCON_ONOFF_APP" "OSCON_ONOFF_ERS" "OSCON_ONOFF_CS"

    changeODMValue $APPLICATION_ID "PROFILE" "EXECUTABLE" "START_TIMEOUT" "START_DELAY" "STOP_TIMEOUT" "STOP_DELAY" "ENS_SYNC" "MONITOR" \
    "IS_ERS_ENABLED" "NOTIFICATION_LEVEL" "SA_SAP_XPLATFORM_LOGGING" "EC_START_FAILED" "EC_START_SERVICE_FAILED" "EC_START_NFS_FAILED" \
    "EC_MONITOR_SAPSTARTSRV_UNAVAILABLE" "EC_MONITOR_FAILOVER_ON_GW_OUTAGE"

    return 0                             
}
#----------------------------------------------------------------------------
# Function:
#   modifySAPERSAttributes
#
# Purpose:
#   To Modify HACMP SAP instance attributes.
#   Validate and change the odm values of particular SAP attribute
#   and SAP Globals
#   Ex:  profile, Executables directory, start timeout, delay
#   stop timeout, delay, ENS Sync time and monitor time out
#
# Arguments:
#   None.
#
# Returns:
#   0 For success
#   1 For failure
#----------------------------------------------------------------------------

function modifySAPERSAttributes {
   [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    checkMinMax "0" "1" "EC_START_FAILED" "EC_START_SERVICE_FAILED" "EC_START_NFS_FAILED" \
    "EC_MONITOR_SAPSTARTSRV_UNAVAILABLE" "EC_MONITOR_FAILOVER_ON_GW_OUTAGE" "IS_NFS" "OSCON_ONOFF_APP" \
    "OSCON_ONOFF_ERS" "OSCON_ONOFF_CS"

    checkMinMax "0" "99" "START_TIMEOUT" "START_DELAY" "STOP_TIMEOUT" "STOP_DELAY"

    checkMinMax "0" "20" "ENS_SYNC"

    checkMinMax "0" "8" "NOTIFICATION_LEVEL"

    checkMinMax "0" "3" "SA_SAP_XPLATFORM_LOGGING"

    checkFilePermissions

    changeODMValue $SAP_GLOBALS "IS_NFS" "EXPORTS" "NFS_IP" "NOTIFY" "SAPADMUSER" "LOGGER_LOGFILE" "OSCON_ONOFF_APP" "OSCON_ONOFF_ERS" "OSCON_ONOFF_CS"

    changeODMValue $APPLICATION_ID "PROFILE" "EXECUTABLE" "START_TIMEOUT" "START_DELAY" "STOP_TIMEOUT" "STOP_DELAY" "ENS_SYNC" \
    "NOTIFICATION_LEVEL" "SA_SAP_XPLATFORM_LOGGING" "EC_START_FAILED" "EC_START_SERVICE_FAILED" "EC_START_NFS_FAILED" "EC_MONITOR_SAPSTARTSRV_UNAVAILABLE" \
    "EC_MONITOR_FAILOVER_ON_GW_OUTAGE"

    return 0
}

#----------------------------------------------------------------------------
# Function:
#   modifySAPSCSAttributes
#
# Purpose:
#   To Modify HACMP SAP instance attributes.
#   Validate and change the odm values of particular SAP attribute
#   and SAP Globals
#   Ex:  profile, Executables directory, start timeout, delay
#   stop timeout, delay, ENS Sync time and monitor time out
#
# Arguments:
#   None.
#
# Returns:
#   0 For success
#   1 For failure
#----------------------------------------------------------------------------

function modifySAPSCSAttributes {

   [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    checkMinMax "0" "1" "IS_ERS_ENABLED" "EC_START_FAILED" "EC_START_SERVICE_FAILED" "EC_START_NFS_FAILED" \
    "EC_MONITOR_SAPSTARTSRV_UNAVAILABLE" "EC_MONITOR_FAILOVER_ON_GW_OUTAGE" "IS_NFS" "OSCON_ONOFF_APP" \
    "OSCON_ONOFF_ERS" "OSCON_ONOFF_CS"

    checkMinMax "0" "99" "START_TIMEOUT" "START_DELAY" "STOP_TIMEOUT" "STOP_DELAY"

    checkMinMax "0" "20" "ENS_SYNC"

    checkMinMax "0" "8" "NOTIFICATION_LEVEL"

    checkMinMax "0" "3" "SA_SAP_XPLATFORM_LOGGING"

    checkFilePermissions

    changeODMValue $SAP_GLOBALS "IS_NFS" "EXPORTS" "NFS_IP" "NOTIFY" "SAPADMUSER" "LOGGER_LOGFILE" "OSCON_ONOFF_APP" "OSCON_ONOFF_ERS" "OSCON_ONOFF_CS"

    changeODMValue $APPLICATION_ID "PROFILE" "EXECUTABLE" "START_TIMEOUT" "START_DELAY" "STOP_TIMEOUT" "STOP_DELAY" "ENS_SYNC"  \
    "IS_ERS_ENABLED" "NOTIFICATION_LEVEL" "SA_SAP_XPLATFORM_LOGGING" "EC_START_FAILED" "EC_START_SERVICE_FAILED" "EC_START_NFS_FAILED" \
    "EC_MONITOR_SAPSTARTSRV_UNAVAILABLE" "EC_MONITOR_FAILOVER_ON_GW_OUTAGE"

    return 0
}


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

ALLARGS=$*
while getopts t:s:i:a:p:e:o:d:O:D:y:m:N:E:I:S:r:x:f:g:n:M:G:u:l:L:X:Y:Z: option
do
    case $option in 
        t)  # Type of instance
            TYPE=$OPTARG
        ;;
        s)  # SAPSYSTEMNAME
            SAPSID=$OPTARG
            SAP_GLOBALS=$(echo $SAP_GLOBALS"_"$SAPSID)
        ;;
        i)  # SAP Instance Name
            INSTANCE=$OPTARG
            # We need to grep Instance No from Instance name
            if [[ ! -z $INSTANCE ]]; then
                INSTANCE_NO=${INSTANCE##${INSTANCE%%??}}
            fi
        ;;
        a)  # Application ID
            APPLICATION_ID=$OPTARG
            APPLICATION_ID=${APPLICATION_ID// /} 
        ;;
        p)  # SAP INSTANCE PROFILE DIRECTORY
            PROFILE=$OPTARG
            PROFILE=${PROFILE// /} 
        ;;
        e)  # SAP INSTANCE EXECUTABLE DIRECTORY
            EXECUTABLE=$OPTARG
            EXECUTABLE=${EXECUTABLE// /} 
        ;;
        o)  #SAP CONTROL WAIT FOR START TIMEOUT
            START_TIMEOUT=$OPTARG
        ;;
        d)  #SAP CONTROL WAIT FOR START DELAY
            START_DELAY=$OPTARG
        ;;
        O)  #SAP CONTROL WAIT FOR STOP TIMEOUT
            STOP_TIMEOUT=$OPTARG
        ;;
        D)  #SAP CONTROL WAIT FOR STOP DELAY
            STOP_DELAY=$OPTARG
        ;;
        y)  # ENS SYNC RETRIES
            ENS_SYNC=$OPTARG 
        ;;
        N)  # IS NFS
            IS_NFS=$OPTARG 
        ;;
        E)  # SAPMNT EXPORT DIRECTORY 
            EXPORTS=$OPTARG 
        ;;
        I)  # NFS IP
            NFS_IP=$OPTARG 
        ;;
        S)  # NOTIFICATION SCRIPT
            NOTIFY=$OPTARG 
        ;;
        r)  # IS ERS ENABLED
            IS_ERS_ENABLED=$OPTARG 
        ;;
        x)  # SAP SA XPLATFORM LOGGING
            SA_SAP_XPLATFORM_LOGGING=$OPTARG 
        ;;
        f)  # EXIT CODE START FAILED
            EC_START_FAILED=$OPTARG 
        ;;
        g)  # EXIT CODE START SERVICE FAILED
            EC_START_SERVICE_FAILED=$OPTARG 
        ;;
        n)  # EXIT CODE START NFS FAILED
            EC_START_NFS_FAILED=$OPTARG 
        ;;
        M)  # EXIT CODE MONITOR UNAVAILABLE
            EC_MONITOR_SAPSTARTSRV_UNAVAILABLE=$OPTARG 
        ;;
        G)  # EXIT CODE MONITOR GW OUTAGE
            EC_MONITOR_FAILOVER_ON_GW_OUTAGE=$OPTARG 
        ;;
        u)  # SAP ADMIN USER
            SAPADMUSER=$OPTARG 
        ;;
        l)  # LOGGER LOGFILE
            LOGGER_LOGFILE=$OPTARG 
        ;;
        L)  # NOTIFICATION LEVEL
            NOTIFICATION_LEVEL=$OPTARG 
        ;;
        X)  # OSCON_ONOFF_CS
            OSCON_ONOFF_CS=$OPTARG 
        ;;
        Y)  # OSCON_ONOFF_ERS
            OSCON_ONOFF_ERS=$OPTARG 
        ;;
        Z)  # OSCON_ONOFF_APP
            OSCON_ONOFF_APP=$OPTARG 
        ;;
        *)
        ;;
    esac
done

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

typeset -A modifyAttributesMethod
modifyAttributesMethod=(
    [AS]="modifySAPASAttributes"
    [SCS]="modifySAPSCSAttributes"
    [ERS]="modifySAPERSAttributes"
)

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

${modifyAttributesMethod[$TYPE]}
exit $?
