# -*- mode: ruby -*-
# vi: set ft=ruby :
class FixGuestAdditions < VagrantVbguest::Installers::RedHat
    def dependencies
        packages = super

        # If there's no "kernel-devel" package matching the running kernel in the
        # default repositories, then the base box we're using doesn't match the
        # latest CentOS release anymore and we have to look for it in the archives...
        if communicate.test('test -f /etc/centos-release && ! yum -q info kernel-devel-`uname -r` &>/dev/null')
            env.ui.warn("[#{vm.name}] Looking for the CentOS 'kernel-devel' package in the release archives...")
            packages.sub!('kernel-devel-`uname -r`', 'http://mirror.centos.org/centos' \
                                                     '/`grep -Po \'\b\d+\.[\d.]+\b\' /etc/centos-release`' \
                                                     '/{os,updates}/`arch`/Packages/kernel-devel-`uname -r`.rpm')
        end

        packages
    end
end

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # define machine in specific scope so that we can have a machines active
  # from multiple git branches.
  config.vbguest.installer = FixGuestAdditions
  config.vm.define "ehx", primary: true do |machine|
    # Every Vagrant development environment requires a box. You can search for
    # boxes at https://atlas.hashicorp.com/search.
    machine.vm.box = "centos/7"
    machine.vm.box_version = "1710.01"

    machine.vm.hostname = "ehx"

    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine. In the example below,
    # accessing "localhost:8080" will access port 80 on the guest machine.
    # config.vm.network "forwarded_port", guest: 80, host: 8080

    # Share an additional folder to the guest VM. The first argument is
    # the path on the host to the actual folder. The second argument is
    # the path on the guest to mount the folder. And the optional third
    # argument is a set of non-required options.
    # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
    machine.vm.provider "virtualbox" do |vb, override|
      vb.name = "ehx"
    #   # Display the VirtualBox GUI when booting the machine
    #   vb.gui = true
    #
    #   # Customize the amount of memory on the VM:
      vb.memory = "4096"

      override.vm.synced_folder ".", "/vagrant", disabled: true
      override.vm.synced_folder ".", "/ehx", type: "virtualbox"
      override.vm.synced_folder "weblogic/domains/ehx_devl/autodeploy", "/u01/app/domains/ehx_devl/autodeploy", type: "virtualbox"
      override.vm.synced_folder "weblogic/domains/ehx_devl/servers/admin/logs", "/u01/app/domains/ehx_devl/servers/admin/logs", type: "virtualbox"
      override.vm.network "forwarded_port", guest: 7001, host: 7001, host_ip: "localhost"
      override.vm.network "forwarded_port", guest: 7002, host: 7002, host_ip: "localhost"
      override.vm.network "forwarded_port", guest: 1521, host: 7521, host_ip: "localhost"
      override.vm.network "forwarded_port", guest: 4000, host: 7004, host_ip: "localhost"
      override.vm.network "forwarded_port", guest: 443, host: 7443, host_ip: "localhost"
      override.vm.network "forwarded_port", guest: 80, host: 7080, host_ip: "localhost"
    end

    machine.vm.provider "hyperv" do |vb, override|
      vb.vmname = "ehx"
      vb.memory = "4096"

      override.vm.synced_folder ".", "/vagrant", disabled: true
      override.vm.synced_folder ".", "/ehx", type: "smb", mount_options: ["username=<windows_username>","password=<windows_password>"]
      override.vm.synced_folder "weblogic/domains/ehx_devl/autodeploy", "/u01/app/domains/ehx_devl/autodeploy", type: "smb", mount_options: ["username=<windows_username>","password=<windows_password>"]
      override.vm.synced_folder "weblogic/domains/ehx_devl/servers/admin/logs", "/u01/app/domains/ehx_devl/servers/admin/logs", type: "smb", mount_options: ["username=<windows_username>","password=<windows_password>"]
    end

    # ssh login using username/password (uncomment if you're using Vagrant 1.85).
    #machine.ssh.username = "AI"
    #machine.ssh.password = "AI"
    #machine.ssh.insert_key = true
    machine.ssh.forward_agent = true
    machine.ssh.forward_x11 = true

    # set auto_update to false, if you do NOT want to check the correct 
    # additions version when booting this machine
    machine.vbguest.auto_update = true

    # do NOT download the iso file from a webserver
    machine.vbguest.no_remote = true

    # Enable provisioning with a shell script. Additional provisioners such as
    # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
    # documentation for more information about their specific syntax and use.
    machine.vm.provision "shell", inline: <<-SHELL
     . /ehx/vagrant-scripts/provision.sh
    SHELL
    machine.vm.provision "shell", privileged: false, run: 'always', inline: <<-SHELL2
     cd /u01/app/domains/ehx_devl && ./startDomainDebug.sh
    SHELL2
 end
end
