#!/usr/local/bin/perl
#
# rt-class-map.pl
#
# Created: 09/28/05 17:54:15 EDT by Andy Harrison
#
#
# Inspect RT's libraries and show a the details of their classes.
#
#
# Usage:
#        rt-class-map.pl [--lib /path/to/libs] [--rt /path/to/rthome]
#                        [--module RT::Module] [--all]
#                        [--verbose]
#
# $Id: rt-class-map.pl,v 1.3 2005/09/29 17:13:34 ajharrison Exp $

use strict;
use warnings;

$|++;

local $main::VERSION =
    '$Id: rt-class-map.pl,v 1.3 2005/09/29 17:13:34 ajharrison Exp $';

# 
# Unless specified on the commandline, this will 
# be the default RT Home directory.
#
use constant RTHOME => '/usr/local/rt3';
use vars     qw/$rt/;

use File::Find;
use Data::Dumper;
use Getopt::Long;
use Class::Inspector;

my %opts;
GetOptions( \%opts, 'all'
                  , 'module=s@'
                  , 'rt=s'
                  , 'lib=s@'
                  , 'verbose'
          );

#
# Where do dwell the RT libs
#
sub libpath {
    # 
    # If not specified in the arguments, 
    # use the constant declared for rthome
    # 
    $rt = $opts{rt} ? $opts{rt}
                    : RTHOME;

    # 
    # Make sure to look in the main lib dir AND
    # the local lib dir.
    # 
    my @default_lib_path;
    push @default_lib_path, RTHOME . $_ for qw(/lib /local/lib);

    # 
    # Otherwise if the --lib argument is found,
    # we'll just use those for libs
    # 
    return $opts{lib} ? $opts{lib}
                      : @default_lib_path;
}

use lib libpath();
use RT;

#
# This makes a list of modules to inspect
#
my   @rt_libs;
push @rt_libs, @{$opts{module}} if $opts{module}; 

if ( $opts{all} ) {

    # 
    # If -all is specified on the cli, any other --module 
    # arguments are ignored and this goes into the RT 
    # directory and looks for *.pm files excluding 
    # Overlay files.
    # 
    # TODO: make this use the command line --lib arguments
    #
    find sub {
        if ( -e and $File::Find::name =~ s/[.]pm$//) {
            my $mod = substr $File::Find::name, length($File::Find::topdir)+1;
            $mod =~ s</><::>g;
            push @rt_libs, $mod if $mod !~ m/Overlay/;
        }
    }, $rt . "/lib";
}

# 
# This takes the list of modules and feeds them
# to the show() sub.
#

if ( $opts{verbose} ) {
    $Data::Dumper::Varname = "rt_libs_";
    print Dumper( @rt_libs );
}

my @fast = map { [ $_, show($_) ]; } @rt_libs;

print Dumper( @fast );

sub show {
    my $class = shift;
    return Class::Inspector->methods( $class, 'public', 'full' );
}

__END__

=head1 NAME

rt-class-map.pl - Inspect RT's libraries and show a the details of their classes.

=head1 SCRIPT CATEGORIES

Misc

=head1 README

I created this script because it sometimes gets confusing when trying to figure out which methods are available to different types of RT objects.

=head1 OSNAMES

any

=head1 PREREQUISITES

C<Class::Inspector>

=head1 COREQUISITES

none

=head1 SYNOPSIS

=head2 OPTIONS AND ARGUMENTS

B<rt-class-map.pl>

Choose from the following options.

=over 15

=item B<[--lib> I</path/to/libs>B<]>

Explicitly specify a library directory.

=item B<[--rt> I</path/to/rthome>B<]>

Specify the path to your RT installation.

=item B<[--module> I<RT::Module>B<]>

Name of the module you wish to inspect.

=item B<[--all]>

Instead of specifying a module, inspect all of the RT modules.

=item B<[--verbose]>

Shows which modules will be inspected.

=back

=head1 ACKNOWLEDGEMENTS

Kanji, L<http://perlmonks.org/?node=Kanji>, for the find sub.

Built using L<Class::Inspector|http://search.cpan.org/~adamk/Class-Inspector-1.13/> by Adam Kennedy L<http://ali.as/>, E<lt>L<cpan@ali.as|mailto:cpan@ali.as>E<gt>.

=head1 SEE ALSO

L<Class::Inspector|http://search.cpan.org/~adamk/Class-Inspector-1.13/> 

=head1 AUTHOR

Andy Harrison

 { 
   domain   => "gmail", 
   tld      => "com", 
   username => "aharrison" 
 }

=cut