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

this_dir = File.dirname(__FILE__) + "/"
require this_dir + "vagrant/hostmaster.rb"

Vagrant::Config.run do |config|

  # define some colors for our output
  def colorize(text, color_code) "#{color_code}#{text}\033[0m" end
  def red(text); colorize(text, "\033[31m"); end
  def green(text); colorize(text, "\033[32m"); end
  def yellow(text); colorize(text, "\033[33m"); end

  Dir.chdir this_dir

  if ARGV.include?("up") || ARGV.include?("provision")
    unless File.exists?(this_dir + "composer.phar")
      `curl -s https://getcomposer.org/installer | php`
      (puts red("Getting composer failed. Exiting..."); exit) unless $?.success?
    else
      IO.popen("php composer.phar self-update")
    end
    file = this_dir + "app/config/parameters.yml"
    FileUtils.cp(file + ".dist", file) unless File.exists?(file)
    puts "Running composer install..."
    IO.popen("php composer.phar install --dev --prefer-source") { |handle| handle.each { |line| puts line } }
    (puts red("Composer install failed. Exiting..."); exit) unless $?.success?
  end

  config.vm.box = "precise32"
  config.vm.customize ["modifyvm", :id, "--memory", 768]

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  # Boot with a GUI so you can see the screen. (Default is headless)
  # config.vm.boot_mode = :gui

  # NFS only necessary if performance too slow
  config.vm.share_folder "v-root", "/vagrant", "." , :nfs => true
  # whithout this symlinks can't be created on the shared folder
  config.vm.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]

  # the hostmaster plugin populates /etc/hosts
  config.vm.host_name = "symfony.lo"
  config.hosts.aliases = %w(elastic.lo)

  # the ip address where the vm can be accessed from the host
  config.vm.network :hostonly, "172.172.172.172"

  # chef solo configuration
  config.vm.provision :chef_solo do |chef|

    chef.cookbooks_path = "./"
    # chef debug level, start vagrant like this to debug:
    # $ CHEF_LOG_LEVEL=debug vagrant <provision or up>
    chef.log_level = ENV['CHEF_LOG'] || "info"

    # chef recipes/roles
    chef.add_recipe("vagrant")
  end
end