#!/bin/bash
### BEGIN BOOT SCRIPT INFO
#Long-Name: VirtualBox
### END BOOT SCRIPT INFO
### BEGIN BOOT SCRIPT INSTALLATION DEFAULTS
#Launch-Mode: async-gui-only
### END BOOT SCRIPT INSTALLATION DEFAULTS
#
#This script expects the VirtualBox configuration to be symlinked to config.vbox
#and adds it to VirtualBox's existing list of VMs. If there is already a VM
#with the same UUID registered, it will be unregistered before this VM is
#registered.

#unregister and re-register the VM in order to make sure that the stored
#absolute paths in the system-wide VM folder are correct
vm_uuid="`grep '<Machine.*uuid=' config.vbox | sed 's/.*\([0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}\).*/\1/'`"
if VBoxManage list vms | grep -q "$vm_uuid"
then
	VBoxManage unregistervm "$vm_uuid"
fi

VBoxManage registervm "`realpath config.vbox`"

VBoxManage startvm "$vm_uuid"
