#!/usr/bin/env ruby
require "optparse"
require "fssm"
require File.dirname(__FILE__) + "/../lib/ESBify"



options = {stdout: false, expectations: "./Expectations.xml", behaviors: "./Behaviours.xml", strategies: "./Strategy.xml", filter: /.*/, only: %w[expectations behaviors strategies], context: {}, watch: false}
OptionParser.new do |opts|
  opts.banner = "Usage: esbify [options] FILES...
  ESBify will parse FILES and transform them to the appropriate XML. By default it will save expectations to 
  Expectations.xml, behaviors to Behaviors.xml and strategies to Strategy.xml. 
  
  This can be customized with the following options:
  "
  
  opts.on("--only expectations,behaviors,strategies", Array, "Only parse these sections.") do |v|
    raise ArgumentError unless v.all?{|o| %w[expectations behaviors strategies].include?(o)}
    options[:only] = v
  end
  
  opts.on("-e", "--expectations FILE", "Output Expectations to FILE") do |v|
    options[:expectations] = v
  end
  
  opts.on("-b", "--behaviors FILE", "Output Behaviours to FILE") do |v|
    options[:behaviors] = v
  end
  
  opts.on("-s", "--strategies FILE", "Output Strategies to FILE") do |v|
    options[:strategies] = v
  end
  
  opts.on("--[no-]stdout", "Print all files to STDOUT insted of individual files.") do |v|
    options[:stdout] = v
  end
  
  opts.separator("\nESBify will also print a list of expectation names to STDOUT.\n")
  
  opts.on("-f", "--filter REGEXP", "When printing out expectations, only print those matching this regexp.") do |v|
    options[:filter] = Regexp.new(v)
  end
  
  opts.separator("\nOther various options:\n")
  
  opts.on("--context HASH_OR_PATH", "A path to a ruby class or a hash of values that will be passed to ERB", "The ruby class should have a underscored filename and CamelCased classname. ") do |v|
    if v =~ /\:|=>/
      options[:context] = eval(v, binding)
    else
      require v
      name = v.gsub(/(_|^)./){|a| a.chars.to_a.last.upcase}
      const = Object.const_defined?(name) ? Object.const_get(name) : Object.const_missing(name)
      options[:context] = const.new
    end
  end
  
  opts.on("-w", "--watch", "Watches files for changes and automatically recompiles on change.", "All files in `pwd` ending with .esb will be watched (FILES are ignored).") do |v|
    options[:watch] = true
  end
  
  opts.on_tail("-h", "--help [README]", "Call -h with README to view the readme file") do |t|
    if t == "README"
      puts File.read(File.dirname(__FILE__) + "/../README.md")
    else
      puts opts
    end
    exit
  end
  
  opts.on_tail("-v", "--version", "Show version") do
    puts ESBify::VERSION
    exit
  end
  
end.parse!



def run(files, options)
  parser = ESBify::Parser.new options[:context]

  files.each do |f|
    parser.parse_file! f
  end 

  if options[:stdout]
    puts parser.expectations if options[:only].include?("expectations")
    puts parser.behaviors if options[:only].include?("behaviors")
    puts parser.strategies if options[:only].include?("strategies")
  else
    File.open(options[:expectations], "w") {|f| f << parser.expectations } if options[:only].include?("expectations")
    File.open(options[:behaviors], "w") {|f| f << parser.behaviors } if options[:only].include?("behaviors")
    File.open(options[:strategies], "w") {|f| f << parser.strategies } if options[:only].include?("strategies")
  end
  STDERR.puts "Compiled!"
  puts parser.expectation.names.grep(options[:filter]).join(",")
end


if options[:watch]
  run(Dir[Dir.pwd + '/*.esb'], options)
  
  FSSM.monitor(Dir.pwd, '*.esb') do
    update do |base, relative|
      run(Dir[Dir.pwd + '/*.esb'], options)
    end
  end
  
else
  run ARGV, options
end