#!/bin/bash
#
# SAPHanaTopology
#
# Description:	Analyses two  SAP HANA Databases in System Replication
#               SAPHanaTopology and SAPHanaController are both needed
#               SAPHanaController is dependent of the analysis of
#               SAPHanaTopology
#
##############################################################################
#
# SAPHanaTopology: (short sht)
# Author:       Fabian Herschel, February 2014
# Support:      linux@sap.com
# License:      GNU General Public License (GPL)
# Copyright:    (c) 2014 SUSE Linux Products GmbH
#               (c) 2015-2026 SUSE LLC
#
# An example usage:
#      See usage() function below for more details...
#
# OCF instance parameters:
#   OCF_RESKEY_SID
#   OCF_RESKEY_InstanceNumber
#   OCF_RESKEY_DIR_EXECUTABLE   (optional, well known directories will be searched by default)
#
# HANA must support the following commands:
#     hdbnsutil -sr_stateConfiguration
#     hdbnsutil -sr_takeover
#     hdbnsutil -sr_register
#     landscapeHostConfiguration.py
#     systemReplicationStatus.py
#
#######################################################################
SAPHanaTopologyVersion="1.3.0"
#
# Initialization:
timeB=$(date '+%s')

raType="sht_"
OCF_FUNCTIONS_DIR="${OCF_FUNCTIONS_DIR:-${OCF_ROOT}/lib/heartbeat}"
# shellcheck source=/dev/null
source "${OCF_FUNCTIONS_DIR}/ocf-shellfuncs"
# TODO PRIO1: NG - adjust the correct path and file name for the library to be sourced for first testing use current directory
# shellcheck source=./ra/saphana-common-lib
source /usr/lib/SAPHanaSR-angi/saphana-common-lib
# shellcheck source=./ra/saphana-topology-lib
source /usr/lib/SAPHanaSR-angi/saphana-topology-lib

#
#######################################################################
#
# KNOWN PROBLEMS TO BE FIXED:

#
# function: main - main function to operate
# params:   ACTION
# globals:  OCF_*(r), CLACT(w), ra_rc(rw), $0(r)
#


if [ "$#" != "1" ]
then
    "${raType}"usage
    exit "$OCF_ERR_ARGS"
fi

ACTION="$1"
if [ "$ACTION" = "status" ]; then
    ACTION=monitor
fi

if is_clone
then
    CLACT=_clone
else
    if [[ "$ACTION" = "promote" || "$ACTION" = "demote" ]]
    then
        super_ocf_log err "ACT: $ACTION called in a non clone environment"
        exit "$OCF_ERR_ARGS"
    fi
fi

# These operations don't require OCF parameters to be set
case "$ACTION" in
    usage|methods)  "${raType}""$ACTION"
                    exit "$OCF_SUCCESS";;
    meta-data)      "${raType}"meta_data
                   exit "$OCF_SUCCESS";;
    notify)         # just ignore
                    exit "$OCF_SUCCESS";;
    *)
                    super_ocf_log info "RA ==== begin action $ACTION$CLACT ($SAPHanaTopologyVersion) ===="
                    core_init "$SAPHanaTopologyVersion" "$timeB"
                    ;;
esac
"${raType}"init

if ! ocf_is_root
then
    super_ocf_log err "ACT: $0 must be run as root"
    exit "$OCF_ERR_PERM"
fi

# parameter check
if  [ -z "$OCF_RESKEY_SID" ]
then
    super_ocf_log err "ACT: Please set parameter SID!"
    exit "$OCF_ERR_ARGS"
fi

if  [ -z "$OCF_RESKEY_InstanceNumber" ]
then
    super_ocf_log err "ACT: Please set parameter InstanceNumber!"
    exit "$OCF_ERR_ARGS"
fi

ra_rc="$OCF_ERR_UNIMPLEMENTED"
case "$ACTION" in
    start|stop|monitor) # Standard controlling actions
        "${raType}""$ACTION$CLACT"
        ra_rc="$?"
        ;;
    validate-all)
        "${raType}"validate
        ra_rc="$?"
        ;;
    reload)
        ra_rc="$OCF_SUCCESS"
        ;;
    *)  # seems to be an unknown request
        "${raType}"methods
        ra_rc="$OCF_ERR_UNIMPLEMENTED"
        ;;
esac
timeE="$(date '+%s')"
(( timeR = timeE - timeB ))
set_g_times
super_ocf_log info "RUNTIME ==== end action $ACTION$CLACT with rc=${ra_rc} ($SAPHanaTopologyVersion) (${timeR}s)===="
super_ocf_log info "RA ==== end action $ACTION$CLACT with rc=${ra_rc} ($SAPHanaTopologyVersion) (${timeR}s; times=$g_time_proc $g_time_chld)===="
exit "${ra_rc}"
# set ts=4 sw=4 sts=4 et
