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

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.box_check_update = false

  config.vm.network "forwarded_port", guest: 5000, host: 5000, host_ip: "127.0.0.1"
  config.vm.network "forwarded_port", guest: 6600, host: 6600, host_ip: "127.0.0.1"

   config.vm.provider "virtualbox" do |vb|
     vb.gui = false
     vb.memory = "512"
   end
  
   config.vm.provision "shell", inline: <<-SHELL
      apt-get update -q
      apt-get install -qy vim tmux git ffmpeg
      
      # python3-poetry is too old (does not support groups ...)
      apt-get install -qy python3-pip
      pip install poetry
      git clone https://github.com/kmille/deezer-downloader.git /opt/deezer
      cd /opt/deezer
      poetry install
      poetry run deezer-downloader --show-config-template > /opt/deezer/settings.ini

      # enable yt-dlp
      pip install yt-dlp
      sed -i 's,.*command = /usr/bin/yt-dlp.*,command = /usr/local/bin/yt-dlp,' /opt/deezer/settings.ini

      # enable mpd
      apt-get install -yq mpd ncmpcpp
      sed -i 's/^bind_to_address.*"localhost"/bind_to_address         "0.0.0.0"/' /etc/mpd.conf
      sed -i 's,^music_directory.*,music_directory         "/tmp/deezer-downloader",' /etc/mpd.conf
      systemctl restart mpd
      sed -i 's/.*use_mpd = False.*/use_mpd = True/' /opt/deezer/settings.ini
      sed -i 's/.*host = 127.0.0.1.*/host = 0.0.0.0/' /opt/deezer/settings.ini

      echo "1) Adjust the Deezer cookie: sudo vim /opt/deezer/settings.ini" >> /etc/motd
      echo "2) Run it: cd /opt/deezer && sudo poetry run deezer-downloader --config settings.ini" >> /etc/motd
      echo "3) Try out: ncmpcpp -h 127.0.0.1 (you won't hear anything because you are in a vm. But you can use it on a Rasberry Pi)" >> /etc/motd
      echo "4) Downloaded files are in /tmp/deezer-downloader" >> /etc/motd
   SHELL
end
