#!/usr/bin/env ruby
# This program converts DocBook documents into .epub files.
# 
# Usage: dbtoepub [OPTIONS] [DocBook Files]
#
# .epub is defined by the IDPF at www.idpf.org and is made up of 3 standards:
# - Open Publication Structure (OPS)
# - Open Packaging Format (OPF) 
# - Open Container Format (OCF)
#
# Specific options:
#     -d, --debug                      Show debugging output.
#     -h, --help                       Display usage info
#     -v, --verbose                    Make output verbose

lib = File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
$LOAD_PATH.unshift(lib) if File.exist?(lib)

require 'optparse'

require 'docbook'

verbose = false
debug = false

# Set up the OptionParser
opts = OptionParser.new
opts.banner = "Usage: #{File.basename($0)} [OPTIONS] [DocBook Files]

#{File.basename($0)} converts DocBook <book> and <article>s into to .epub files.

.epub is defined by the IDPF at www.idpf.org and is made up of 3 standards:
- Open Publication Structure (OPS)
- Open Packaging Format (OPF) 
- Open Container Format (OCF)

Specific options:"
opts.on("-d", "--debug", "Show debugging output.") {debug = true; verbose = true}
opts.on("-h", "--help", "Display usage info") {puts opts.to_s; exit 0}
opts.on("-v", "--verbose", "Make output verbose") {verbose = true}

db_files = opts.parse(ARGV)
if db_files.size == 0
  puts opts.to_s
  exit 0
end

db_files.each {|docbook_file|
  e = DocBook::Epub.new(docbook_file)
  epub_file = File.basename(docbook_file, ".xml") + ".epub"
  puts "Rendering DocBook file #{docbook_file} to #{epub_file}" if verbose
  e.render_to_file(epub_file)
}  
