#!/usr/bin/env ruby
# prime-convert-userdict: A command to convert from userdict2 to <new user dict?> for PRIME.
# $Id: prime-userdict-convert,v 1.1.2.1 2003/12/21 14:29:28 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/makedict/userdict'
require 'getoptlong'

class PrimeUserdictConvertCommand
  include Debug

  def initialize (command_name)
    @debug_mode = false

    @indexing   = true
    @conversion = true
    @output_dictname = nil
    @input_dictname  = nil

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

  Usage: #{@command_name} [options] [output_dictname] [input_dictname] 
  -a,  --all        :  conversion and indexing (default)
  -c,  --conversion :  conversion only
  -i,  --indexing   :  indexing only
       --auto       :  conversion automatically.

  -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 @conversion then
      if @output_dictname.nil? or @input_dictname.nil? then
	print_usage()
	exit()
      end
      prime_conv = PrimeUserdictConvert.new(@output_dictname)
      prime_conv.convert(@input_dictname)
    end

    if @indexing then
      if @output_dictname.nil? then
	print_usage()
	exit()
      end
      prime_makeindex = PrimeUserdictMakeIndex.new(@output_dictname)
      prime_makeindex.make_indexes()
    end
    puts "Done."
  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],
		       ['--auto',               GetoptLong::NO_ARGUMENT],
		       ['--all',        '-a',   GetoptLong::NO_ARGUMENT],
		       ['--conversion', '-c',   GetoptLong::NO_ARGUMENT],
		       ['--indexing',   '-i',   GetoptLong::NO_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]
    @input_dictname  = ARGV[1]

    if options['auto'] then
      @indexing   = true
      @conversion = true
      @input_dictname  = (ARGV[0] or (ENV['HOME'] + "/.prime/prime-dict-user"))
      @output_dictname = (ARGV[1] or (ENV['HOME'] + "/.prime/userdict"))
    elsif options['all'] then
      @indexing   = true
      @conversion = true
    elsif options['indexing'] then
      @indexing   = true
      @conversion = false
    elsif options['conversion'] then
      @indexing   = false
      @conversion = true
    end
  end
end

### FIXME: Check both errors 'file not found' and 'overwriting files'
### FIXME: <komatsu@taiyaki.org> (2003-11-30)

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