#!  perl
use strict;
use Getopt::Long;
use File::Find;
use Cwd;
use Config;

#
# 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";
}

my $gpath = $ENV{GPT_LOCATION};

if (!defined($gpath))
{
  $gpath = $ENV{GLOBUS_LOCATION};

}

if (!defined($gpath))
{
   die "GPT_LOCATION needs to be set before running this script"
}

@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");
}

my $VERSION = "0.01";

require Pod::Usage;

# declare all of the option variables
my $startdir = cwd();
my $man = 0;
my $help = 0;
my $pkgdir = "$startdir/binaries";
my ($verbose, $version, $rpm, $all, $logfile, $buildno, $debug,
    $installdir, $rpmprefix, $rpmlicense);

#
# if rpmprefix is not passed in, then it should be set to "/usr/grid" by
# the Grid::GPT::Archive module
#

# process the -option options

GetOptions( 
           'installdir=s' => \$installdir,
           'location=s' => \$installdir,
           'pkgdir=s' => \$pkgdir,
           'buildnumber|buildno=i' => \$buildno,
           'native' => \$rpm,
           'rpmprefix=s' => \$rpmprefix,
           'rpmlicense=s' => \$rpmlicense,
           'all' => \$all,
           'log' => \$logfile,
           'verbose!' => \$verbose, 
           'debug' => \$debug, 
           'help|?' => \$help,
           'man' => \$man,
           'version' => \$version) 
  or Pod::Usage::pod2usage(0);

Pod::Usage::pod2usage(0) 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::PkgMngmt::Archive;
require Grid::GPT::FilelistFunctions;
require Grid::GPT::Installation;

my @pkgnames = @ARGV;


Pod::Usage::pod2usage(-verbose => 0, 
                      -exitval => 1,
                      -output =>  \*STDERR,
                      -message => 
                      "ERROR: No packages specified!") 
  if ! @pkgnames and ! defined $all; 
# Create package directory

  Grid::GPT::FilelistFunctions::mkinstalldir("$pkgdir");
  $pkgdir = Grid::GPT::FilelistFunctions::abspath($pkgdir);


my $log = new Grid::GPT::PkgMngmt::Inform(verbose => $verbose, log => $logfile, debug => $debug);

my $locations = new Grid::GPT::Locations(
                                         installdir => $installdir,
                                        );

my $installation = 
  new Grid::GPT::Installation(locations => $locations, with_filelists => 1);
  
my $packager = 
  new Grid::GPT::PkgMngmt::Archive(locations => $locations,
                                   gpt_rpm_prefix => $rpmprefix,
                                   buildno => $buildno,
                                   verbose => $log->{'verbose'},
                                   license => $rpmlicense,
                                   rpm => $rpm,
                                   log=> $log);
  
my $pkgs;
if (defined $all) {
  @$pkgs = grep { $_->pkgname() ne 'globus_core' } @{$installation->pkgs()};
} else {
  $pkgs = [];
  for my $pn (@pkgnames) {
    my $mypkgs = $installation->query(pkgname => $pn);
die "ERROR: Package $pn not found\n" if( ! @$mypkgs );
    push @$pkgs, @$mypkgs;
  }
}
chdir $pkgdir;
$packager->archive($pkgs);
chdir $startdir;

exit 0;

__END__


=head1 NAME

B<gpt-pkg> - Creates flavored binary packages out of an installation 

=head1 SYNOPSIS

B<gpt-pkg> [options] packages

  Options:

     -verbose                          Print copious output
     -help                             Print usage
     -man                              Print man page.
     -version                          Print GPT version.
     -native                           Create native pkgs as well as GPT pkgs (RPMs only).
     -all                              Archive everything in the package directory..
     -installdir=PATH                  Override $GLOBUS_LOCATION
     -pkgdir=PATH                      Directory to put the packages
     -rpmprefix=PATH                   Absolute path encoded in the RPM.
     -buildnumber=NUMBER               Build number used for pgm_static packages
     -rpmlicense=LABEL                 License Label added to the RPM header
     [packages]                        List of packages to be archived

=head1 DESCRIPTION

B<gpt-pkg> Creates binary packages from an installation.  The
installation needs to contain GPT packaging data files. An
installation is created by running I<gpt-build> on a collection of
source packages or bundles.

=head1 FILES IN PGM PACKAGES

Installed files such as executables that are found in pgm and
ptm_static packages will overwrite one another when multiple flavors
of the same package are built by gpt-build. To preserve multiple
flavors of these files, gpt-build copies them into flavored
subdirectories.  For example, the program $GLOBUS_LOCATION/bin/foo
will be copied into the location $GLOBUS_LOCATION/bin/gcc32/shared if
it was built with dynamically with the gcc32 flavor.  B<gpt-pkg>
retrieves these files and copies them back to their proper location
before packaging them.  Thus gpt-pkg will also overwrite these files
as it archives the different flavors of a pgm package.

=head1 OPTIONS

=over 8

=item B<-installdir>

Specify the directory in which the installed files currently live.  If
this argument is not supplied, the value of the environment variable
GLOBUS_LOCATION is used instead.

=item B<-all>

Packages all of the binaries in an installation.

=item B<-native>

Creates a package in the native packaging format.  Currently only rpm
is supported.

=item B<-rpmprefix=<path_to_installation>>

This should be set to the path to your GLOBUS_LOCATION.  GPT will use a
value of "/usr/grid" if this option is not specified.

=item B<-rpmlicense=<label>>

Provides an alternate copyright label for the rpms. The default is
whatever GPT was configured with.

=item B<-verbose>

Prints out all of the build messages.

=item B<-log=FILE>

Appends all of the build messages to FILE. This option also sets -verbose. 

=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 pgm and pgm_static packages

gpt-build copies the files for these packages in a flavored location
because different flavors of these types of packages will conflict
with each other.  B<gpt-pkg> restores these files when it packages pgm
and pgm_static types.

=head1 SEE ALSO

gpt-bundle(1) gpt-install(1) gpt-build(1)

=head1 AUTHOR

Michael Bletzinger E<lt>mbletzin.ncsa.uiuc.eduE<gt> and Eric Blau
E<lt>blau.mcs.anl.govE<gt>

=cut

