#!/usr/bin/env ruby
#
# $Id: xmlresume2europecv.rb 6 2004-10-14 10:36:22Z thomas $
#
# xmlresume2europecv: converts an xml resume to a europecv LaTeX file
# Copyright (C) 2004 Thomas Leitner
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not,
# write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

require 'optparse'
require 'ostruct'
require 'xmlresume2x/converter'

converter = XMLResume2x::Converter.new

# Try to parse data dir independently to show correct help messages
begin
  OptionParser.new do |opt|
    opt.on( "-d", "--data-dir DATADIR" ) {|converter.data_dir|}
    opt.on( "-h", "--help" ) {}
    opt.on( "-v", "--version" ) {}
  end.permute( ARGV )
rescue OptionParser::ParseError
end

options = OpenStruct.new

opt = OptionParser.new do |opt|
  opt.banner = "Usage: xmlresume2x [options] RESUME.XML "
  opt.separator "Options:"
  opt.separator ""
  opt.on( "-f", "--format FORMAT", converter.available_formats, "Use FORMAT as the output format (#{converter.available_formats.join(', ')})" ) {|options.format|}
  opt.on( "-l", "--lang LANG", converter.available_langs, "Use LANG as the resume language (#{converter.available_langs.join(', ')})" ) {|options.language|}
  opt.on( "-c", "--config CONFIG", "Use CONFIG as additional configuration file (can be used to override default behaviour)" ) {|options.userconfig|}
  opt.on( "-d", "--data-dir DATADIR", "Use the directory DATADIR as data directory" ) {|converter.data_dir|}
  opt.on( "-h", "--help" ) { puts opt; exit 1}
end

files = opt.permute( ARGV )
raise OptionParser::NeedlessArgument.new( *files[1..-1] ) if files.length > 1
if options.format.nil? || options.language.nil?
  $stderr.puts "Error: You need to specify the FORMAT and the LANGUAGE!!!"
  $stderr.puts opt;
  exit 1
end
converter.load_config( options.format, options.language, options.userconfig )
puts converter.convert( files[0] )
