###########################################################
# 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.

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

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"

  # 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

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #

  #config.vm.network "private_network", ip: "192.168.33.10"
  #config.vm.synced_folder ".", "/vagrant_data"
  # Customzie the amount of cpu cores visible to the VM:
  #  vb.cpus = 4

  config.vm.provider "virtualbox" do |vb|
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  #    vb.memory = "6144"

    # Enable USB
    vb.customize ["modifyvm", :id, "--usb", "on"]
    # Please use your esp device id, run `VBoxManage.exe list usbhost` list devices
    vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'ESP', '--vendorid', '0x1a86', '--productid', '0x7523']
    # Disable log
    vb.customize ['modifyvm', :id, '--uartmode1', 'disconnected']
  end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
    apt-get -qq update && apt-get -qq upgrade -y
    apt-get -qq install -y gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing libpixman-1-0 libpixman-1-dev libglib2.0-dev zlib1g-dev cmake
    if [ ! -d /opt/local/espressif/ ]; then
      mkdir -p /opt/local/espressif/
    fi
    if [ ! -d /opt/local/espressif/xtensa-esp32-elf/ ]; then
      #wget -qO- https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar xz -C /opt/local/espressif/
      wget -qO- https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp32-2019r1-linux-amd64.tar.gz | tar xz -C /opt/local/espressif/

    fi
    if [ ! -d /opt/local/espressif/esp-idf/ ]; then
      git clone -q --recursive https://github.com/espressif/esp-idf.git /opt/local/espressif/esp-idf
    else
      cd /opt/local/espressif/esp-idf && git pull -q && git submodule --quiet update --init --recursive
    fi
    chown -R vagrant: /opt/local/espressif/
    # Build qemu
    cd /home/vagrant
    git clone git://github.com/Ebiroll/qemu-xtensa-esp32
    mkdir qemu_esp32
    cd qemu_esp32
    ../qemu-xtensa-esp32/configure --disable-werror --prefix=`pwd`/root --target-list=xtensa-softmmu
    make
    chown -R vagrant: /home/vagrant/qemu_esp32
  SHELL
  config.vm.provision 'shell', privileged: false, inline: <<-SHELL
    python -m pip install -q --user -r /opt/local/espressif/esp-idf/requirements.txt
    grep -q "IDF_PATH" /home/vagrant/.profile || echo "export IDF_PATH=\"/opt/local/espressif/esp-idf\"\nexport PATH="/opt/local/espressif/xtensa-esp32-elf/bin:${PATH}"\ncd /vagrant" >> /home/vagrant/.profile
  SHELL
end

