# This guide is optimized for Vagrant 1.7 and above.
# Although versions 1.6.x should behave very similarly, it is recommended
# to upgrade instead of disabling the requirement below.
Vagrant.require_version ">= 1.7.0"

mull_version = %x(git describe --tags --dirty | tr -d '\n')

def configure_vm(name, config)
  config.vm.provider "virtualbox" do |v|
    v.memory = 8192
    v.cpus = 4
    v.name = "mull-#{name}"
  end
end

Vagrant.configure(2) do |config|

  config.vm.define "debian" do |cfg|
    cfg.vm.box = "debian/stretch64"
    cfg.ssh.insert_key = false

    configure_vm("debian", cfg)

    cfg.vm.provision "ansible" do |ansible|
      ansible.verbose = "v"
      ansible.playbook = "debian-playbook.yaml"
      ansible.extra_vars = {
        llvm_version: ENV['LLVM_VERSION'] || '6.0.0',
        gitref: ENV['GITREF'] || 'main',
        mull_version: ENV['mull_version'] || "#{mull_version}"
      }
    end
  end

  config.vm.define "ubuntu18" do |cfg|
    cfg.vm.box = "ubuntu/bionic64"
    cfg.ssh.insert_key = false

    configure_vm("ubuntu18", cfg)

    cfg.vm.provision "shell", inline: "apt-get update && apt-get install -y python"

    cfg.vm.provision "ansible" do |ansible|
      ansible.verbose = "v"
      ansible.playbook = "ubuntu-playbook.yaml"
      ansible.extra_vars = {
        llvm_version: ENV['LLVM_VERSION'] || '10.0.0',
        gitref: ENV['GITREF'] || 'main',
        checkout: ENV['checkout'] || 'true',
        mull_version: ENV['mull_version'] || "#{mull_version}"
      }
    end
  end

  config.vm.define "ubuntu20.04" do |cfg|
    cfg.vm.box = "ubuntu/focal64"
    cfg.ssh.insert_key = false

    configure_vm("ubuntu20.04", cfg)

    cfg.vm.provision "shell", inline: "apt-get update && apt-get install -y python"

    cfg.vm.provision "ansible" do |ansible|
      ansible.verbose = "v"
      ansible.playbook = "ubuntu-playbook.yaml"
      ansible.extra_vars = {
        llvm_version: ENV['LLVM_VERSION'] || '12.0.0',
        gitref: ENV['gitref'] || 'main',
        checkout: ENV['checkout'] || 'true',
        mull_version: ENV['mull_version'] || "#{mull_version}"
      }
    end
  end

  config.vm.define "fedora" do |cfg|
    cfg.vm.box = "fedora/29-cloud-base"
    cfg.vm.box_version = "29.20181024.1"
    cfg.ssh.insert_key = false

    configure_vm("fedora", cfg)

    cfg.vm.provision "shell", inline: "yum install -y python"

    cfg.vm.provision "ansible" do |ansible|
      ansible.verbose = "v"
      ansible.playbook = "fedora-playbook.yaml"
      ansible.extra_vars = {
        llvm_version: ENV['LLVM_VERSION'] || '6.0.0',
        gitref: ENV['GITREF'] || 'main',
        mull_version: ENV['mull_version'] || "#{mull_version}"
      }
    end
  end

  config.vm.define "freebsd" do |cfg|
    cfg.vm.box = "freebsd/FreeBSD-11.2-STABLE"
    cfg.vm.box_version = "2019.04.18"
    cfg.ssh.insert_key = false
    cfg.vm.base_mac = "080027D14C66"
    cfg.ssh.shell = "sh"
    cfg.vm.synced_folder ".", "/vagrant", disabled: true

    configure_vm("freebsd", cfg)

    cfg.vm.provision "shell", inline: "pkg install -y python"

    cfg.vm.provision "ansible" do |ansible|
      ansible.verbose = "v"
      ansible.playbook = "freebsd-playbook.yaml"
      ansible.extra_vars = {
        llvm_version: ENV['LLVM_VERSION'] || '8.0.0',
        ansible_python_interpreter: '/usr/local/bin/python',
        make_program: 'gmake',
        gitref: ENV['GITREF'] || 'main',
        mull_version: ENV['mull_version'] || "#{mull_version}"
      }
    end
  end

  config.vm.define "macos" do |cfg|
    cfg.vm.box = "yzgyyang/macOS-10.14"
    cfg.vm.synced_folder ".", "/vagrant", disabled: true

    configure_vm("macOS", cfg)

    cfg.vm.provision "ansible" do |ansible|
      ansible.verbose = "v"
      ansible.playbook = "macos-playbook.yaml"
      ansible.extra_vars = {
        llvm_version: ENV['LLVM_VERSION'] || '8.0.0',
        gitref: ENV['GITREF'] || 'main',
        SDKROOT: '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk',
        mull_version: ENV['mull_version'] || "#{mull_version}"
      }
    end

  end

end
