#!/usr/bin/env ruby
#
# This file was generated by RubyGems.
#
# The application 'org-ruby' 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('org-ruby', 'org-ruby', version)
  else
    gem "org-ruby", version
    Gem.bin_path("org-ruby", "org-ruby", version)
  end

if __FILE__ == path
#!/usr/bin/env ruby
require 'org-ruby'
require 'optparse'

options = {}
options_parser = OptionParser.new do |opts|
  options[:help] = false
  options[:format] = :html
  
  opts.banner = "Usage: org-ruby <file> [options]"

  opts.on("-h", "--help", "Show this message") do |v|
    options[:help] = true
  end

  opts.on("-d", "--debug", "Run with $DEBUG true") do |v|
    options[:debug] = true
  end

  opts.on("-m", "--markup <file>", "Set Custom Markup file") do |f|
    options[:markup_file] = f
  end

  opts.on("-t", "--translate FORMAT", [:html, :textile, :markdown],
          "Translate the ORG file to the specified format.") do |v|
    options[:format] = v
  end

  opts.on("-v", "--version", "Print version") do |v|
    options[:version] = true
  end
end

begin
  options_parser.parse!

  if options[:version]
    puts OrgRuby::VERSION
    exit
  end

  if (ARGV.length == 0) then
    puts options_parser
  else
    data = IO.read(ARGV[0])
    p = Orgmode::Parser.new(data, (options[:markup_file] ? {:markup_file => options[:markup_file]} : {}))
    $DEBUG = true if options[:debug]
    puts p.to_html if options[:format] == :html
    puts p.to_textile if options[:format] == :textile
    puts p.to_markdown if options[:format] == :markdown
  end
rescue OptionParser::ParseError
  puts options_parser
end

else
  load(path)
end
