#! /usr/bin/perl


my ($prefix, $globus_prefix, $exec_prefix, $bindir, $sbindir, $libdir);
my ($datarootdir, $datadir, $perlmoduledir, $gptdatadir);
my ($gptexecsharedir, $amdir, $pkg_confdir, $aclocaldir);

if (exists $ENV{GPT_LOCATION})
{
    $prefix = $ENV{GPT_LOCATION};
}
else
{
    $prefix = "/usr";
}

if (exists $ENV{GLOBUS_LOCATION})
{
    $globus_prefix = $ENV{GLOBUS_LOCATION};
}
else
{
    $globus_prefix = "/usr";
}

$exec_prefix = "${prefix}";
$bindir = "${exec_prefix}/bin";
$sbindir = "${exec_prefix}/sbin";
$libdir = "${exec_prefix}/lib";
$datarootdir = "${prefix}/share";
$datadir = "${prefix}/share";
$perlmoduledir = "/usr/share/perl5";
$gptdatadir = "${datadir}/globus/gpt";
$gptexecsharedir = "${datadir}/globus/gpt";
$amdir = "${datadir}/globus/amdir";
$pkg_confdir = "${datadir}/globus/gpt";
$aclocaldir = "${datadir}/globus/aclocal";

unshift(@INC, "${perlmoduledir}");

use strict;
use Getopt::Long;
use Config;

#
# Do a perl check for version >= 5.005.
#

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

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

require Pod::Usage;

my $VERSION = 0.01;
my $srcdir = cwd();
my $flavor = "noflavor";
my $verbose = 0;
my ($help, $man);

# sub pod2usage {
#   my $ex = shift;
#   print "gpt_generate_bin_pkg_data [-verbose -help] -flavor=build_flavor src_metadata_file\n";
#   exit $ex;
# }

GetOptions( 'flavor=s' => \$flavor, 'verbose=i' => \$verbose, 'help' => \$help) 
  or Pod::Usage::pod2usage(1);

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

require Grid::GPT::PackageFactory;
require Grid::GPT::V1::Definitions;
use Cwd;

my $srcfile = shift;
my $factory = new Grid::GPT::PackageFactory;
my $pkg = $factory->type_of_package($srcfile);
$pkg->read_metadata_file($srcfile);

my @pkgtypes = @Grid::GPT::V1::Definitions::package_types;

for my $typ (@pkgtypes) {
  next if $typ eq 'src';
  my $type_flavor = $flavor;
  $type_flavor = "noflavor" 
    if defined ($Grid::GPT::V1::Definitions::noflavor_pkg_types{$typ});
 
  # Don't put down pkgdata if there isn't a filelist
  next unless ( -f "${type_flavor}_${typ}.filelist" );

  my $out = $srcfile;
  $out =~ s/_src/_$ {type_flavor}_$typ/;
  my $bin_pkg = $pkg->convert_metadata($typ, $flavor);
  $bin_pkg->output_metadata_file($out);
}

__END__
