#!/usr/local/bin/ruby
#
# refe
#
# Copyright (c) 2001-2003 Minero Aoki <aamine@loveruby.net>
#
# This program is free software.
# You can distribute/modify this program under the terms of
# the GNU Lesser General Public License version 2 or later.
#

require 'refe/database'
require 'refe/searcher'
require 'refe/mygetopt'
require 'refe/info'


def main
  begin
    trap(:PIPE, 'EXIT')
  rescue
  end
  trap(:INT, 'EXIT')

  getopt = MyGetoptLong.new(<<EndUsage, <<EndOptions)

refe -- Ruby reference manual referer

Usage:  refe [options] [method name exp.| function name exp.]
  
Options

%%options%%

Acceptable method name expressions (-r/-c)
  c = class name, m = method name;
  names are omittable (e.g. gsub! -> gsub gsu gs g gsu! gs! g!).

  c      (gives the list of all methods)

  c#m                                    i#see -> IO#seek
  c,m                                    s,ind -> String#index
  c m                                    s g!  -> String#gsub!
  m      (gives method description)      each_w -> Enum#each_with_index

Acceptable function name expressions (-e/-C)
  w = word;  words are omittable (e.g. index -> inde ind in i)

  _w     (prefixed to rb_)               _eval -> rb_eval
  w_w_w  (expanded to word_word_word)    r_e_s_w -> rb_eval_string_wrap
  w w w  (expanded to word_word_word)    r e s w -> rb_eval_string_wrap

EndUsage

  o -r -              -      Lookup method document from method name.
  o -c -              -      Lookup function from method name.
  o -e -              -      Lookup function document from function name.
  o -C -              -      Lookup function from function name.

  o -a --all          -      Print all description of classes/methods.
  o -s --short        -      Print only names.
  o -l --line         -      Print one name per line.

  o -d --databasedir  <dir>  Use <dir> as the database root directory.

  o -  --version      -      Print version and quit.
  o -  --copyright    -      Print copyright and quit.
  o -h --help         -      Print this message.
  x -  --debug        -      Turn on tracing.

EndOptions


  opts = {}
  begin
    getopt.each do |name, arg|
      opts[name.sub(/\A\-+/, '')] = arg
    end
  rescue => err
    getopt.usage(1, err.message)
  end
  getopt.usage(0) if opts['help']
  (puts "ReFe version #{ReFe::Version}"; exit 0) if opts['version']
  (puts "ReFe -- #{ReFe::Copyright}"; exit 0) if opts['copyright']
  if opts['debug']
    $REFE_TRACE = true
    $DEBUG = true
  end

  unless opts['r'] or opts['c'] or opts['e'] or opts['C']
    opts['r'] = true
  end
  if (opts['r'] or opts['c']) and (opts['e'] or opts['C'])
    $stderr.puts '-r/-c and -e/-C are exclusive.'
    exit 1
  end
  if opts['r'] or opts['c']
    getopt.usage(1) if ARGV.size > 3
  end

  begin
    db = ReFe::Database.new(opts['databasedir'])
    policy = ReFe::OutputPolicy.new2(opts)
    ReFe::MethodSearcher.new(db.class_document,
                             db.method_document,
                             policy).search(ARGV) if opts['r']
    ReFe::MethodSearcher.new(db.class_document,
                             db.mf_relation,
                             policy).search(ARGV) if opts['c']
    ReFe::FunctionSearcher.new(db.function_document,
                               policy).search(ARGV) if opts['e']
    ReFe::FunctionSearcher.new(db.function_source,
                               policy).search(ARGV) if opts['C']
  rescue RuntimeError
    raise
  rescue ReFe::CompletionError => err
    $stderr.puts err.message
    exit 1
  rescue StandardError => err
    raise if $DEBUG
    $stderr.puts err.message
    exit 1
  end
end


main
