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

  config.vm.define "@@MOCHA_SUV_ID@@" do |box|

    # Every Vagrant development environment requires a box. You can search for
    # boxes at https://vagrantcloud.com/search.
    # box.vm.box = "mocha/lunar64"
    box.vm.box = "ubuntu/mantic64"
    #box.vm.box = "default.box"
    
    box.vm.hostname = "@@MOCHA_SUV_ID@@.privatesuv.com"

    # 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.
    # box.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
    # box.vm.network "forwarded_port", guest: 80, host: 8080

    # 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
    # box.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.
    #box.vm.network "private_network", type: "dhcp"
    box.vm.network "private_network", ip: "@@MOCHA_SUV_ADDRESS@@"

    # Create a public network, which generally matched to bridged network.
    # Bridged networks make the machine appear as another physical device on
    # your network.
    # box.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.
    # box.vm.synced_folder "../data", "/vagrant_data"

    # Provider-specific configuration so you can fine-tune various
    # backing providers for Vagrant. These expose provider-specific options.
    # Example for VirtualBox:
    #
    # box.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.memory = "1024"
    # end
    #
    # View the documentation for the provider you are using for more
    # information on available options.
    box.vm.provider "virtualbox" do |vb|
      vb.name = "mochasuv-@@MOCHA_SUV_ID@@"
    end

    # 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.
    box.vm.provision "shell", inline: <<-SHELL
    
    # create peristent mocha configuration dir
    mkdir /etc/mocha
    mkdir /etc/mocha/include

    echo "pending:building:1/27:check for updates" > /etc/mocha/suvstatus

	  apt-get update
  

    echo "pending:building:2/27:install dependencies" > /etc/mocha/suvstatus

	  apt-get install -y mariadb-server apache2 php php-mysql php-xml php-mbstring pwgen python3-mysqldb
  

    echo "pending:building:2/27:install dependencies [redis]" > /etc/mocha/suvstatus
    snap install redis
    # pecl install redis
    apt-get install -y python3-hiredis
  
    echo "pending:building:3/27:apply web server configuration" > /etc/mocha/suvstatus

    # enable apache modules
    a2enmod rewrite
    a2enmod ssl
    
    systemctl restart apache2

    # ssh - for development only - REMOVE if publishing for public use
    sed -e "s/KbdInteractiveAuthentication no/KbdInteractiveAuthentication yes/" /etc/ssh/sshd_config > /etc/ssh/sshd_config
    systemctl restart ssh
    
   
    echo "pending:building:4/27:configure users" > /etc/mocha/suvstatus

    # add webmasters group and webmaster user
    addgroup webmasters
    addgroup zqadmin
    
    WEBMASTER_PASS="Solty/AyanamiREI^^96274"
    ZQADMIN_PASS="Solty/AyanamiREI^^96274"

    useradd -g webmasters -p "$(openssl passwd -6 $WEBMASTER_PASS)" webmaster
    useradd -g zqadmin -p "$(openssl passwd -6 $ZQADMIN_PASS)" zqadmin

    echo "pending:building:5/27:import machine template" > /etc/mocha/suvstatus
    SHELL

    box.vm.provision "file", source: "site.conf", destination: "$HOME/mochadev.conf"
    box.vm.provision "file", source: "localhost.key", destination: "$HOME/localhost.key"
    box.vm.provision "file", source: "localhost.crt", destination: "$HOME/localhost.crt"
    box.vm.provision "file", source: "mocha-libexec", destination: "$HOME/mocha-libexec"
    box.vm.provision "file", source: "uploads", destination: "$HOME/uploads"
    box.vm.provision "file", source: "sql", destination: "$HOME/sql"
    box.vm.provision "file", source: "libraries", destination: "$HOME/libraries"
    box.vm.provision "file", source: "site", destination: "$HOME/html"
    box.vm.provision "file", source: "site/.htaccess", destination: "$HOME/html/.htaccess"
   
    # initial provision of site
    box.vm.provision "shell", inline: <<-SHELL

      # **************************************************************
      # SITE CONFIGURATION AND SSL
      # **************************************************************
      mv /home/vagrant/mochadev.conf /etc/apache2/sites-available/mochadev.conf
      
      mv /home/vagrant/localhost.key /etc/ssl/certs
      mv /home/vagrant/localhost.crt /etc/ssl/certs
      
      # **************************************************************
      # APACHE2 CONFIGURATION
      # **************************************************************

      echo "pending:building:6/27:apply machine template" > /etc/mocha/suvstatus
      # block port 80
      sed -i -e "s/Listen 80/#Listen 80/" /etc/apache2/ports.conf

      # disable default site and enable mochadev site
      a2dissite 000-default
      a2ensite mochadev

      # reload configuration
      systemctl reload apache2
      
      echo "pending:building:7/27:install utilities" > /etc/mocha/suvstatus

      mkdir /usr/share/mocha
      mkdir /usr/lib/mocha
      
      mkdir /var/mocha
      mv /home/vagrant/uploads /var/mocha

      echo "mocha_suv" > /etc/mocha/dbname
      echo "mocha_suv" > /etc/mocha/dbuser

      mv /home/vagrant/mocha-libexec/mocha /usr/bin/mocha
      mv /home/vagrant/mocha-libexec/mocha-* /usr/lib/mocha
      mv /home/vagrant/mocha-libexec/internal /usr/lib/mocha
      mv /home/vagrant/mocha-libexec/spot_register_for_shutdown /usr/lib/mocha
      
      mv /home/vagrant/sql /usr/lib/mocha/sql
      cp /home/vagrant/html/include/Configuration.inc.php.template /etc/mocha/include/

      chmod a+x /usr/bin/mocha
      chmod --recursive a+x /usr/lib/mocha

      chown --recursive zqadmin /usr/lib/mocha /usr/share/mocha /etc/mocha /var/mocha
      chgrp --recursive zqadmin /usr/lib/mocha /usr/share/mocha /etc/mocha /var/mocha
      
      echo "pending:building:8/27:reset suv" > /etc/mocha/suvstatus
    SHELL
    
    # reset the site to the default upon launching the VM
    # remove the run: "always" if you do not desire this behavior
    box.vm.provision "shell", inline: <<-SHELL

      if [ "@@MOCHA_ALWAYS_PROVISION@@" != "true" ]; then
        rm -rf /var/www/html
        cp -r /home/vagrant/html /var/www

        rm -rf /usr/share/mocha/libraries
        cp -r /home/vagrant/libraries /usr/share/mocha/libraries
      fi

    SHELL
    box.vm.provision "shell", run: "always", inline: <<-SHELL
      
      if [ "@@MOCHA_ALWAYS_PROVISION@@" == "true" ]; then
        rm -rf /var/www/html
        cp -r /home/vagrant/html /var/www

        rm -rf /usr/share/mocha/libraries
        cp -r /home/vagrant/libraries /usr/share/mocha/libraries
        
        rm -rf /etc/mocha/*

        echo "!! WARNING !! This is a TRANSIENT SUV !!"
        echo ">> Data you change on this instance WILL NOT BE SAVED"
        echo ">> You MUST migrate your changes manually before shutting down if you wish to save them"
      else
        echo "!! This is a PERSISTENT SUV !!"
        echo ">> Data you change on this instance will be kept INDEFINITELY"
        echo ">> UNTIL you manually reset it with 'mocha suv reset'"
      fi

      chown --recursive webmaster /var/www
      chgrp --recursive webmasters /var/www

      chmod a+x /etc/mocha
      
      # create the initial mocha user and get a randomly generated password
      mocha up

      echo "running:buildcompleted" > /etc/mocha/suvstatus
      
    SHELL
  end
  
  #config.trigger.after :up do |trigger|
  #  trigger.info = "Open newly provisioned SUV in firefox"
  #  trigger.run = {inline: "bash -c 'firefox https://@@MOCHA_SUV_HOSTNAME@@/super/d/home.htmld' &"}
  #end
  
end
