#!/usr/bin/env ruby
# prime-dict-convert: A dictionary converter from a dictionary of another IME
# $Id: prime-convert-userdict,v 1.1 2003/12/01 07:07:51 komatsu Exp $
#
# Copyright (C) 2003 Hiroyuki Komatsu <komatsu@taiyaki.org>
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of 
# the GNU General Public License version 2.

require 'prime/taiyaki'
require 'getoptlong'
require 'prime/makedict/dictformat-skkdic'
require 'prime/makedict/dictformat-pubdic'
require 'prime/makedict/dictformat-cannadic'

class PrimeConvertCommand
  include Debug

  def initialize (command_name)
    @debug_mode = false

    @indexing   = true
    @conversion = true
    @output_dictname = nil
    @input_dictnames = []

    @command_name = command_name
    @version = "0.2.0"
    @usage = <<"EOF"
#{@command_name}:#{@version} -- converts user dictionaries for PRIME.

  Usage: #{@command_name} output_dictname --<format> input_dictname [options]
       --append    : append words to the existent output dictionary. (default)
       --overwrite : delete the existent output dictionary and make new dict.

       --canna  :  convert from a canna dict
       --skk    :  convert from a skk dict
       --pubdic :  convert from a pubdic dict

  -v,  --version  :  show the version and exit
  -h,  --help     :  show this help and exit
  -d,  --debug    :  run under debug mode
EOF
  end

  def main ()
    parse_options()

    if @output_dictname.nil? or @input_dictnames.empty? then
      print_usage()
      exit()
    end

    basedict = DictFormat.new()
    if @flag_append then
      basedict.load_existent_dict(@output_dictname)
      dict = basedict.dict
    end
    dict = basedict.dict

    converter = nil
    @input_dictnames.each {|(type, filename)|
      if type == :skk then
	converter = DictFormatSkkdic.new()
      elsif type == :canna then
	converter = DictFormatCannadic.new()
      elsif type == :pubdic then
	converter = DictFormatPubdic.new()
      else
	puts "Invalid format option..."
	exit()
      end
      converter.dict = dict
      converter.load_external_dict(filename)
      dict = converter.dict
    }
    converter.save_dict(@output_dictname)
  end

  def print_version ()
    puts "#{@command_name}:#{@version}"
  end
  
  def print_usage ()
    puts @usage
  end

  private
  def parse_options ()
    options = {}
    parser = GetoptLong.new()
    parser.set_options(['--help',       '-h',   GetoptLong::NO_ARGUMENT],
		       ['--version',    '-v',   GetoptLong::NO_ARGUMENT],
		       ['--debug',      '-d',   GetoptLong::NO_ARGUMENT],
		       ['--append',             GetoptLong::NO_ARGUMENT],
		       ['--overwrite',          GetoptLong::NO_ARGUMENT],
		       ['--canna',              GetoptLong::REQUIRED_ARGUMENT],
		       ['--skk',                GetoptLong::REQUIRED_ARGUMENT],
		       ['--pubdic',             GetoptLong::REQUIRED_ARGUMENT]
		       )

    parser.each_option {|option, arg|
      options[option.sub(/^--/, '')] = arg
    }

    if options['version'] then
      print_version()
      exit()
    elsif options['help'] then
      print_usage()
      exit()
    end

    if options['debug'] then
      $DEBUG = true
    end

    @output_dictname = ARGV[0]

    @flag_append = true
    if options['append'] then
      @flag_append = true
    elsif options['overwrite'] then
      @flag_append = false
    end

    if options['pubdic'] then
      @input_dictnames.push([:pubdic, options['pubdic']])
    end
    if options['canna'] then
      @input_dictnames.push([:canna, options['canna']])
    end
    if options['skk'] then
      @input_dictnames.push([:skk, options['skk']])
    end
  end
end



if File::expand_path($0) == File::expand_path(__FILE__) then
  conv_command = PrimeConvertCommand.new(File::basename($0))
  conv_command.main()
end
