#####
##### This file is part of Mail-in-a-Box-LDAP which is released under the
##### terms of the GNU Affero General Public License as published by the
##### Free Software Foundation, either version 3 of the License, or (at
##### your option) any later version. See file LICENSE or go to
##### https://github.com/downtownallday/mailinabox-ldap for full license
##### details.
#####

def checkout_tag_and_mount(obj, tag)
  if "#{tag}" == ""
    obj.vm.synced_folder "../../..", "/mailinabox", id: "mailinabox", automount: false
  else
    _srcdir="src/maibldap-#{tag}"
    if not Dir.exist?(_srcdir)
      puts "Cloning tag #{tag} to #{_srcdir}"
      if tag.size==40 and tag.match?(/\A[0-9a-fA-F]+\Z/)
        system("git clone #{ENV['MIABLDAP_GIT']} #{_srcdir}")
        system("cd #{_srcdir}; git reset --hard #{tag}")
      else
        system("git clone -b #{tag} --depth 1 #{ENV['MIABLDAP_GIT']} #{_srcdir}")
      end
    end
    obj.vm.synced_folder _srcdir, "/mailinabox", id: "mailinabox", automount: false
  end
end


Vagrant.configure("2") do |config|

  checkout_tag_and_mount config, ENV['RELEASE_TAG']
  
  config.vm.define "preloaded-ubuntu-bionic64" do |m1|
    m1.vm.box = "ubuntu/bionic64"
    m1.vm.provision :shell, :inline => <<-SH
cd /mailinabox
tests/vagrant/preloaded/prepvm.sh --no-dry-run
rc=$?
echo "$rc" > "/vagrant/prepcode.txt"
[ $rc -gt 0 ] && exit 1
exit 0
SH
  end

  config.vm.define "preloaded-ubuntu-jammy64" do |m1|
    m1.vm.box = "ubuntu/jammy64"
    m1.vm.boot_timeout = 30
    m1.vm.provision :shell, :inline => <<-SH
cd /mailinabox
tests/vagrant/preloaded/prepvm.sh --no-dry-run
rc=$?
echo "$rc" > "/vagrant/prepcode.txt"
[ $rc -gt 0 ] && exit 1
exit 0
SH
  end

end