Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2204"
  config.vm.provider :libvirt do |libvirt|
    libvirt.memory = 4096
  end
  config.vm.network :public_network, :name => "default"
  config.vm.synced_folder "..",
    "/vagrant",
    type: "9p",
    accessmode: "mapped",
    mount_options: ['rw', 'trans=virtio', 'version=9p2000.L']
  config.vm.provision "file", source: "~/.ssh/id_rsa", destination: "/home/vagrant/.ssh/id_rsa"
  config.vm.provision "shell", inline: "apt-get update -y"
  config.vm.provision "shell", inline: "apt-get install -y build-essential jq"
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    curl -L -O https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init
    chmod +x rustup-init
    ./rustup-init --default-toolchain stable --no-modify-path -y
    echo "source ~/.cargo/env" >> ~/.bashrc
  SHELL
  config.vm.provision "shell", inline: <<-SHELL
    curl -L -O https://github.com/casey/just/releases/download/1.25.2/just-1.25.2-x86_64-unknown-linux-musl.tar.gz
    mkdir just
    tar xvf just-1.25.2-x86_64-unknown-linux-musl.tar.gz -C just
    rm just-1.25.2-x86_64-unknown-linux-musl.tar.gz
    sudo mv just/just /usr/local/bin
    rm -rf just

    curl -L -O https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init
    chmod +x rustup-init
    ./rustup-init --default-toolchain stable --no-modify-path -y
    echo "source ~/.cargo/env" >> ~/.bashrc
    # Copy the binaries to a system-wide location for running tests as the root user
    sudo cp ~/.cargo/bin/** /usr/local/bin
    sudo rsync -av \
      --exclude 'artifacts' \
      --exclude 'deploy' \
      --exclude 'target' \
      --exclude '.git' \
      --exclude '.vagrant' \
      /vagrant/ /root/safe_network
  SHELL
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    mkdir -p ~/.vim/tmp/ ~/.vim/backup
    cat <<'EOF' > ~/.vimrc
set nocompatible

let mapleader=" "
syntax on

set background=dark
set backspace=indent,eol,start
set backupdir=~/.vim/tmp//
set directory=~/.vim/backup
set expandtab
set foldlevel=1
set foldmethod=indent
set foldnestmax=10
set hlsearch
set ignorecase
set incsearch
set laststatus=2
set nobackup
set nofoldenable
set nowrap
set number relativenumber
set ruler
set shiftwidth=4
set smartindent
set showcmd
set shortmess+=A
set tabstop=4
set viminfo+=!

nnoremap j gj
nnoremap k gk
EOF
    cp ~/.vimrc /tmp/.vimrc
  SHELL
  config.vm.provision "shell", inline: <<-SHELL
    mkdir -p /root/.vim/tmp/ /root/.vim/backup
    cp /tmp/.vimrc /root/.vimrc
  SHELL
end
