#!/bin/bash
#######################################################
#
# automate-030-crm-sphanasr
# to be called on the landscape 'master' 
# init cluster on first node
#
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2017-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/files

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 ""
fi


function do_cluster()
{
    local rest=$1
    local first_node="${rest%%:*}"
    rest="${rest#*:}"
    local SID="${rest%%:*}"; rest="${rest#*:}"
    local INO="${rest%%:*}"; rest="${rest#*:}"
    local SRVIP="${rest%%:*}"; rest="${rest#*:}"

# TODO: NET_IF and SBD must NOT be static

if [ -n "$first_node" ]; then
	case "$LandscapeSLES" in
	    SLES11* | 11* )
	;;
	    SLES12* | 12* )
		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
        ;;
	    SLES15* | 15* )
		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
        ;;
        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:$CLUSTER_SID:$CLUSTER_INST:$CLUSTER_IP"
        #echo "rule 1" >&2
    elif [ "$GROUP" = "$CLUSTER_OWNER" -a $node = "all" ]; then
        echo "$CLUSTER_SYS_SITE1:$CLUSTER_SID:$CLUSTER_INST:$CLUSTER_IP"
        #echo "rule 2: node=$node " >&2
    elif [ "$GROUP" = "all" -a "$node" = "$CLUSTER_SYS_SITE1" ]; then
        echo "$CLUSTER_SYS_SITE1:$CLUSTER_SID:$CLUSTER_INST:$CLUSTER_IP"
        #echo "rule 3" >&2
    elif [ "$GROUP" = "$CLUSTER_OWNER" -a "$node" = "$CLUSTER_SYS_SITE1" ]; then
        echo "$CLUSTER_SYS_SITE1:$CLUSTER_SID:$CLUSTER_INST:$CLUSTER_IP"
        #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
