#!/usr/bin/env ruby
# encoding: utf-8

$KCODE = 'U' unless RUBY_VERSION > "1.9.0"
# Japanese locale see some odd failure without below...
ENV["LANG"] = ENV["LANG"].sub("utf8", "utf-8")

require 'rubygems'
begin
  require 'gettext'
rescue LoadError
  require 'rubygems'
  require 'gettext'
end
require 'alexandria'
require 'optparse'
require 'ostruct'

options = OpenStruct.new
OptionParser.new do |opts|
  opts.banner = "Usage: alexandria [options]"
  opts.on("-l", "--list", "List libraries in numbered format") { puts Alexandria.list_books_on_console; exit }
  #opts.on("--output FORMAT", 
  #        "Output library as FORMAT: csv|xml|yaml; use with --library or omit for ALL") do |val| 
  #        options.format = val; exit
  #end
  #opts.on("--library LIBRARY", 
  #        "Used with --output to pick a library to output") do |val|
  #        options.library = val; exit
  #end
  opts.on("-d", "--debug", "Run in debug mode") { $DEBUG = true }
  opts.on("-h", "--help", "Show help options") { puts opts; exit }
  opts.on("-v", "--version", "Get version number") { puts Alexandria::VERSION; exit }
end.parse!


begin
  Alexandria.main
rescue => e
  $stderr.puts <<EOS
-----------------------
Alexandria just crashed
-----------------------
Timestamp: #{Time.now}
Message: #{e.message}
Backtrace:
#{e.backtrace.join("\n")}
Release: #{Alexandria::VERSION}(#{Alexandria::DISPLAY_VERSION})
Uname -a: #{`uname -a`.chomp}
--
Please report this dump to '#{Alexandria::LIST}' with some additional
information, such as the description of the crash and the steps to reproduce it
(if it's possible).
EOS
  exit 1
end
