#!/usr/bin/env ruby

# License
#
#   Copyright (c) 2004 Koen Vervloesem. All rights reserved.
#
#   Permission is hereby granted, free of charge, to any person obtaining
#   a copy of this software and associated documentation files (the
#   "Software"), to deal in the Software without restriction, including
#   without limitation the rights to use, copy, modify, merge, publish,
#   distribute, and/or sell copies of the Software, and to permit persons
#   to whom the Software is furnished to do so, provided that the above
#   copyright notice(s) and this permission notice appear in all copies of
#   the Software and that both the above copyright notice(s) and this
#   permission notice appear in supporting documentation.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
#   OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
#   HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
#   SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
#   RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
#   CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#   Except as contained in this notice, the name of a copyright holder
#   shall not be used in advertising or otherwise to promote the sale, use
#   or other dealings in this Software without prior written authorization
#   of the copyright holder.


require 'jpeg2pdf'
require 'optparse'
require 'ostruct'

def add_entry(entry)
  if File.directory?(entry) and File.basename(entry) != "." and File.basename(entry) != ".."
    Dir.entries(entry).sort.each { |filename|
      if Options.all or filename[0]!='.'
        newentry = File.join(entry, filename)
        if Options.recursive
          add_entry(newentry)
        elsif File.file?(newentry)
          Jpeg2pdf.add_jpeg(newentry)
        end
      end
    }
  elsif File.file?(entry)
    if Options.verbose
      puts "Adding " + entry
    end
    Jpeg2pdf.add_jpeg(entry)
  end
end

Options = OpenStruct.new
Options.all = false
Options.recursive = false
Options.verbose = false

opts = OptionParser.new

  opts.banner = "Usage: jpeg2pdf [options] <directory> <pdf>"
  opts.separator("")
  opts.separator("Options:")

  opts.on("-r", "--recursive", "Process directories recursively") do
    Options.recursive = true
  end

  opts.on("-a", "--all", "Process al files, including hidden files") do
    Options.all = true
  end

  opts.on("-v", "--verbose", "Give more output") do
    Options.verbose = true
  end

  # general options
  opts.on_tail("-h", "--help", "Show usage information") do
    puts opts
    exit
  end

  opts.on_tail("-V", "--version", "Show version") do
    puts "jpeg2pdf 0.12"
    exit
  end

  opts.parse!(ARGV)

if ARGV.size != 2
  puts opts
  exit
else
  Jpeg2pdf = JPEG2PDF.new(File.new(ARGV[1], "wb"))

  add_entry(File.expand_path(ARGV[0]))

  Jpeg2pdf.close
end
