#!/usr/bin/ruby require 'optparse' require 'rubygems' require 'bindata' require 'xtmfile/header' require 'xtmfile/joiner' require 'xtmfile/splitter' class XtmJoin class XtmJoinArgumentError < ArgumentError ; end attr_reader :opts def initialize args @args = [] @input_filename = nil parse_command_line args end def parse_command_line args @args = args.clone @opts = OptionParser.new do |opts| opts.banner = "Usage: #{File.basename $0} [options]\n" opts.separator "" opts.separator "Options" opts.on("-i", "--input FILE", "Input XTM file (mandatory)") do |r| @input_filename = r end opts.on("-o", "--output FILE", "Output file (optional)") do |o| @output_filename = o end opts.on("-m", "--md5", "Verify MD5 sums") do |m| @verify_md5 = m end opts.on("-h", "--help", "Show this help") do |h| @help = h end opts.on("-v", "--verbose", "Show warnings too") do |v| @verbose = v end opts.separator "" end end def validate! @opts.parse! raise XtmJoinArgumentError, "No input XTM file specified!" if @input_filename.nil? raise RuntimeError, "Current input XTM does not exist!" unless File.exist? @input_filename raise RuntimeError, "Current input XTML is not a file!" unless File.file? @input_filename @input_filename = File.expand_path @input_filename end def run validate! # FIXME: prevent overwriting xtmj = XtmFile::Joiner.new @input_filename, @output_filename puts "Writing data to %s" % xtmj.output_filename xtmj.start do |event, info| case event when XtmFile::Joiner::EVENT_OPENFILE then puts "Opening %s" % info when XtmFile::Joiner::EVENT_PROGRESS then print "\x1b[uProgress : %s %" % info STDOUT.flush end end STDOUT.puts "" end def self.main args xj = nil begin xj = XtmJoin.new args xj.run exit 0 rescue XtmJoinArgumentError => e STDERR.puts "%s" % xj.opts STDERR.puts "error: %s" % e.message exit 1 rescue SystemExit => e raise e rescue Exception => e STDERR.puts "error: %s" % e.message exit 1 end end end XtmJoin.main ARGV