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

def os_cpu_cores
  case RbConfig::CONFIG['host_os']
  when /darwin/
    Integer(`sysctl -n hw.ncpu`)
  when /linux/
    Integer(`cat /proc/cpuinfo | grep processor | wc -l`)
  else
    raise StandardError, "Unsupported platform"
  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|
  config.vm.synced_folder "../../../", "/cfe/core", type: "rsync",
                          rsync__args: ["--archive", "--links", "--delete"]

  config.vm.box = "generic/centos9s"
  config.vm.provider :libvirt do |lv|
    lv.cpus = os_cpu_cores
    lv.memory = 1024
    lv.disk_driver :cache => 'unsafe'
  end

  # common provisioning
  config.vm.provision "shell", inline: <<-SHELL
sed -ri '/-build/d' /etc/hosts
dnf -q -y install epel-release
dnf config-manager --set-enabled crb
dnf -q -y install '@Development tools' pkgconfig pam-devel libtool libtool-ltdl
dnf -q -y install 'perl(Sys::Hostname)'  # needed for remake_outputs.pl
dnf -q -y update
SHELL

  no_deps_vm_name = "no-deps-build"
  full_vm_name = "full-build"

  # using system-provided dependencies
  config.vm.define no_deps_vm_name.to_sym, primary: true do |nd|
    nd.vm.hostname = no_deps_vm_name.to_sym

    nd.vm.provision "shell", inline: <<-SHELL
set -e
dnf -q -y install lmdb-devel pcre2-devel openssl-devel libyaml-devel libxml2-devel
SHELL

    nd.vm.provision "shell", inline: <<-SHELL
set -e
shopt -s expand_aliases
alias make='make -j -l#{os_cpu_cores}'
cd /cfe/core
make clean && make maintainer-clean
./autogen.sh -C --enable-debug
make
export UNSAFE_TESTS=1
export GAINROOT=sudo
chmod -R go-w tests/acceptance
make check
SHELL

  end

  # doing a full build will the dependencies
  config.vm.define full_vm_name.to_sym, primary: false, autostart: false do |fd|
    fd.vm.hostname = full_vm_name.to_sym
    fd.vm.synced_folder "../../../../buildscripts", "/cfe/buildscripts", type: "rsync",
                          rsync__args: ["--archive", "--links", "--delete"]
    fd.vm.synced_folder "../../../../masterfiles", "/cfe/masterfiles", type: "rsync",
                          rsync__args: ["--archive", "--links", "--delete"]

    fd.vm.provision "shell", inline: <<-SHELL
set -e
dnf -q -y install 'perl(ExtUtils::MakeMaker)' 'perl(Digest::MD5)' 'perl(Module::Load::Conditional)' 'perl(IO::Uncompress::Gunzip)' 'perl(JSON::PP)' 'perl(IPC::Cmd)' \
                  ncurses-devel rpm-build
dnf -q -y install psmisc # for fuser command
dnf -q -y install wget
SHELL

    fd.vm.provision "shell", inline: <<-SHELL
shopt -s expand_aliases
export MAKEFLAGS="-j -l#{os_cpu_cores}"
alias make='make -j -l#{os_cpu_cores}'
export BUILD_TYPE=DEBUG
export NO_CONFIGURE=1
export PROJECT=community
cd /cfe
./buildscripts/build-scripts/autogen
yum -q -y remove libtool libtool-ltdl
./buildscripts/build-scripts/clean-buildmachine
./buildscripts/build-scripts/install-dependencies
./buildscripts/build-scripts/configure
./buildscripts/build-scripts/compile
./buildscripts/build-scripts/package
rm -rf /var/cfengine
export TEST_MACHINE=chroot
./buildscripts/build-scripts/clean-buildmachine
./buildscripts/build-scripts/install-dependencies
./buildscripts/build-scripts/test
SHELL
  end
end
