#! /bin/bash
# vim: shiftwidth=4 tabstop=4 expandtab

if in_array "${COMMAND_ARGS[0]}" --help -h help; then
    echo "$COMMAND [VM] [target hypervisor] [stop-before]"
    if [ "${COMMAND_ARGS[1]}" == "details" ]; then
        echo "  Migrate a VM from one hypervisor to another"
        echo "    stop-before      Stop the VM before migration (instead of live-migration)"
    fi
    return
fi

VM=${COMMAND_ARGS[0]}
[[ -z "$VM" ]] && usage "VM name is missing"

HYP=$( locate "$VM" )
check_hyp "$HYP" || usage "Unknown VM $VM"

is_running "$VM" "$HYP" || fatal_error "VM $VM is not running, can't migrate it."

TARGET_HYP=${COMMAND_ARGS[1]}
[[ -z "$TARGET_HYP" ]] && usage "Target hypervisor name is missing"
check_hyp "$TARGET_HYP" || usage "Invalid target hypervisor name"

if [[ "$TARGET_HYP" == "$HYP" ]]; then
    echo "VM $VM is already on $HYP"
    exit 0
fi

if [ "${COMMAND_ARGS[2]}" == "stop-before" ]; then
    confirm "Stop VM $VM on $HYP and restart it on $TARGET_HYP" || exit 0
    if [[ $AUTO -eq 0 ]] && confirm "Do you want to manually shutdown the virtual machine"; then
        echo -n "Please shutdown the virtual machine and type [enter] to continue ..."
        read -r
        wait_stopped "$VM" "$HYP"
    else
        stop_and_wait_stopped "$VM" "$HYP"
    fi

    echo -n "Dump virtual machine configuration on $HYP..."
    TMP=$( mktemp )
    ssh "$HYP" "$REAL_VIRSH" dumpxml --inactive "$VM" > "$TMP" || \
        fatal_error "Failed to dump VM $VM configuration"
    echo " done."

    echo -n "Copy virtual machine configuration on $TARGET_HYP..."
    run_external scp -q "$TMP" "$TARGET_HYP":"$TMP" || \
        fatal_error "Failed to copy VM $VM configuration on $TARGET_HYP."
    echo " done."

    echo "Define virtual machine on $TARGET_HYP..."
    run_external ssh "$TARGET_HYP" "$REAL_VIRSH" define "$TMP" || \
        fatal_error "Failed to define VM $VM on $TARGET_HYP."

    echo "Start virtual machine on $TARGET_HYP..."
    run_external ssh "$TARGET_HYP" "$REAL_VIRSH" start "$VM" || \
        fatal_error "Failed to start VM $VM on $TARGET_HYP."

    echo "Undefine virtual machine on $HYP..."
    run_external ssh "$HYP" "$REAL_VIRSH" undefine "$VM" || fatal_error "Failed to undefine VM $VM."

    if [[ -z "$GENERATE_SCRIPT" ]]; then
        echo -n "Remove temporary files..."
        rm -f "$TMP"
        ssh "$TARGET_HYP" rm -f "$TMP"
        echo " done."
    else
        echo "rm -f $TMP" >> "$GENERATE_SCRIPT"
        echo "ssh $TARGET_HYP rm -f $TMP" >> "$GENERATE_SCRIPT"
    fi

    echo
    echo "Virtual machine $VM successfully migrated from $HYP on $TARGET_HYP"
    clean_cache
else
    confirm "Live migrate VM $VM from $HYP to $TARGET_HYP" || exit 0
    run_external ssh "$HYP" \
        "$REAL_VIRSH" migrate \
            --live \
            --persistent \
            --undefinesource \
            --verbose \
            "$VM" \
            "qemu+ssh://$TARGET_HYP/system" \
            "tcp://$TARGET_HYP" || \
        fatal_error "Failed to live migrate VM $VM from $HYP to $TARGET_HYP."
    clean_cache
fi
