#!/bin/sh
#
#######################################################
#
# automate-040-hana-configure-saphanasr-hook
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2022
# (c) 2022 SUSE LLC
#
# To be called at LandscapeMaster
#
# Tasks automated:
# - configure SAPHanaSR hook
#
# Preequisites:
# - VMs installed, SAPHanaSR SW and HANA installed
#
# Syntax:
#
# Next Automation Script
#
#######################################################
#
# Version 1.0.2022.03.24.1
#
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"
########### CHECK::: FILES=/data/SCT/LandscapeCore/automate-simple-stack/files

GROUP=""
node="all"
declare -a allSystems=()

while [ $# -gt 0 ]; do
    case "$1" in
        --group=* | --owner=* ) GROUP=${1#--group=}
                   ;;
        --node=* ) node=${1#--node=}
                   ;;
        --force )
                   reset_options="${reset_options:+$reset_options }--force"
                   ;;
    esac
    shift
done

function filterSystem()
{
    local systemDef="" systemKV=""
    local fields="SYSTEM"
    local f="" ptrF="" outPut=0 outStr=""
    while [ $# -gt 0 ]; do
        case "$1" in
            --system ) systemDef="$2"; shift
                       ;;
            --fields ) fields="$2"; shift
                       ;;
        esac
        shift
    done
    for systemKV in ${systemDef}; do
        sKey="${systemKV%=*}"; sKey=${sKey^^}
        sValue=${systemKV#*=}
        local SYSTEM_$sKey=$sValue
    done
    # BAUSTELLE
    if [ $GROUP = "all" -a $node = "all" ]; then
        outPut=1
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "all" ]; then
        outPut=1
    elif [ $GROUP = "all" -a $node = "$SYSTEM_SYSTEM" ]; then
        outPut=1
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "$SYSTEM_SYSTEM" ]; then
        outPut=1
    fi
    if [ $outPut -eq 1 ]; then
        for f in $fields; do
            ptrF="SYSTEM_$f"
            outStr="${outStr}${outStr:+ }${!ptrF}"
        done
        printf "%s" "$outStr"
    fi
}

function get_landscape_saphanasr()
{
    #
    # new LandscapeSAPHanaSR format:
    # "owner=1 sid=HA1 inst=10 ip=192.168.201.113 rmode=sync omode=logreplay dbtype=multiple_containers sys_site1=simple01 sys_site2=simple02 name_site1=WDF1 name_site2=ROT1
    #

    for record in "${LandscapeSAPHanaSR[@]}"
    do
        for saphanasrKV in ${record}; do
            echo "saphanasrKV: $saphanasrKV"
            srKey="${saphanasrKV%=*}"; srKey=${srKey^^}
            srValue=${saphanasrKV#*=}
            #local SAP_HANA_SR_$srKey=$srValue
            declare SAP_HANA_SR_$srKey=$srValue
        done
        if [ "$SAP_HANA_SR_OWNER" = "$GROUP" ]; then
            export prim=$SAP_HANA_SR_SYS_SITE1
            export secn=$SAP_HANA_SR_SYS_SITE2
            export SID=$SAP_HANA_SR_SID
            export sid="${SID,,}"
            export ino=$SAP_HANA_SR_INST
            export primSite=$SAP_HANA_SR_NAME_SITE1
            export secnSite=$SAP_HANA_SR_NAME_SITE2
            export primList=""
            export secnList=""
        fi
        echo "SAP_HANA_SR_STAR: ${!SAP_HANA_SR_*}"
        unset ${!SAP_HANA_SR_*}
        echo "SAP_HANA_SR_STAR: ${!SAP_HANA_SR_*}"
    done
}

function set_maintenance()
{
    local system="$1" SID="$2" sid="${SID,,}" ino="$3" maint="$4"
    ssh -T root@$system <<EOF
        crm resource maintenance "msl_SAPHana_${SID}_HDB${ino}" "${maint}"
EOF
}

function configure_glob_saphanasr()
{
    local system="$1" SID="$2" sid="${SID,,}"
    echo "configure_glob_saphanasr $system"
    ssh -T root@$system <<EOF
    #
    # GLOBAL.INI add SAPHANASR HOOK
    #
    (
        su - ${sid}adm HDB stop
        cat /usr/share/SAPHanaSR/samples/global.ini >> /hana/shared/"${SID}"/global/hdb/custom/config/global.ini 
        echo "${sid}adm ALL=(ALL) NOPASSWD: /usr/sbin/crm_attribute -n hana_${sid}_*" > /etc/sudoers.d/SAPHanaSR
        su - ${sid}adm HDB start
    ) 
EOF
}

get_landscape_saphanasr
echo "prim=$prim secn=$secn SID=$SID"


set_maintenance "$prim" "$SID" "$ino" "on"
configure_glob_saphanasr "$prim" "$SID"
configure_glob_saphanasr "$secn" "$SID"
set_maintenance "$prim" "$SID" "$ino" "off"
