#!/usr/bin/env ruby

# Note: this program is part of qtruby and makes use of its internal functions.
#       You should not rely on those in your own programs.

require 'getopts'
getopts('r:hvimp')

case File.basename $0
when "rbqtapi"
   require 'Qt'
when "rbtdeapi"
   require 'Korundum'
end

if $OPT_v
    # TODO add and use version number #{TQt::VERSION}
    print "qtruby using Qt-#{TQt::version}\n"
    exit 0 
elsif $OPT_h
    print $usage
    exit 0
end

if $OPT_m
    while 1
        line = STDIN.readline.chomp
        line.gsub!(/^Q(?=[A-Z])/,'TQt::')
        line.gsub!(/^K/,'KDE::') unless line =~ /^(KDE)|(TDEIO)|(KParts)|(KNS)/
        classid = TQt::Internal::find_pclassid($_)
        puts "__START__"
        if classid
            a = TQt::Internal::findAllMethods(classid)
            ids = (a.keys.sort.map{|k|a[k]}).flatten
            candidates = TQt::dumpCandidates(ids)
            sup = []
            TQt::Internal::getAllParents(classid, sup)
            sup.each {
                |sup_item|
                a = TQt::Internal::findAllMethods(sup_item)
                ids = (a.keys.sort.map{|k|a[k]}).flatten
                candidates << TQt::Internal::dumpCandidates(ids)
            }
            candidates.gsub("\t","") # erm. whats the "s" mean on s/\t//gs ?
            print candidates
        end
        puts "__END__"
    end
end

search_string = ARGV[0] ? ARGV[0].dup : nil
search_string.gsub!(/^Q(?=[A-Z])/,'TQt::') if search_string
# search_string.gsub!(/^K(?=[^D][^E])/,'KDE::') if search_string
search_string.gsub!(/^K/,'KDE::') unless search_string.nil? or search_string =~ /^(KDE)|(TDEIO)|(KParts)|(KNS)/
classid = search_string ? TQt::Internal::find_pclassid(search_string) : 1
if classid == 0
    puts "Class #{search_string} not found"
    exit 1
end
regexp = nil
regexp = ( $OPT_i ? Regexp.new($OPT_r, Regexp::IGNORECASE) : Regexp.new($OPT_r) ) if $OPT_r
candidates = ""
while true
    a = TQt::Internal::findAllMethods(classid)
    break if a.nil? 
    ids = (a.keys.sort.map{|k|a[k]}).flatten
    candidates = TQt::Internal::dumpCandidates(ids)
    if $OPT_p and !search_string.empty? and classid
	sup = []
        TQt::Internal::getAllParents(classid, sup)
	sup.each {
	    |sup_item|
            a = TQt::Internal::findAllMethods(sup_item)
            ids = (a.keys.sort.map{|k|a[k]}).flatten
            candidates << TQt::Internal::dumpCandidates(ids)
        }
    end
    if regexp
	candidates.split("\n").each {
	    |candidate|
	    puts candidate if candidate =~ regexp
	}
    else
	print candidates
    end
    break unless search_string.nil?
    classid += 1
end

BEGIN {
$usage = <<USAGE
rbqtapi - a qtruby introspection tool\t(c) Germain Garand 2003 <germain\@ebooksfrance.org>

usage: rbqtapi [-r <re>] [<class>]

options:
\t-r <re> : find all functions matching regular expression/keyword <re>
\t-i : together with -r, performs a case insensitive search
\t-p : display also inherited methods for <class>.
\t-v : print qtruby and Qt versions
\t-h : print this help message
USAGE
}
