#!/usr/bin/ruby

require 'rubygems'
require 'ginger'
require 'rake'

if ARGV.length == 0
  puts <<-USAGE
ginger #{Ginger::Version::String}
Use ginger to run specs for each scenario defined. Scenarios must be set out in
a file called ginger_scenarios.rb wherever this tool is run. Once they're
defined, then you can run this tool and provide the rake task that would
normally be called.

Examples:
  ginger spec
  ginger test
  ginger spec:models
USAGE
  exit 0
end

file_path = File.join Dir.pwd, ".ginger"

File.delete(file_path) if File.exists?(file_path)

scenarios = Ginger::Configuration.instance.scenarios
puts "No Ginger Scenarios defined" if scenarios.empty?

scenarios.each_with_index do |scenario, index|
  puts <<-SCENARIO

-------------------
Ginger Scenario: #{scenario.name || index+1}
-------------------
  SCENARIO
  
  File.open('.ginger', 'w') { |f| f.write index.to_s }
  
  system("rake #{ARGV.join(" ")}")     || # Standard
  system("rake.bat #{ARGV.join(" ")}") || # Windows
  raise('Missing: rake or rake.bat. You might want to fix that.')
end

File.delete(file_path) if File.exists?(file_path)
