#!/bin/bash
#######################################################
#
# automate-010-install-hana
# to be called on the landscape 'master' 
# installing saphostagent
# installing sap hana
# configuring system replication
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016-2018 SUSE Linux GmbH, Nuremberg, Germany
#
# Preequisites:
# - VMs installed with SLES
#
# Syntax:
# automate-010-install-hana [--group=<group>] [--node=<system>] ...
# Next Automation Script
# 
#
#######################################################
#
set -u

logFile="/var/log/automate-010-install-hana.$$"
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

function usage()
{
    cat <<EOF
automate-010-install-hana [--group=GROUP] \\
                          [--all | --primary | --secondary] {[--step=STEP|STEPS]}

GROUP name of the training group/owner which systems needs to be reset
      (like 1, fh, demo, admin, ...) the grou must match the systems
      list in the landscape configuration
      [--all | --primary | --secondary] 
STEP  Multiple SAP HANASR steps could be specified by
      all|uninstall|install|enable|register|backup ...
      (TODO start|stop|configure_hooks|configure_single_nameserver)
      all is default and means install enable register
STEPS Colon (,) separated list of STEPs (STEP1,STEP2,STEP3 )
EOF
}

GROUP=""
node="all"
fh_reset_param=""

while [ $# -gt 0 ]; do
    case "$1" in
        --help | -h ) usage
                      exit
                      ;;
        --group=* ) GROUP=${1#--group=}
                   ;;
        --owner=*) GROUP=${1#--owner=}
                   ;;
        --primary | --secondary | --all | --step=* )
                   fh_reset_param="$fh_reset_param${fh_reset_param:+ }$1" 
                   ;;
        * )        echo "parameter $1 unknown"
                   ;;
    esac
    shift
done

if [[ -z "$GROUP" ]]; then
    echo "You must specify a group of systems using parameter --group=GROUP"
    exit 2
fi

#
# QUIET MODE
#

exec 3>&1
if true; then
    exec 1>$logFile
    exec 2>$logFile
fi

echo "$(date) == Logs are written to $logFile" >&3

export FILES="$LandscapeRoot/files"

#
# 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

echo "prim=$prim"

export TAG="fh_reset_ScaleUP"
export HANA_INST_DIR="$LandscapeSAPHANAinstDir"

#ssh root@$prim zypper --non-interactive in "$LandscapeRoot/rpm/saphostagent.rpm"
#ssh root@$secn zypper --non-interactive in "$LandscapeRoot/rpm/saphostagent.rpm"
echo "$(date) == Starting fh_reset_Scale-Up $fh_reset_param" >&3
fh_reset_Scale-Up $fh_reset_param


# TODO: PRIO 1: Param-Section of bin/fh_reset_ScaleUP
# TODO: PRIO 2: Tale sql-pwd out of the pwd file instead of hard coded


echo "rc=$?"
echo "Done... "
echo ""
