#!/usr/pkg/bin/ruby18
# prime-dict-index: A dictionary indexer for a PRIME dictionary.
# $Id: prime-dict-index.src,v 1.2 2004/03/25 17:19: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.

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

require 'prime/taiyaki'
require 'getoptlong'
require 'prime/makedict/basicdict'

class PrimeDictIndexCommand
  include Debug

  def initialize (command_name)
    @debug_mode = false

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

    @command_name = command_name
    @version = '1.0.0.1'
    @usage = <<"EOF"
#{@command_name}:#{@version} -- indexes a PRIME dictionary.

  Usage: #{@command_name} target_dictname [options]
  -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? then
      print_usage()
      exit()
    end

    index_dict()
  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]
		       )

    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]

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



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