#!/usr/bin/env ruby
require 'English'
require 'pathname'
require 'optimist'

lib_path = Pathname(File.join(File.dirname(__FILE__),"..","lib")).realpath
$LOAD_PATH.unshift(lib_path)
require 'patience_diff'

begin
  program_name = File.basename($PROGRAM_NAME)
  opts = Optimist::options do
    banner <<-EOF.gsub(/^\s*/, '')
      Usage: #{program_name} [options] left-file right-file (left-file-2 right-file-2 ...)
      Options:
    EOF

    version "#{program_name} #{PatienceDiff::VERSION}"

    opt :debug, "Debugging mode"
    opt :context, "Lines of context", :default => 3
    opt :all_context, "Don't collapse common sections; output entire files. Ignored in html format."
    opt :format, "Specify output format. Currently 'text' and 'html' are supported.", :default => "text"
    opt :ignore_whitespace,
      "Ignore trailing whitespace, and treat leading whitespace as either present or not. This switch is for compatibility with diff's -b option.",
      :short => '-b'
  end

  raise PatienceDiff::UsageError unless ARGV.length and ARGV.length % 2 == 0

  differ = PatienceDiff::Differ.new(opts)
  formatter = case opts.delete(:format)
    when 'text'
      PatienceDiff::Formatter
    when 'html'
      PatienceDiff::Html::Formatter
    else
      raise PatienceDiff::UsageError
    end.new(differ)

  formatter.format do |diff|
    ARGV.each_slice(2) do |(left_file, right_file)|
      diff.files left_file, right_file
    end
  end.tap do |result|
    print result
  end

rescue PatienceDiff::UsageError => e
  Optimist.module_eval do
    @last_parser.educate($stderr)
  end
rescue StandardError => e
  if opts[:debug]
    raise
  else
    $stderr.puts "Error: #{e}"
  end
end
