require 'yaml'

def_copr_repo = "nieslony/arachne"

if not ENV.key?('ARACHNE_HOSTNAME')
    hostname = "arachne.test.lab"
    puts "Warning: ARACHNE_HOSTNAME not set, falling back to 'arachne.test.lab'"
else
    hostname = ENV['ARACHNE_HOSTNAME']
end

if not ENV.key?("ARACHNE_REPO")
    arachne_repo = ""
    if not ENV.key?("ARACHNE_COPR")
        arachne_copr = def_copr_repo
        puts "Warning neither ARACHNE_REPO nor ARACHNE_COPR set. Falling back to copr repo 'nieslony/arachne'"
    else
        arachne_copr = ENV['ARACHNE_COPR']
        puts "Warning: ARACHNE_REPO noit set falling back to given ARACHNE_COPR " + arachne_copr
    end
end

if File.exist?("provision.d/ansible.cfg")
    ansible_cfg = "provision.d/ansible.cfg"
else
    ansible_cfg = ""
end

if File.exist?("provision.d/extra_vars.yml")
    extra_vars = YAML.load_file("provision.d/extra_vars.yml")
else
    extra_vars = {}
end
extra_vars["webserver_vhosts"] = [hostname]

Vagrant.configure("2") do |config|
    config.vagrant.plugins = [ "vagrant-libvirt", "vagrant-timezone", "vagrant-proxyconf" ]

    config.timezone.value = :host

    config.vm.provider :libvirt do |libvirt|
        libvirt.cpus = 2
        libvirt.memory = 2048
        libvirt.clock_offset = 'utc'
        libvirt.keymap = "de"
        libvirt.channel :type => 'unix',
            :target_name => 'org.qemu.guest_agent.0',
            :target_type => 'virtio'
        libvirt.video_type = "qxl"
        libvirt.input :type => "mouse",
            :bus => "usb"
        libvirt.random :model => 'random'
    end

    config.vm.define "arachne" do |arachne|
        arachne.vm.box = "almalinux/10"
        arachne.timezone.value = :host

        config.vm.provision "shell",
            name: "Disable cloud init",
            run: "always",
            inline: "touch /etc/cloud/cloud-init.disabled"

        Dir["provision.d/*"].each do |filename|
            case File.extname(filename)
                when ".rb"
                    puts "Including #{filename} as Ruby"
                    eval(IO.read(filename), binding, File.realpath(filename))
                when ".sh"
                    puts "Including #{filename} as Shell Provisioner"
                    config.vm.provision "shell",
                        path: "#{filename}"
                when ".yml"
                    puts "Including #{filename} as Ansible Provisioner"
                    if filename != "provision.d/extra_vars.yml"
                        if ansible_cfg != ""
                            config.vm.provision "Provision #{filename}",
                                type: "ansible",
                                playbook: filename,
                                config_file: ansible_cfg,
                                extra_vars: extra_vars
                        else
                            config.vm.provision "Provision #{filename}",
                                type: "ansible",
                                playbook: filename,
                                extra_vars: extra_vars
                        end
                    end
                else
                    puts "Ignoring #{filename}"
            end
        end
        arachne.vm.hostname = hostname

        arachne.vm.provision "Install arachne",
            type: "ansible",
            playbook: "arachne.yml",
            extra_vars: {
                arachne_repo: arachne_repo,
                arachne_copr: arachne_copr
                }
    end # arachne
end # config
