#!/bin/bash
#######################################################
#
# automate-030-crm-sphanasr
# to be called on one node only - could also be called on the hypervisor
# creating cluster config for the SAP HANA SR instance pair
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2017 SUSE Linux GmbH, Nuremberg, Germany
#
#
# Tasks automated:
# - starts the VMs and send keycodes to start install 
#   from CD but configuring network for autoYast
# - installation of the VMs
#
# Preequisites:
# - VMs installed with SLES
# - cluster basics configured and crm bootstrap tuned
#
# Syntax:
# automate-030-crm-saphanasr [--group=<group NR>] 
# Next Automation Script
# 
#
#######################################################
#
set -u
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

FILES=$LandscapeRoot/files
PATH="$PATH:$LandscapeRoot/bin"

function usage()
{
    cat <<EOF
automate-030-crm-saphanasr [--group=GROUP] 

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
EOF
}

nodeNr="all"
fh_reset_param=""

while [ $# -gt 0 ]; do
    case "$1" in
        --help | -h ) usage
                      exit
                      ;;
        --group=* ) GROUP=${1#--group=}
                   ;;
        --nodeNr=* ) nodeNr=${1#--nodeNr=}
                   ;;
        --node=* ) nodeNr=${1#--node=}
                   ;;
        --fh_reset=* ) fh_reset_param=${1#--fh_reset=}
                   echo fh_reset_param=$fh_reset_param
                   ;;
        * )        echo "parameter $1 unknown"
                   ;;
    esac
    shift
done

if [ -z "$GROUP" ]; then
	echo "Welcome... I am just doing some SLES automation tasks"
	echo "If any question comes along during the auto installation process, just say 'yes'"
	echo ""
	echo -n "Please enter your group number: "
	read GROUP
	echo ""
	echo "Your group number is $GROUP"
	echo "Press <enter>"
    read
	echo ""
	echo "Installing SAP HOSTAGENT and SAP HANA on both nodes..."
	echo ""
fi
echo "Doing tasks for group $GROUP ..."
echo ""
nodes=""
for system in "${LandscapeSystems[@]}"
do
    create=0
    read hypervisor VM OWNER REST <<< $system
    #
    # TODO: currently only one group per call is supported
    #
    if [ $GROUP = "all" -a nodeNr="all" ]; then
        create=1;
    elif [ $GROUP = "$OWNER" -a nodeNr="all" ]; then
        nodes="$nodes $VM"
        create=1;
    elif [ $GROUP = "all" -a nodeNr="$VM" ]; then
        nodes="$nodes $VM"
        create=1;
    elif [ $GROUP = "$OWNER" -a nodeNr="$VM" ]; then
        nodes="$nodes $VM"
        create=1;
    fi
    read first_node other_nodes <<< $nodes
done

for record in "${LandscapeSAPHanaSR[@]}"
do
    read owner xSID xINO serviceIP replicationMode operationMode databaseType primary secondary primSite secSite X <<< $record
    if [ "$owner" = "$GROUP" ]; then
        export prim=$primary
        export secn=$secondary
        export SID=$xSID
        export sid="${SID,,}"
        export ino=$xINO
        export SRVIP=$serviceIP
        export primSite=$primSite
        export secnSite=$secSite
    fi
done

#export TAG="fh_reset_ScaleUP"
# TODO: ?? /hana_inst/SCT_workshop/SAP-MEDIA/51050308/DATA_UNITS/HDB_SERVER_LINUX_X86_64"
#export HANA_INST_DIR="$LandscapeSAPHANAinstDir"

echo "SID=$SID INO=$ino SRVIP=$SRVIP"

sed -e "s/@@SID@@/$SID/g" \
    -e "s/@@INO@@/$ino/g" \
    -e "s/@@SRVIP@@/$SRVIP/g" \
    $FILES/crm-saphanasr.template.SLES12.SCTSR.txt > /tmp/crm-saphanasr_${SID}_HDB${ino}.txt
rsync /tmp/crm-saphanasr_${SID}_HDB${ino}.txt root@$first_node:.
ssh $first_node /usr/sbin/crm configure load update crm-saphanasr_${SID}_HDB${ino}.txt

#ssh root@$prim zypper --non-interactive in "$LandscapeRoot/rpm/saphostagent.rpm"
#ssh root@$secn zypper --non-interactive in "$LandscapeRoot/rpm/saphostagent.rpm"
#bash 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 ""
echo "Done... "
echo ""
