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

# You may want to enable this line if you're using libvirt and have trouble using 'vagrant destroy'.
# For more information see https://github.com/vagrant-libvirt/vagrant-libvirt/issues/561
# ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'

class NoDistributionSpecified < Vagrant::Errors::VagrantError
    error_message "No distribution specified, use the DISTRO environment variable."
end

class UnknownDistributionSpecified < Vagrant::Errors::VagrantError
    error_message "The specified distribution is not supported."
end

# Configuration map for all supported OS.
distro_settings = {
    'jessie' => {
        box_name: 'debian/jessie64',
        ip_addr: '192.168.10.101',
        gateway: '192.168.10.1'
    },
    'trusty' => {
        box_name: 'ubuntu/trusty64',
        ip_addr: '192.168.10.102',
        gateway: '192.168.10.1'
    },
    'xenial' => {
        box_name: 'yk0/ubuntu-xenial',
        ip_addr: '192.168.10.103',
        gateway: '192.168.10.1'
    },
    'malachite' => {
        box_name: 'opensuse/openSUSE-42.1-x86_64',
        ip_addr: '192.168.10.104',
        gateway: '192.168.10.1'
    },
    'leap' => {
        box_name: 'opensuse/openSUSE-42.3-x86_64',
        ip_addr: '192.168.10.105',
        gateway: '192.168.10.1'
    }
}

# Get the distribution name to be used.
distro = ENV['DISTRO']
unless distro
    #raise NoDistributionSpecified.new
    # Set the default distribution to openSUSE.
    distro = "leap"
end
unless distro_settings.has_key? distro
    raise UnknownDistributionSpecified
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.

    # Set the box to be used in the VM.
    config.vm.box = distro_settings[distro][:box_name]

    # Set the machine name.
    config.vm.define "oa_#{distro}"

    # Set the host name.
    config.vm.hostname = "oa#{distro}"

    config.vm.provider :libvirt do |lv|
        # May be necessary to use 'qemu' here on some mainboards where 'kvm' doesn't work through a bug. 'qemu' seems to
        # work on all machines, but it looks like it's slower than 'kvm'.
        # See OP-1455 (https://tracker.openattic.org/browse/OP-1455) for more information.
        lv.driver = 'kvm'
    end

    # Disable automatic box update checking. If you disable this, then
    # boxes will only be checked for updates when the user runs
    # `vagrant box outdated`. This is not recommended.
    # config.vm.box_check_update = false

    # 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

    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    # Needs to be enabled for VirtualBox to be able to use NFS
    config.vm.network 'private_network', ip: distro_settings[distro][:ip_addr]

    # Reconfigure default route, otherwise the NAT interface is used which
    # is the default in a Vagrant box. This will lead into an incorrect primary
    # interface configuration in oA. To prevent this, the Host-only adapter
    # is configured as default route.
    config.vm.provision "shell",
        run: "always",
        inline: "ip route flush 0/0"
    config.vm.provision "shell",
        run: "always",
        inline: "route add default gw #{distro_settings[distro][:gateway]}"

    # Create a public network, which generally matched to bridged network.
    # Bridged networks make the machine appear as another physical device on
    # your network.
    # config.vm.network "public_network"

    # 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 '..', '/home/vagrant/openattic', type: 'nfs',
                            :mount_options => ['nolock,vers=3,udp,noatime,actimeo=1']
    config.vm.synced_folder '.', '/vagrant', disabled: true # Disable default

    # Provider-specific configuration so you can fine-tune various
    # backing providers for Vagrant. These expose provider-specific options.
    # Example for VirtualBox:
    #
    config.vm.provider :virtualbox do |vb|
        # Rename the box.
        vb.name = "oa_#{distro}_box"
        #   # Display the VirtualBox GUI when booting the machine
        #   vb.gui = true
        vb.memory = '2048'
    end
    config.vm.provider :libvirt do |lv|
        lv.memory = '1024'
    end
    #
    # View the documentation for the provider you are using for more
    # information on available options.

    # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
    # such as FTP and Heroku are also available. See the documentation at
    # https://docs.vagrantup.com/v2/push/atlas.html for more information.
    # config.push.define "atlas" do |push|
    #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
    # end

    # 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.
    # config.vm.provision "shell", inline: <<-SHELL
    #   sudo apt-get update
    #   sudo apt-get install -y apache2
    # SHELL

    #  disk1 = './tmp/disk1.vdi'
    #  disk2 = './tmp/disk2.vdi'

    #  config.vm.provider "virtualbox" do |v|
    #     unless File.exist?(disk1)
    #         v.customize ['createhd', '--filename', disk1, '--size', 30 * 1024]
    #         v.customize ['createhd', '--filename', disk2, '--size', 30 * 1024]
    #     end
    #     v.customize ['storageattach', :id, '--storagectl', 'SATAController', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk1]
    #     v.customize ['storageattach', :id, '--storagectl', 'SATAController', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', disk2]
    #  end

    config.vm.provision :shell, :path => 'install.sh'

    if Vagrant.has_plugin?('vagrant-cachier') and distro != 'xenial'
        # Configure cached packages to be shared between instances of the same base box.
        # More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage
        config.cache.scope = :box

        # OPTIONAL: If you are using VirtualBox, you might want to use that to enable
        # NFS for shared folders. This is also very useful for vagrant-libvirt if you
        # want bi-directional sync
        config.cache.synced_folder_opts = {
            type: :nfs,
            # The nolock option can be useful for an NFSv3 client that wants to avoid the
            # NLM sideband protocol. Without this option, apt-get might hang if it tries
            # to lock files needed for /var/cache/* operations. All of this can be avoided
            # by using NFSv4 everywhere. Please note that the tcp option is not the default.
            mount_options: ['rw', 'vers=3', 'tcp', 'nolock']
        }
        # For more information please check http://docs.vagrantup.com/v2/synced-folders/basic_usage.html
    end
end
