#!/bin/bash

new_machinefolder="/tmp/deployed"
machinename="testMachine"

vboxmanage setproperty machinefolder "$new_machinefolder"

echo -e "import\n"

vboxmanage import "/tmp/test.ova" --vsys 0 --vmname "$machinename" --cpus 1 --memory 2048 --unit 10 --disk "$new_machinefolder/$machinename/disk1.vmdk" --unit 11 --disk "$new_machinefolder/$machinename/disk2.vmdk" --unit 12 --disk "$new_machinefolder/$machinename//disk3.vmdk"

echo -e "\n\ncreatehd\n"

vboxmanage createhd --filename "$new_machinefolder/$machinename//new_disk3.vdi" --size 16384 --format VDI

echo -e "\n\nstorageattach\n"

vboxmanage storageattach "$machinename" --storagectl IDEController --port 1 --device 0 --type hdd --medium "$new_machinefolder/$machinename//new_disk3.vdi"

echo -e "\n\nclosemedium\n"
# Fail:
# VBoxManage: error: Cannot register the hard disk '/tmp/deployed/testMachine/disk3.vmdk' {0780e5a2-1edc-4a0f-92b5-4cfdb475819b} because a hard disk '/tmp/deployed/testMachine//disk3.vmdk' with UUID {0780e5a2-1edc-4a0f-92b5-4cfdb475819b} already exists
# VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBox, interface IVirtualBox, callee nsISupports
# VBoxManage: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 178 of file VBoxManageDisk.cpp
vboxmanage closemedium disk "$new_machinefolder/$machinename//disk3.vmdk" --delete

# echo -e "\n\nunregister\n"

# vboxmanage unregistervm "$machinename" --delete > /dev/null

echo -e "\n\ndone\n"

vboxmanage setproperty machinefolder "$HOME/VirtualBox VMs/"
