# -*- mode: ruby -*-
# vi: set ft=ruby sw=2 ts=2 :

# Set this to custom url if using a fork
cloudron_setup_url = 'https://git.cloudron.io/cloudron/box/raw/master/scripts/cloudron-setup'

Vagrant.configure(2) do |config|
  # Minimum requirement for Cloudron is 16.04
  config.vm.box = "ubuntu/xenial64"

  # Mosh is super nice, why not?
  enable_mosh = false
  if enable_mosh
      (60000..61000).each do |p|
          config.vm.network "forwarded_port", guest: p, host: p, protocol: 'udp'
      end
  end

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  # config.vm.network "forwarded_port", guest: 443, host: 8081

  config.vm.network "private_network", ip: "192.168.100.100"

  # Set hostname for the box
  config.vm.hostname = "cloudron.cloud"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "~/.ssh", "~/.ssh/vagrant_data"

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false
    # Customize the amount of memory on the VM:
    vb.memory = "2048"
  end

  # Virtual disk filepath
  config.vm.provider :virtualbox do |vb|
    # require 'pry'
    # binding.pry
    # disk_filepath = "#{ENV["HOME"]}/VirtualBox VMs/#{vb.name}/extended.vdi"
    disk_filepath = "extended.vdi"
    if ARGV[0] == "up" && ! File.exists?(disk_filepath)
      # Create vdi
      vb.customize [
        'createhd',
        '--filename', disk_filepath,
        '--format', 'VDI',
        # 20GB
        '--size', 20 * 1024
      ]

      vb.customize [
        'storageattach', :id,
        '--storagectl', 'SCSI',
        '--port', 2, '--device', 0,
        '--type', 'hdd',
        '--medium', disk_filepath
      ]

      # Run script to map new disk
      # TODO: This doesn't actually work to extend yet
      #config.vm.provision "shell", inline: <<-SHELL
      #  pvcreate /dev/sdb
      #  vgextend VolGroup /dev/sdb
      #  lvextend /dev/VolGroup/lv_root /dev/sdb
      #  resize2fs /dev/VolGroup/lv_root
      #SHELL
    end
  end

  # Set up Cloudron on provision. If using a fork, change this URL
  config.vm.provision "shell", inline: <<-SHELL
    wget #{cloudron_setup_url}
    chmod +x cloudron-setup
    ./cloudron-setup --domain cloudron.cloud --provider generic --tls-provider fallback --dns-provider noop
  SHELL
end