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

# 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|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "archlinux/archlinux"
  config.vm.box_version = "20241001.267073"  
  config.vm.hostname = "spring-v20241213"
  
  config.vm.define "spring" do |h|
    h.vm.provider :virtualbox
    h.vm.disk :disk, size: "200GB", primary: true
  end
  
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  config.vm.network "forwarded_port", guest: 8000, host: 8000
  config.vm.network "forwarded_port", guest: 3000, host: 3000
  config.vm.network "forwarded_port", id: "ssh", guest: 22, host: 10022

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./mnt", "/mnt"

  # Disable the default share of the current code directory. Doing this
  # provides improved isolation between the vagrant box and your host
  # by making sure your Vagrantfile isn't accessible to the vagrant box.
  # If you use this you may want to enable additional shared subfolders as
  # shown above.
  # config.vm.synced_folder ".", "/vagrant", disabled: true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
    vb.name = "mint"
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
    # Customize the amount of memory on the VM:
    vb.memory = "4096"
    vb.cpus = "8"
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # config.ssh.insert_key = false
  # config.ssh.password = "vagrant"

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
    pacman-key --init
    pacman -Sy --noconfirm archlinux-keyring
    pacman-key --populate
    pacman -Syu --noconfirm
    pacman -S --needed --noconfirm base-devel bzip2 bzip3 p7zip unarchiver vim git cmake wget zsh man-db man-pages \
      pwgen sshpass openssl openssh rsync zip unzip tree tmux asciidoc doxygen cpio net-tools bind-tools \
      imagemagick ffmpeg xorg-font-util \
      ttf-dejavu wqy-bitmapfont wqy-microhei wqy-zenhei \
      composer xdebug php-fpm php-pgsql php-sqlite php-redis php-mongodb php-imagick php-gd php-intl php-enchant php-snmp php-tidy php-xsl php-sodium php-odbc php-pspell \
      hspell nuspell libvoikko hunspell hunspell-en_us \
      nodejs npm yarn \
      postgresql rabbitmq redis
    pacman -Scc --noconfirm
    
    echo "en_US.UTF-8 UTF-8" > /etc/locale.gen locale-gen
    systemctl enable postgresql
    systemctl enable redis
    systemctl enable rabbitmq

    sed -i 's/memory_limit =.*/memory_limit = 1G/' /etc/php/php.ini
    sed -i 's/;extension=bcmath/extension=bcmath/' /etc/php/php.ini
    sed -i 's/;extension=bz2/extension=bz2/' /etc/php/php.ini
    sed -i 's/;extension=calendar/extension=calendar/' /etc/php/php.ini
    sed -i 's/;extension=enchant/extension=enchant/' /etc/php/php.ini
    sed -i 's/;extension=exif/extension=exif/' /etc/php/php.ini
    sed -i 's/;extension=ftp/extension=ftp/' /etc/php/php.ini
    sed -i 's/;extension=gd/extension=gd/' /etc/php/php.ini
    sed -i 's/;extension=gettext/extension=gettext/' /etc/php/php.ini
    sed -i 's/;extension=gmp/extension=gmp/' /etc/php/php.ini
    sed -i 's/;extension=iconv/extension=iconv/' /etc/php/php.ini
    sed -i 's/;extension=intl/extension=intl/' /etc/php/php.ini
    sed -i 's/;extension=mysqli/extension=mysqli/' /etc/php/php.ini
    sed -i 's/;extension=odbc/extension=odbc/' /etc/php/php.ini
    sed -i 's/;extension=pdo_mysql/extension=pdo_mysql/' /etc/php/php.ini
    sed -i 's/;extension=pdo_odbc/extension=pdo_odbc/' /etc/php/php.ini
    sed -i 's/;extension=pdo_pgsql/extension=pdo_pgsql/' /etc/php/php.ini
    sed -i 's/;extension=pdo_sqlite/extension=pdo_sqlite/' /etc/php/php.ini
    sed -i 's/;extension=pgsql/extension=pgsql/' /etc/php/php.ini
    sed -i 's/;extension=pspell/extension=pspell/' /etc/php/php.ini
    sed -i 's/;extension=snmp/extension=snmp/' /etc/php/php.ini
    sed -i 's/;extension=soap/extension=soap/' /etc/php/php.ini
    sed -i 's/;extension=sockets/extension=sockets/' /etc/php/php.ini
    sed -i 's/;extension=sodium/extension=sodium/' /etc/php/php.ini
    sed -i 's/;extension=sqlite3/extension=sqlite3/' /etc/php/php.ini
    sed -i 's/;extension=tidy/extension=tidy/' /etc/php/php.ini
    sed -i 's/;extension=xsl/extension=xsl/' /etc/php/php.ini
    sed -i 's/;extension=redis/extension=redis/' /etc/php/conf.d/redis.ini
    sed -i 's/;extension=igbinary/extension=igbinary/' /etc/php/conf.d/igbinary.ini
    sed -i 's/; extension = imagick/extension=imagick/' /etc/php/conf.d/imagick.ini
    sed -i 's/;zend_extension=opcache/zend_extension=opcache/' /etc/php/php.ini
    sed -i 's/;zend_extension=xdebug/zend_extension=xdebug/' /etc/php/conf.d/xdebug.ini
    sed -i 's/;xdebug.mode=debug/xdebug.mode=develop,profile,coverage,debug/' /etc/php/conf.d/xdebug.ini
    echo 'xdebug.start_with_request=yes' | tee -a /etc/php/conf.d/xdebug.ini
    echo 'xdebug.client_host=0.0.0.0' | tee -a /etc/php/conf.d/xdebug.ini
    echo 'xdebug.client_port=9003' | tee -a /etc/php/conf.d/xdebug.ini
    echo 'xdebug.cli_color = 2' | tee -a /etc/php/conf.d/xdebug.ini

    su -l -c 'mkdir -p ~/downloads ~/build ~/local ~/tmp' vagrant
    su -l -c 'git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh' vagrant
    su -l -c 'cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc' vagrant
    su -l -c "echo 'export LANG=en_US.UTF-8' >> ~/.zshrc" vagrant
    su -l -c "echo 'export LC_ALL=en_US.UTF-8' >> ~/.zshrc" vagrant
    su -l -c "echo 'export EDITOR=vim' >> ~/.zshrc" vagrant

    su -l -c 'git config --global core.quotepath false' vagrant
    su -l -c 'git config --global http.version HTTP/1.1' vagrant
    su -l -c 'git config --global pull.rebase false' vagrant

    su -l -c "echo 'set-option -g history-limit 102400' > ~/.tmux.conf" vagrant
    su -l -c "echo 'set-option -g default-shell "/bin/zsh"' >> ~/.tmux.conf" vagrant
    
    chsh -s /bin/zsh vagrant
  SHELL
end
