#!/usr/bin/env ruby -s
require 'rubygems'
require File.expand_path(File.dirname(__FILE__) + "/../lib/rdf_context")
require 'getoptlong'
class Parse
  include RdfContext
  def parse(file, base_uri, store = nil)
    puts "Parse: #{file.is_a?(StringIO) ? base_uri : file}" if $quiet
    graph_opts = {:identifier => base_uri}
    graph_opts[:store] = store if store
    pg = RdfContext::Graph.new if $pg_format
    parser = Parser.new(:graph => Graph.new(graph_opts), :processor_graph => pg)
    parser.parse(file.respond_to?(:read) ? file : File.open(file), base_uri, :strict => true)
    puts parser.graph.serialize(:format => $format.to_sym, :base => base_uri) unless $quiet
    puts parser.debug.to_a.join("\n\t") if $verbose
    if pg
      puts "\nProcessor Graph:\n"
      puts pg.serialize(:format => $pg_format.to_sym) unless $quiet
    end
  rescue RdfException => e
    puts "Parse failure: #{e.message}"
    puts parser.debug if $verbose && parser
    raise if RdfContext.debug?
  rescue Exception => e
    puts "Parser fault: #{e.message}"
    puts parser.debug if parser && !$quiet
    raise
  end
end

mode = ARGV.shift
raise "Mode must be one of 'parse'" unless mode == "parse"

$verbose = false
$quiet = false
$format = "ttl"
$pg_format = nil
base_uri  = "http://example.com"
store = :list_store
opts = GetoptLong.new(
  ["--verbose", GetoptLong::NO_ARGUMENT],
  ["--quiet", GetoptLong::NO_ARGUMENT],
  ["--debug", GetoptLong::NO_ARGUMENT],
  ["--pg-format", GetoptLong::REQUIRED_ARGUMENT],
  ["--format", GetoptLong::REQUIRED_ARGUMENT],
  ["--store", GetoptLong::REQUIRED_ARGUMENT],
  ["--uri", GetoptLong::REQUIRED_ARGUMENT]
)
opts.each do |opt, arg|
  case opt
  when '--verbose' then $verbose = true
  when '--quiet' then $quiet = true
  when '--debug' then ::RdfContext::debug = true
  when '--format' then $format = arg
  when '--pg-format' then $pg_format = arg
  when '--uri' then base_uri = arg
  when '--store'
    case arg
    when /list/
      store = :list_store
    when /memory/
      store = :memory_store
    else
      puts "Creating SQLite3 database '#{arg}'" unless File.exists?(arg)
      store = RdfContext::SQLite3Store.new(RdfContext::URIRef.new("http://kellogg-assoc.com/rdf_context"), :path => arg)
    end
  end
end

x = Parse.new
if ARGV.empty?
  s = $stdin.read
  x.parse(StringIO.new(s), base_uri, store)
else
  ARGV.each do |test_file|
    x.parse(test_file, base_uri, store)
  end
end