ENV['VAGRANT_DEFAULT_PROVIDER'] = 'cloudstack'

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

  config.vm.provider :cloudstack do |cloudstack, override|

    cloudstack.host = "cloudstack.ikoula.com"
    cloudstack.path = "/client/api"
    cloudstack.port = "443"
    cloudstack.scheme = "https"

    cloudstack.api_key             = "#{data['vm']['provider']['cloudstack']['api_key']}"
    cloudstack.secret_key         = "#{data['vm']['provider']['cloudstack']['secret_key']}"
    cloudstack.keypair              = "#{data['vm']['provider']['cloudstack']['keypair']}"
    cloudstack.network_type         = "basic"

    cloudstack.template_name                     = "#{data['vm']['provider']['cloudstack']['template_name']}"
    cloudstack.service_offering_name             = "#{data['vm']['provider']['cloudstack']['service_offering_name']}"

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

    if !data['vm']['provider']['cloudstack']['zone_name'].nil?
      cloudstack.zone_name                  = "#{data['vm']['provider']['cloudstack']['zone_name']}"
    end

    if !data['vm']['provider']['cloudstack']['security_groups_id'].nil? && !data['vm']['provider']['cloudstack']['security_groups_id'].empty?
      cloudstack.security_groups_id = data['vm']['provider']['cloudstack']['security_groups_id']
    end

    cloudstack.tags = {}
    data['vm']['provider']['cloudstack']['tags'].each do |key, tag|
      cloudstack.tags.store(:key, tag)
    end
  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
