#!/usr/bin/env ruby
require 'rubygems'
gem 'treetop'

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
require 'treetop'

if ARGV.empty?
  puts "Usage:\n\ntt foo.treetop bar.treetop ...\n or\ntt foo.treetop -o alternate_name.rb\n\n"
  exit
end

compiler = Treetop::Compiler::GrammarCompiler.new

while !ARGV.empty?
  treetop_file = ARGV.shift
  if !File.exist?(treetop_file)
    puts "Error: file '#{treetop_file}' doesn't exist\n\n"
    exit(2) 
  end
  if ARGV.size >= 2 && ARGV[1] == '-o'
    ARGV.shift	  # explicit output file name option
    compiler.compile(treetop_file, ARGV.shift)
  else
    # list of input files option
    compiler.compile(treetop_file)
  end
end
