#!/bin/bash
#
#######################################################
#
# automate-01-install-sles
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016-2018 SUSE Linux GmbH, Nuremberg, Germany
#
# To be called at LandscapeMaster
#
# Tasks automated:
# - starts the VMs and send keycodes to start install 
#   from CD but configuring network for autoYast
# - installation of the VMs
#
# Preequisites:
# - VMs defined but *not* started
#
# Syntax:
# automate-01-install-sles [--owner=<owner>] [--node=<nodeName>] [--force]
#
# Next Automation Script
#
#######################################################
#
# Version 3.0.2018.03.21.1
#
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

export node="all"
reset_options=""
allSystems=""

while [ $# -gt 0 ]; do
    case "$1" in
        --group=* | --owner=* ) GROUP=${1#*=}
                   ;;
        --node=* ) node=${1#*=}
echo node: $node
                   ;;
        --force )
                   reset_options="${reset_options:+$reset_options }--force"
                   ;;
    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 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"
        #echo "rule 1" >&2
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "all" ]; then
        echo "$SYSTEM_SYSTEM"
        #echo "rule 2: node=$node " >&2
    elif [ $GROUP = "all" -a $node = "$SYSTEM_SYSTEM" ]; then
        echo "$SYSTEM_SYSTEM"
        #echo "rule 3" >&2
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "$SYSTEM_SYSTEM" ]; then
        echo "$SYSTEM_SYSTEM"
        #echo "rule 4" >&2
    fi
}

for system in "${LandscapeSystems[@]}"
do
    allSystems="$allSystems $(filterSystem $system)"
done
#reset_system ${reset_options} $allSystems
reset_system_pxe ${reset_options} $allSystems

