#!/bin/sh
#
#######################################################
#
# automate-005-configure-saptune
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2023
# (c) 2023 SUSE Linux GmbH, Nuremberg, Germany
#
# To be called at LandscapeMaster
#
# Tasks automated:
# - configure saptune e.g. for HANA
#
# Preequisites:
# - VMs installed and ssh login configured
#
# Syntax:
# automate-005-configure-saptune [--owner=<owner> | --group=<group>] [--node=<nodeName>]
#
# Next Automation Script
#    TBD
#
#######################################################
#
# Version 3.0.2023.04.17.1
#
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

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 configure_saptune()
{
    local system=$1
    echo "configure_saptune $system"
    ssh -T root@$system <<EOF
    case "$LandscapeSLES" in
        SLE12* | SLES12* | SLE15* | SLES15* ) 
            # TODO: if sap-hana and sap-nw pattern installed then S4HANA-APP+DB
            if zypper se --installed-only -t pattern sap-hana; then
                st_solution="S4HANA-DBSERVER"
            else
                st_solution="S4HANA-APPSERVER"
            fi
            systemctl stop sapconf
            systemctl disable sapconf
            zypper --non-interactive rm sapconf; 
            systemctl enable saptune
            systemctl start saptune
            saptune revert all
            saptune solution apply "\$st_solution"
        ;;
    esac
EOF
}

# select hyp,system from LandscapeSystems where ...

for systemDef in "${LandscapeSystems[@]}"
do
    help1=$(filterSystem --fields 'SYSTEM' --system "$systemDef"; printf "\n")
    if [ ${#allSystems[@]} -gt 0 ]; then
        allSystems=( "${allSystems[@]}" "$help1" )
    else 
        allSystems=( "$help1" )
    fi
done

for s in "${allSystems[@]}"; do
    read sys X <<< "$s"
    if [ -n "$sys" ]; then
        configure_saptune "$sys"
    fi
done
