ENV['VAGRANT_DEFAULT_PROVIDER'] = 'google'

require 'fog/version'

Vagrant.configure('2') do |config|
  config.vm.box      = 'gce'
  config.vm.hostname = "#{data['vm']['hostname']}"

  config.vm.provider :google do |google, override|
    google.google_project_id      = "#{data['vm']['provider']['gce']['project_id']}"
    google.google_client_email    = "#{data['vm']['provider']['gce']['client_email']}"
    google.google_key_location    = "#{data['vm']['provider']['gce']['key_location']}"
    google.machine_type           = "#{data['vm']['provider']['gce']['machine_type']}"
    google.image                  = "#{data['vm']['provider']['gce']['image']}"
    google.zone                   = "#{data['vm']['provider']['gce']['zone']}"
    google.name                   = "#{data['vm']['provider']['gce']['name']}"
    google.tags                   = data['vm']['provider']['gce']['tags']

    override.ssh.username         = "#{data['ssh']['username']}"
    override.ssh.private_key_path = "#{data['ssh']['private_key_path']}"
  end

  data['vm']['synced_folder'].each do |i, folder|
    if folder['source'] != '' && folder['target'] != ''
      config.vm.synced_folder "#{folder['source']}", "#{folder['target']}", id: "#{i}"
    end
  end

  config.vm.provision 'shell' do |s|
    s.path = 'puphpet/shell/initial-setup.sh'
    s.args = '/vagrant/puphpet'
  end
  config.vm.provision :shell, :path => 'puphpet/shell/check-puppet-modules.sh'
  config.vm.provision :shell, :path => 'puphpet/shell/install-ruby.sh'
  config.vm.provision :shell, :path => 'puphpet/shell/install-puppet.sh'

  config.vm.provision :puppet do |puppet|
    ssh_username = !data['ssh']['username'].nil? ? data['ssh']['username'] : 'vagrant'
    puppet.facter = {
      'ssh_username'     => "#{ssh_username}",
      'provisioner_type' => ENV['VAGRANT_DEFAULT_PROVIDER'],
    }
    puppet.manifests_path = "#{data['vm']['provision']['puppet']['manifests_path']}"
    puppet.manifest_file  = "#{data['vm']['provision']['puppet']['manifest_file']}"
    puppet.module_path    = "#{data['vm']['provision']['puppet']['module_path']}"

    if !data['vm']['provision']['puppet']['options'].empty?
      puppet.options = data['vm']['provision']['puppet']['options']
    end
  end

  config.vm.provision :shell do |s|
    s.path = 'puphpet/shell/execute-files.sh'
    s.args = ['exec-once', 'exec-always']
  end
  config.vm.provision :shell, run: 'always' do |s|
    s.path = 'puphpet/shell/execute-files.sh'
    s.args = ['startup-once', 'startup-always']
  end

  config.vm.provision :shell, privileged: false do |s|
    s.path = 'puphpet/shell/execute-files.sh'
    s.args = ['exec-once-unprivileged', 'exec-always-unprivileged']
  end
  config.vm.provision :shell, run: 'always', privileged: false do |s|
    s.path = 'puphpet/shell/execute-files.sh'
    s.args = ['startup-once-unprivileged', 'startup-always-unprivileged']
  end

  config.vm.provision :shell, :path => 'puphpet/shell/important-notices.sh'

  if !data['ssh']['host'].nil?
    config.ssh.host = data['ssh']['host']
  end
  if !data['ssh']['port'].nil?
    config.ssh.port = "#{data['ssh']['port']}"
  end
  if !data['ssh']['guest_port'].nil?
    config.ssh.guest_port = data['ssh']['guest_port']
  end
  if !data['ssh']['shell'].nil?
    config.ssh.shell = "#{data['ssh']['shell']}"
  end
  if !data['ssh']['keep_alive'].nil?
    config.ssh.keep_alive = data['ssh']['keep_alive']
  end
  if !data['ssh']['forward_agent'].nil?
    config.ssh.forward_agent = data['ssh']['forward_agent']
  end
  if !data['ssh']['forward_x11'].nil?
    config.ssh.forward_x11 = data['ssh']['forward_x11']
  end

end

