# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-20.04"

  Dir.mkdir 'vagrant' unless File.exists? 'vagrant'
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.vm.synced_folder "vagrant", "/vagrant"


  if ['up', 'reload'].member? ARGV[0]
    image = ENV['IMAGE']
    if image === nil
      puts "Usage: IMAGE='path/to/snapshot.img' vagrant #{ARGV.join( ' ' )}"
      exit(1)
    elsif image.end_with? '.img'
      vdi = image.sub('.img', '.vdi')
      if ::File.exist? vdi
          puts "[WARN] A vdi image of #{image} already exists at #{vdi}, will use the existing #{vdi}"
      else
          system('VBoxManage', 'convertdd', image, vdi)
          raise Exception.new "failed to convert #{image} to a vdi" unless $?.success?
          puts "[INFO] Created a new VDI image of #{image} at #{vdi}"
      end
    end
  end

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', vdi]
  end

  config.vm.provision "shell", inline: <<-SHELL
    sudo mkdir /mnt/snapshot
    sudo mount -o ro /dev/sdb1 /mnt/snapshot
    echo 'cd /mnt/snapshot' >> ~vagrant/.profile
  SHELL
end
