#!/usr/pkg/bin/ruby193

require 'yaml'

def execute(*commands)
  commands.each do |command|
    system(command)
    unless $?.success?
      message = [
        "Executing shell command failed.",
        "  Command: #{command}",
        "  Status:  #{$?.exitstatus}"
      ].join("\n")
      raise message
    end
  end
end

def reset_bundle
  execute(
    "rm -rf .bundle/gems",
    "rm -rf gemfiles/.bundle/gems",
    "rm -f *.lock",
    "rm -f gemfiles/*.lock"
  )
end

def with_rbenv(command)
  %{export PATH="$HOME/.rbenv/bin:$PATH"; eval "$(rbenv init -)"; #{command}}
end

def run(ruby_version, gemfile, task = "test")
  ENV["RBENV_VERSION"] = ruby_version
  ENV["BUNDLE_GEMFILE"] = gemfile
  ENV["MOCHA_OPTIONS"] = "debug"
  ENV["MOCHA_NO_DOCS"] = "true"
  reset_bundle
  execute(
    with_rbenv("bundle install --gemfile=#{gemfile}"),
    with_rbenv("bundle exec rake #{task}"),
  )
end

travis_config = YAML.load(File.read('.travis.yml'))
build_configs = travis_config['matrix']['include']
travis_config['rvm'].each do |ruby_version|
  travis_config['gemfile'].each do |gemfile|
    travis_config['env'].each do |env|
      build_configs << { 'rvm' => ruby_version, 'gemfile' => gemfile, 'env' => env }
    end
  end
end

build_configs.each do |config|
  ruby_version = config['rvm']
  gemfile = config['gemfile']
  environment_variables = Hash[*config['env'].split.flat_map { |e| e.split('=') }]
  original_environment_variables = {}
  begin
    environment_variables.each do |k, v|
      original_environment_variables[k] = ENV[k]
      ENV[k] = v
    end
    p [ruby_version, gemfile, environment_variables]
    run(ruby_version, gemfile)
  ensure
    original_environment_variables.each do |k, v|
      ENV[k] = v
    end
  end
end
