# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'pathname'
BASE_URL          = 'http://dl.dropbox.com/u/13510779'
LAST_RELEASE_DATE = '2013-07-12'
LOCAL_BOXES_PATH  = Pathname('../boxes/output').expand_path
def lxc_box_url(release_name)
  file_name      = "lxc-#{release_name}-amd64-#{LAST_RELEASE_DATE}.box"
  local_box_file = LOCAL_BOXES_PATH.join(file_name)

  local_box_file.exist? ?
    local_box_file.to_s :
    "#{BASE_URL}/#{file_name}"
end

BOXES = {
  precise: {
    lxc_url:  lxc_box_url('precise'),
    vbox_url: 'http://files.vagrantup.com/precise64.box'
  },
  quantal: {
    lxc_url:  lxc_box_url('quantal'),
    vbox_url: 'https://github.com/downloads/roderik/VagrantQuantal64Box/quantal64.box'
  },
  raring:  {
    lxc_url:  lxc_box_url('raring'),
    vbox_url: 'http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box'
  },
  squeeze: {
    lxc_url:  lxc_box_url('squeeze'),
    # https://gist.github.com/henare/1964037
    vbox_url: 'http://dl.dropbox.com/u/174733/debian-squeeze-64.box'
  },
  wheezy:  {
    lxc_url:  lxc_box_url('wheezy'),
    vbox_url: 'http://puppet-vagrant-boxes.puppetlabs.com/debian-70rc1-x64-vbox4210.box'
  },
  sid: {
    lxc_url:  lxc_box_url('sid'),
  }
}

Vagrant.require_plugin 'vagrant-lxc'
Vagrant.require_plugin 'vagrant-cachier'
Vagrant.require_plugin 'vagrant-pristine'

Vagrant.configure("2") do |config|
  config.vm.synced_folder "../", "/vagrant", id: 'vagrant-root', nfs: true

  config.cache.scope = :machine
  config.cache.auto_detect = true
  config.cache.enable_nfs = true

  ip_suffix = 30
  BOXES.each do |box_name, box_config|
    config.vm.define(box_name.to_sym) do |vm_config|
      vm_config.vm.network :private_network, ip: "192.168.50.#{ip_suffix += 1}"
      vm_config.vm.box      = "#{box_name}64"

      if box_config[:vbox_url]
        vm_config.vm.provider :virtualbox do |vb, vb_config|
          vb_config.vm.box_url  = box_config[:vbox_url]
          vb_config.vm.hostname = 'vbox'

          vb.customize [
            "modifyvm", :id,
            "--memory", '1536',
            "--cpus", '2'
          ]
        end
      end

      if box_config[:lxc_url]
        vm_config.vm.provider :lxc do |lxc, lxc_config|
          lxc_config.vm.box_url  = box_config[:lxc_url]
          lxc_config.vm.hostname = 'lxc-dev-box'

          # Required to boot nested containers
          lxc.customize 'aa_profile', 'unconfined' unless %w(squeeze wheezy sid).include? box_name.to_s
        end
      end
    end
  end

  config.vm.provision :shell, :inline => 'sudo apt-get update'
  config.vm.provision :shell, :path   => 'shell-provisioning/upgrade-kernel'

  config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "."
    puppet.manifest_file  = "site.pp"
    puppet.options << [ '--verbose', '--debug' ]
  end
end