#! perl

#
# Do a perl check for version >= 5.005.  See 'gpt-translate-interpreter' should you
# need to alter the invocation path to a valid perl interpreter in the GPT front-end
# programs.
#

if ( ! ( defined eval "require 5.005" ) )
{
    die "GPT requires at least Perl version 5.005";
}

#
# dig the globus and gpt paths out of the user's environment variables
#

my $gpt_path = $ENV{GPT_LOCATION};
my $globus_path = $ENV{GLOBUS_LOCATION};

if ( !defined($gpt_path) && !defined($globus_path) )
{
    die("GPT_LOCATION or GLOBUS_LOCATION needs to be set before running this script");
}

if ( defined($gpt_path) )
{
    $gpath = $gpt_path;
}

if ( defined($globus_path) && !defined($gpath) )
{
    $gpath = $globus_path;
}

@INC = ("$gpath/lib/perl", "$gpath/lib/perl/$Config{'archname'}", @INC);

if ( ! ( defined eval "require Grid::GPT::GPTObject" ) )
{
    die("$gpath does not appear to hold a valid GPT installation\n");
}

if ( ! -d "$globus_path/etc/globus_packages" )
{
    die("Can't find a globus_packages directory to work on in your GLOBUS_LOCATION!\n");
}

#
# standard includes
#

use Getopt::Long;

require Pod::Usage;

#
# argument specification
#


my %queryargs = ();
# sub pod2usage {
#   print "gpt-query [-name=Name -flavor=Flavor -pkgtype=Type --help -sdk -deps=<run|sdk>] [-file=Name] [pkgname-flavor-pkgtype+] \n";
#   exit shift;
# }

my ($help, $man, $lookupfile);

GetOptions(
           'help' => \$help,
           'man' => \$man,
           'version' => \$version,
          )
  or Pod::Usage::pod2usage(0);

Pod::Usage::pod2usage(1) if $help;
Pod::Usage::pod2usage(-verbose => 2) if $man;


require Grid::GPT::GPTIdentity;
Grid::GPT::GPTIdentity::print_gpt_version() if defined $version;

# require Grid::GPT::Query;
require Grid::GPT::Installation;

my $installation = 
  new Grid::GPT::Installation(
                              pkgdir => "$globus_path/etc/globus_packages",
                             );

Pod::Usage::pod2usage(-verbose => 0, 
                      -exitval => 1,
                      -output =>  \*STDERR,
                      -message => 
                      "ERROR: no dependency types specified!") if ! @ARGV;

for my $d( @ARGV ) {
  $installation->set_depenv($d);
}

open (OUT, ">./deptable.html");

select(OUT);

print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">
<html>
  <head>
    <title>Globus Packages</title>
  </head>

  <body>

    <h1>Installation Dependency Tree</h1>\n";

$installation->printtablehtml();

print "  </body>
</html>
";

select(STDOUT);
close OUT;

=head1 NAME

B<gpt-deps> - Reports on an installation's package dependencies

=head1 SYNOPSIS

gpt-deps [ -help -man -version ] DependencyTypes

=head1 DESCRIPTION

B<gpt-deps> is an experimental script which prints out an html file
that contains a hyperlinked report of dependencies for packages
installed at $GLOBUS_LOCATION.  The output file is called
./deptable.html.  One or more of the following dependency types can be
reported:

  Runtime - Packages needed at runtime
  Setup - Packages needed to set up localization data such as 
          configuration files.
  Build - Packages needed to build other packages.

If you are confused by the dependency types, simply run 'gpt-deps
Runtime Setup Build' and view ./deptree.html with your favorite
browser. Be sure to set $GLOBUS_LOCATION.


=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=item B<-version>

Prints the version of GPT and exits.

=back

=head1 BUGS

Future versions will provide more flexibility in inputs, and output
formats.

=head1 SEE ALSO

gpt-install(1) gpt-verify(1) gpt-query(1) gpt-postinstall(1)

=head1 AUTHOR

Michael Bletzinger E<lt>mbletzin.ncsa.uiuc.eduE<gt>

=cut
