#!/usr/bin/env ruby
# prime-dict-convert: A dictionary converter from a dictionary of another IME
# $Id: prime-dict-convert,v 1.1.2.2 2003/12/29 09:50:36 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'
require 'prime/makedict/basicdict'

class PrimeDictConvertCommand
  include Debug

  def initialize (command_name)
    @debug_mode = false
    @is_interactive = true

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

    @command_name = command_name
    @version = "0.2.0"
    @usage = <<"EOF"
#{@command_name}:#{@version} -- converts from a dictionary of another IME.

  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.

       --prime  :  merge the input_dict with the output_dict
       --canna  :  convert from the canna dict
       --skk    :  convert from the skk dict
       --pubdic :  convert from the pubdic dict

  -q,  --quiet    :  run this command and show nothing.
  -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
    convert_dicts()
    index_dict()
  end

  def convert_dicts
    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 == :prime then
	converter = DictFormat.new()
      elsif 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 index_dict
    indexer = PrimeBasicdict.new(@output_dictname, @is_interactive)
    indexer.make_pos_table()
    indexer.make_basicdict_indexes()
  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],
                       ['--quiet',      '-q',   GetoptLong::NO_ARGUMENT],
		       ['--append',             GetoptLong::NO_ARGUMENT],
		       ['--overwrite',          GetoptLong::NO_ARGUMENT],
		       ['--prime',              GetoptLong::REQUIRED_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
    if options['quiet'] then
      @is_interactive = false
    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['prime'] then
      @input_dictnames.push([:prime, options['prime']])
    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 = PrimeDictConvertCommand.new(File::basename($0))
  conv_command.main()
end
