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

# SRC - https://www.vagrantup.com/docs/multi-machine/

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

# Use rbconfig to determine if we're on a windows host or not.
require 'rbconfig'
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)

require 'yaml'

dir = File.dirname(File.expand_path(__FILE__))

configValues = YAML::load_file("#{dir}/puphpet/config.yaml")
apacheConfigValues = YAML::load_file("#{dir}/puphpet/ansible/apache.yml")
data = configValues['vagrantfile-local']

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # LOCAL DEV MACHINE
  # DEPLOY WITH vagrant up dev --debug
  config.vm.define "dev" do |config|
    config.vm.box = "#{data['vm']['box']}"      

    if data['vm']['hostname'].to_s != ''
      config.vm.hostname = "#{data['vm']['hostname']}"
    end
    
    config.vm.provider "virtualbox" do |provider, override|
      override.vm.provision "shell" do |s|
        s.path = "puphpet/shell/initial-setup.sh"
        s.args = "/vagrant/puphpet"
      end

      # Install ansible inside the box.
      override.vm.provision :shell, :path => "puphpet/shell/ansible.sh"

      # Install ansible playbooks inside the box.
      override.vm.provision :shell, :path => "puphpet/ansible/run-ansible-playbook.sh"

      # Install drupal within vm for testing.
      if !ENV['VAGRANT_CI'].nil?
        override.vm.provision :shell, :path => "puphpet/ansible/run-drupal-playbook.sh"
      end    
      
      # If hostsupdater plugin is installed, add all servernames as aliases.
      if Vagrant.has_plugin?("vagrant-hostsupdater")
        override.hostsupdater.aliases = []
        for host in apacheConfigValues[0]['vars']['apache_vhosts']
          # Add all the hosts that aren't defined as Ansible vars.
          unless host['servername'].include? "{{"
            override.hostsupdater.aliases.push(host['servername'])
          end
        end
      end

      if data['vm']['network']['private_network'].to_s != ''
        override.vm.network "private_network", ip: "#{data['vm']['network']['private_network']}", lxc__bridge_name: "lxcbr0"
      end

      data['vm']['network']['forwarded_port'].each do |i, port|
        if port['guest'] != '' && port['host'] != ''
          override.vm.network :forwarded_port, guest: port['guest'].to_i, host: port['host'].to_i, auto_correct: true
        end
      end

      override.vm.synced_folder "./", "/var/www"
      
      override.vm.usable_port_range = (10200..10500)

      if !data['vm']['provider']['virtualbox'].empty?        
        data['vm']['provider']['virtualbox']['modifyvm'].each do |key, value|
          if key == "natdnshostresolver1"
            value = value ? "on" : "off"
          end
          provider.customize ["modifyvm", :id, "--#{key}", "#{value}"]
        end        
      end

      override.ssh.username = "vagrant"
      override.ssh.password = "vagrant"
      override.ssh.shell = "sh"
      override.ssh.insert_key = "false"
      override.ssh.forward_agent = true
    end
  end


  # STAGING SERVER @ DIGITAL OCEAN DROPLET
  # DEPLOY WITH vagrant up staging --debug
  # SRC - https://github.com/devopsgroup-io/vagrant-digitalocean
  # List available regions with:  vagrant digitalocean-list regions $DIGITAL_OCEAN_TOKEN
  # List available sizes with:    vagrant digitalocean-list sizes $DIGITAL_OCEAN_TOKEN
  # List available images with:   vagrant digitalocean-list images $DIGITAL_OCEAN_TOKEN  
  config.vm.define "staging" do |config|
    config.vm.box = "#{data['vm']['box']}"      

    if data['vm']['hostname'].to_s != ''
      config.vm.hostname = "#{data['vm']['hostname']}"
    end
    
    config.vm.provider :digital_ocean do |provider, override|
          # This is akways needed but if the key is already used in the DO account
          # you *need* to set the provider.ssh_key_name parameter.
          override.ssh.private_key_path = '~/.ssh/id_rsa'
          override.vm.box = 'digital_ocean'
          override.vm.box_url = "https://github.com/devopsgroup-io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
          override.nfs.functional = false          
          override.vm.allowed_synced_folder_types = :rsync  # fix for SMB error wehn deploying from Mac
          override.vm.synced_folder "./", "/var/www"
          provider.token = '976c69ac483e3518b0e18d5d837657974c4d37af95a170f9010223c1d729714e'
          provider.image = 'ubuntu-14-04-x64'
          provider.region = 'nyc3'
          provider.size = '4gb'
          provider.monitoring = true
          provider.ssh_key_name = "Andrew Ash - applesauce"
          
          # Install ansible inside the box.
          override.vm.provision :shell, :path => "puphpet/shell/ansible.sh"

          # Install ansible playbooks inside the box.
          override.vm.provision :shell, :path => "puphpet/ansible/run-ansible-playbook.sh"

          # Install drupal within vm for testing.
          if !ENV['VAGRANT_CI'].nil?
            override.vm.provision :shell, :path => "puphpet/ansible/run-drupal-playbook.sh"
          end    
    end    
  end
end
