#!/bin/sh
#######################################################
#
#  automate-029-crm-bs
# to be called on the landscape 'master' 
# init cluster on first node
#
#
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016-2018 SUSE Linux GmbH, Nuremberg, Germany
#
set -u

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

GROUP="all"
node="all"
force=0

FILES=$LandscapeIn/automate-SAPHanaSR-scaleOut/files


while [ $# -gt 0 ]; do
    case "$1" in
        --group=* ) GROUP=${1#--group=}
                   ;;
        --node=* ) node=${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 ""
fi


function do_cluster()
{
    local first_node="$1"



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

#
# For SAPHanaSR we iterate over Lanscape variable LandscapeSAPHanaSR KV-pair sys_site1=<system-name>
#
function filterCluster()
{
    local cluster="$@"
    local clusterKV
    for clusterKV in ${cluster}; do
        cKey="${clusterKV%=*}"; cKey=${cKey^^}
        cValue=${clusterKV#*=}
        local CLUSTER_$cKey=$cValue
    done
    # BAUSTELLE
    if [ "$GROUP" = "all" -a "$node" = "all" ]; then
        echo "$CLUSTER_SYS_SITE1"
        #echo "rule 1" >&2
    elif [ "$GROUP" = "$CLUSTER_OWNER" -a $node = "all" ]; then
        echo "$CLUSTER_SYS_SITE1"
        #echo "rule 2: node=$node " >&2
    elif [ "$GROUP" = "all" -a "$node" = "$CLUSTER_SYS_SITE1" ]; then
        echo "$CLUSTER_SYS_SITE1"
        #echo "rule 3" >&2
    elif [ "$GROUP" = "$CLUSTER_OWNER" -a "$node" = "$CLUSTER_SYS_SITE1" ]; then
        echo "$CLUSTER_SYS_SITE1"
        #echo "rule 4" >&2
    fi
}

#
# For SAPHanaSR we iterate over Lanscape variable LandscapeSAPHanaSR KV-pair sys_site1=<system-name>
#
for cluster in "${LandscapeSAPHanaSR[@]}"
do
    allClusters="$allClusters $(filterCluster $cluster)"
done

for aCluster in ${allClusters}; do
    do_cluster "${aCluster}"
done
