#!/usr/bin/ruby

require File.expand_path('../../lib/peck', __FILE__)
require File.expand_path('../../lib/peck/option_parser', __FILE__)

options = Peck::OptionParser.parse(ARGV)
files = options.last

if options.first.has_key?('h') || options.first.has_key?('help')
  puts <<-EOF
Usage: peck [options] [files]

  -c, --concurrency        set the number of threads used to run the suite
  -e, --match-regexp       run tests that match a given regular expression
  -t, --match-text         run tests that match a given string
  -a, --recently-accessed  run tests that have been updated in the last 15 minutes
  -S, --no-strip-backtrace doesn't strip anything from the backtrace
  -f, --fail-fast          fail with the first failing assertion
  -d, --debug              log debug messages to stdout
  -h, --help               show this message
  EOF
  exit
end

unless (%w(v version) & options.first.keys).empty?
  puts "Peck #{Peck::VERSION}"
  exit 1
end

if concurrency = options.first['c'] || options.first['concurrency']
  Peck.concurrency = concurrency.to_i
end

if match_regexp = options.first['e'] || options.first['match-regexp']
  Peck.spec_selector = Regexp.new(match_regexp)
end

if match_text = options.first['t'] || options.first['match-text']
  Peck.spec_selector = /#{match_text}/
end

if options.first['S'] || options.first['no-strip-backtrace']
  Peck.clean_backtrace = false
end

if options.first['f'] || options.first['fail-fast']
  Peck.fail_fast = true
end

unless (%w(d debug) & options.first.keys).empty?
  require File.expand_path('../../lib/peck/debug', __FILE__)
end

files.concat Dir["{test,spec}/**/*_{test,spec}.rb"] if files.empty?

files.each do |file|
  if options.first.has_key?('a') || options.first.has_key?('recently-accessed')
    load file if File.ctime(file) > Time.now - 900
  else
    load file
  end
end