#!/bin/sh
#######################################################
#
# automate-020-install-clsw
# to be called on the landscape 'master' 
# installing pattern ha_sles
# installing current RA rpms
#
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016-2017 SUSE Linux GmbH, Nuremberg, Germany
#
set -u

source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

GROUP="all"
nodeNr="all"
force=0

while [ $# -gt 0 ]; do
    case "$1" in
        --group=* ) GROUP=${1#--group=}
                   ;;
        --nodeNr=* ) nodeNr=${1#--nodeNr=}
                   ;;
        --node=* ) nodeNr=${1#--node=}
                   ;;
        --force )
echo "====== FORCE ========"
                   force=1
                   ;;
    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
    VM=${VM#*=}
    OWNER=${OWNER#*=}
    echo "dbg: $system VM=${VM}"
    if [ $GROUP = "all" -a nodeNr="all" ]; then
        create=1;
    elif [ $GROUP = "$OWNER" -a nodeNr="all" ]; then
        create=1;
    elif [ $GROUP = "all" -a nodeNr="$VM" ]; then
        create=1;
    elif [ $GROUP = "$OWNER" -a nodeNr="$VM" ]; then
        create=1;
    fi

    if [ $create -eq 1 ]; then
        ssh $VM zypper --non-interactive ref
        ssh $VM zypper --non-interactive up
        ssh $VM zypper --non-interactive install -t pattern ha_sles
        # TODO
        #ssh $VM zypper --non-interactive install $LandscapeRoot/rpm/ClusterTools2-2.5.3-16.1.noarch.rpm
        #ssh $VM zypper --non-interactive install $LandscapeRoot/rpm/SAPHanaSR.rpm
        #ssh $VM zypper --non-interactive install $LandscapeRoot/rpm/SAPHanaSR-doc.rpm
    else
        echo "dbg: create=$create"
    fi
done
