#!/usr/bin/env ruby
#
# This file was generated by RubyGems.
#
# The application 'json_schemer' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

Gem.use_gemdeps

version = ">= 0.a"

str = ARGV.first
if str
  str = str.b[/\A_(.*)_\z/, 1]
  if str and Gem::Version.correct?(str)
    version = str
    ARGV.shift
  end
end

path =
  if Gem.respond_to?(:activate_bin_path)
    Gem.activate_bin_path('json_schemer', 'json_schemer', version)
  else
    gem "json_schemer", version
    Gem.bin_path("json_schemer", "json_schemer", version)
  end

if __FILE__ == path
#!/usr/bin/env ruby

require 'json'
require 'optparse'
require 'pathname'
require 'json_schemer'

parser = OptionParser.new('Usage:', 32, '  ')
parser.separator("  #{parser.program_name} [options] <schema> <data>...")
parser.separator("  #{parser.program_name} [options] <schema> -")
parser.separator("  #{parser.program_name} [options] - <data>...")
parser.separator("  #{parser.program_name} -h | --help")
parser.separator("  #{parser.program_name} --version")
parser.separator('')
parser.separator('Options:')
parser.on('-e', '--errors MAX', Integer, 'Maximum number of errors to output', 'Use "0" to validate with no output')
parser.on_tail('-h', '--help', 'Show help')
parser.on_tail('-v', '--version', 'Show version')

options = {}
parser.parse!(:into => options)

if options[:help]
  $stdout.puts(parser)
  exit
end

if options[:version]
  $stdout.puts("#{parser.program_name} #{JSONSchemer::VERSION}")
  exit
end

if ARGV.size == 0
  $stderr.puts("#{parser.program_name}: no schema or data")
  exit(false)
end

if ARGV.size == 1
  $stderr.puts("#{parser.program_name}: no data")
  exit(false)
end

if ARGV.count('-') > 1
  $stderr.puts("#{parser.program_name}: multiple stdin")
  exit(false)
end

errors = 0
schema = ARGF.file.is_a?(File) ? Pathname.new(ARGF.file.path) : ARGF.file.read
schemer = JSONSchemer.schema(schema)

while ARGV.any?
  data = JSON.parse(ARGF.skip.file.read)
  schemer.validate(data).each do |error|
    exit(false) if options[:errors] == 0
    errors += 1
    $stdout.puts(JSON.generate(error))
    exit(false) if options[:errors] == errors
  end
end

exit(errors.zero?)

else
  load(path)
end
