#!/usr/pkg/bin/ruby18
#### prime-userdict-update: Command to update a userdict of PRIME.
#### $Id: prime-userdict-update.src,v 1.5 2005/03/07 07:51:34 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.

PRIME_LIBDIR = '/usr/pkg/lib/ruby/vendor_ruby/1.8'
$LOAD_PATH.unshift(PRIME_LIBDIR) unless $LOAD_PATH.member?(PRIME_LIBDIR)

require 'prime/prime-config'
require 'prime/makedict/userdict'
require 'getoptlong'

class PrimeUserdictUpdateCommand
  include Debug

  def initialize (command_name)
    @debug_mode = false

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

    @command_name = command_name
    @version = '1.0.0.1'
    @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.

  -q,  --quiet      :  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 @conversion then # with --conversion
      if @output_dictname.nil? or @input_dictname.nil? then
	print_usage()
	exit()
      end
      prime_update = PrimeUserdictUpdate.new(@output_dictname, @is_interactive)
      if prime_update.check_necessity(@input_dictname) then
	prime_update.update(@input_dictname)
	prime_update.delete_files(@input_dictname)
      end

      if @indexing then # with --conversion and --indexing
        prime_makeindex =
             PrimeUserdictMakeIndex.new(@output_dictname, @is_interactive)
        prime_makeindex.make_indexes()
      end
    elsif @indexing then # with --indexing
      if @output_dictname.nil? then
	print_usage()
	exit()
      end
      prime_makeindex =
	PrimeUserdictMakeIndex.new(@output_dictname, @is_interactive)
      prime_makeindex.make_indexes()
    end

    system('/usr/pkg/bin/prime-refresh')

    $stderr.puts "Done." if @is_interactive
  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],
		       ['--quiet',      '-q',   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

    if options['quiet'] then
      @is_interactive = false
    else
      @is_interactive = true
    end

    @output_dictname = ARGV[0]
    @input_dictname  = ARGV[1]

    if options['auto'] then
      @indexing   = true
      @conversion = true
      @input_dictname  = (ARGV[0] or (PRIME_USER_DIR + "/userdict_diff"))
      @output_dictname = (ARGV[1] or (PRIME_USER_DIR + "/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 = PrimeUserdictUpdateCommand.new(File::basename($0))
  conv_command.main()
end
