#!/usr/bin/env ruby

# Extract the setup commands for a development Mastodon server, in the
# form of shell scripts.
#
# Expects a single argument: the path to a clone of the Mastodon
# server git repository (https://github.com/mastodon/mastodon).
#
# Output is three shell scripts in the cwd: provisionA.sh,
# provisionElasticsearch.sh, provisionB.sh.
#
# Works by loading 'Vagrantfile' from that directory, which is a setup
# script for the VM management tool 'vagrant', describing a full VM
# which Vagrant can set up by talking to Virtualbox or some similar
# tool. Mastodon's Vagrantfile defines most of the setup commands in
# three large Ruby string variables, and then has a
# Vagrant.configure() call containing a block that does the actual
# setup work, expecting that 'vagrant' has defined the Vagrant class
# to have that static method. So we can extract the string variables
# by simply making our own 'Vagrant' class with a configure method
# that does nothing.

class Vagrant
  def self.configure(foo)
  end
end

load "#{ARGV[0]}/Vagrantfile"

File.open("provisionA.sh", "w") do |file|
  file.write("#!/bin/bash\n")
  file.chmod(0755)
  file.write($provisionA)
end
File.open("provisionElasticsearch.sh", "w") do |file|
  file.write("#!/bin/bash\n")
  file.chmod(0755)
  file.write($provisionElasticsearch)
end
File.open("provisionB.sh", "w") do |file|
  file.write("#!/bin/bash\n")
  file.chmod(0755)
  file.write($provisionB)
end
