#!/bin/sh
#######################################################
#
#  automate-029-crm-bs
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016 SUSE Linux GmbH, Nuremberg, Germany
#
# To be called at primary node
#
# Syntax:
# automate-029-crm-bs [--group=<group NR>] [--nodeNr=1|2] 
# Next Automation Script
# 
#
#######################################################
#
set -u
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

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


while [ $# -gt 0 ]; do
    case "$1" in
        --group=* ) GROUP=${1#--group=}
                   ;;
        --nodeNr=* ) nodeNr=${1#--nodeNr=}
                   ;;
        --node=* ) nodeNr=${1#--node=}
                   ;;
    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
    # array_nodes=( x $nodes )
    read first_node other_nodes <<< $nodes
#
# Do some 'initial' things like sleha_init does...
#
done

if [ -n "$first_node" ]; then
	case "$LandscapeSLES" in
	    SLES11* | 11* )
        ;;
	    SLES12* | 12* )
            rsync $FILES/crm-bs.SLES12.Express.txt root@$first_node:.
            ssh $first_node /usr/sbin/crm configure load update crm-bs.SLES12.Express.txt
        ;;
    esac
fi
