#!/bin/bash
#######################################################
#
# createDisks
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016 SUSE Linux GmbH, Nuremberg, Germany
#
# 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
#
# Next Automation Script
# automate-01-install-sles
#
#######################################################
#
set -u

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

set +u
force=0
listOnly=0
allSystems=""
destroyOnly=0

while [ $# -gt 0 ]; do
    case "$1" in
        --force )
               echo "====== FORCE ========"
               force=1
               ;;
        --list )
               listOnly=1
               ;;
        --destroy )
               destroyOnly=1
               ;;
        * )
               allSystems="$allSystems $1"
               ;;
    esac
    shift
done

createOneDisk()
{
    local VM=$1 OWNER=$2 disk=$3
    for diskKV in ${disk}; do
        sKey="${diskKV%=*}"; sKey=${sKey^^}
        sValue=${diskKV#*=}
        local DISK_$sKey=$sValue
    done
    #echo DISK_STAR: ${!DISK_*}
    #set | grep '^DISK_'
    
    case "$DISK_USAGE" in
        vm ) dDir="vm_$VM";;
        owner ) dDir="owner_$OWNER";;
    esac
    # echo "$VM@$hypervisor: Path: $LandscapeVMDiskDirectory/$dDir/$DISK_NAME  Format: $DISK_TYPE  Size: $DISK_SIZE"
    # set -x
    theDisk="${LandscapeVMDiskDirectory}/${dDir}/${DISK_NAME}.${DISK_TYPE}"
    if [ "$listOnly" -eq 0 ]; then
        if [ "$SYSTEM_HYP" = "$HOSTNAME" ]; then
            echo "===== LOCAL ===="
            ssh_or_local="bash"
        else
            echo "===== REMOTE ($SYSTEM_HYP) ===="
            ssh_or_local="ssh -T root@$SYSTEM_HYP"
        fi
        if [ $force -eq 1 ]; then 
            $ssh_or_local <<EOF
                test -e $theDisk && rm -- $theDisk
EOF
        else
            if [ $destroyOnly -eq 1 ]; then 
                read -p "Delete disk $theDisk?" yn
                case $yn in
                    [Yy]* ) 
                        $ssh_or_local <<EOF
                            test -e $theDisk && rm -v -- $theDisk
EOF
                        ;;
                esac

            fi
        fi
        if [ $destroyOnly -eq 0  ]; then
            $ssh_or_local << EOF
                if [ -e $theDisk ]; then
                    echo "SKIP $theDisk"
                else
                    echo "CREATE $theDisk ($DISK_TYPE $theDisk $DISK_SIZE)"
                    mkdir -p $LandscapeVMDiskDirectory/$dDir
                    qemu-img create -f "$DISK_TYPE" "$theDisk" "$DISK_SIZE"
                fi
EOF
        fi
    else # listOnly
        echo $theDisk
    fi
}

createDiskForOneSystem()
{
    local thesystem=$1 system=$2
    for systemKV in ${system}; do
        sKey="${systemKV%=*}"; sKey=${sKey^^}
        sValue=${systemKV#*=}
        local SYSTEM_$sKey=$sValue
    done
    if [ $thesystem = $SYSTEM_SYSTEM ]; then

        if [ -n "$SYSTEM_TYPE" ]; then
            eval theDisks=(\"\${LandscapeDisks_${SYSTEM_TYPE}[@]}\")
        else
            theDisks=( "${LandscapeDisks[@]}" )
        fi
        for disk in "${theDisks[@]}"; do
            createOneDisk "$SYSTEM_SYSTEM" "$SYSTEM_OWNER" "$disk"
        done
    fi
}

for thesystem in $allSystems; do
    for system in "${LandscapeSystems[@]}"
    do
        createDiskForOneSystem $thesystem "$system"
    done
done
