#!/bin/bash
#
#######################################################
#
# automate-00-destroyDisks
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016-2018 SUSE Linux GmbH, Nuremberg, Germany
#
# To be called at LandscapeMaster
#
# Tasks automated:
# - creates the XML files for autoyast and kvm
# - creates the (stopped) VMs in kvm
#
# Preequisites:
# - both hypervisors (hana-01 and hana-02 up and running)
# - dhcp on br0 (userlan)
# - nating on br0 (userlan)
# - VMs are *not* already available
#
# Syntax:
# automate-00-createDisks [--owner=<owner>] [--node=<nodeName>] [--force]
#
# Next Automation Script
# automate-01-install-sles
#
#######################################################
#
# Version 3.0.2018.03.21.1
#
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

GROUP="all"
node="all"
force=""
allSystems=""

while [ $# -gt 0 ]; do
    case "$1" in
        --group=* | --owner=* ) GROUP=${1#*=}
                   ;;
        --node=* ) node=${1#*=}
                   ;;
        --force )
                   force="--force"
                   ;;
    esac
    shift
done

function filterSystem()
{
    local system="$@"
    local systemKV
    for systemKV in ${system}; do
        sKey="${systemKV%=*}"; sKey=${sKey^^}
        sValue=${systemKV#*=}
        local SYSTEM_$sKey=$sValue
    done
    # BAUSTELLE
    if [ $GROUP = "all" -a $node = "all" ]; then
        echo "$SYSTEM_SYSTEM"
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "all" ]; then
        echo "$SYSTEM_SYSTEM"
    elif [ $GROUP = "all" -a $node = "$SYSTEM_SYSTEM" ]; then
        echo "$SYSTEM_SYSTEM"
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "$SYSTEM_SYSTEM" ]; then
        echo "$SYSTEM_SYSTEM"
    fi
}

for system in "${LandscapeSystems[@]}"
do
    allSystems="${allSystems}${allSystems:+ }$(filterSystem $system)"
done
if [ -n "$allSystems" ]; then
    createDisks --destroy $force $allSystems
fi
