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

Vagrant.configure("2") do |config|
  config.vm.box = "terrywang/archlinux"

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    #vb.gui = true
  
    # Customize the amount of memory on the VM:
    vb.name = "archlinux_dotfiles"
    vb.memory = "1024"
    vb.cpus = 2
  end

  config.vm.provision "shell", inline: <<-SHELL
    # SWAP
    fallocate -l 1G /swapfile
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    echo "/swapfile none swap defaults 0 0" >> /etc/fstab
    # TMP
    mount -o remount,size=1G /tmp
    # LOCALE
    echo "fr_FR.UTF-8 UTF-8" > /etc/locale.gen
    locale-gen
    echo 'LANG="fr_FR.UTF-8"' > /etc/locale.conf
    # KEYBOARD
    echo 'KEYMAP=fr' > /etc/vconsole.conf
    loadkeys fr
    localectl --no-convert set-x11-keymap fr
    # UPGRADE
    pacman -Syu --noconfirm
    # DEPS
    pacman -S ansible jshon --noconfirm
    pacman -R cowsay --noconfirm || :
    # DOTFILES DIR
    mkdir -p /home/vagrant/dotfiles
  SHELL

  config.vm.synced_folder "../../", "/home/vagrant/dotfiles/", type: "rsync", rsync__exclude: [".git/", ".vagrant.d/"]

  $script = <<-SCRIPT
  # PREP
  touch ~/.viminfo; rm ~/.viminfo
  touch ~/.vimrc; rm ~/.vimrc
  # EXEC
  cd /tmp
  curl -o PKGBUILD https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=packer-git; makepkg PKGBUILD --install --needed --noconfirm
  cd $HOME/dotfiles
  echo "[desktop]\nlocalhost ansible_connection=local" > hosts
  ansible-playbook install.yml && echo -e "vagrant\n/usr/bin/zsh" | chsh
  SCRIPT

  config.vm.provision "shell", inline: $script, privileged: false
end
