#! perl
use strict;
use Getopt::Long;
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 or GLOBUS_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");
}

require Pod::Usage;

my ($conf, $core, $std, $all, $installdir);
my $help = 0;
my $man = 0;
my $VERSION = "0.01";
my $version;

# sub pod2usage {
#   my $ex = shift;
#   print "gpt-flavor-configuration [ -installdir=<DIR> -standard -all  -conf=<flavor configuration file>]
# ";
#   exit $ex;
# }


GetOptions( 'conf=s' => \$conf, 
            'installdir=s' => \$installdir, 
            'location=s'=> \$installdir,
            'help' => \$help, 
            'standard' => \$std,
            'man' => \$man,
            'all' => \$all,
            '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::V1::BuildFlavors;
require Grid::GPT::Locations;
my $locations = new Grid::GPT::Locations(
                                         installdir => $installdir,
                                        );


my $flavors;

die "ERROR: Use either -all or -standard not both\n" 
  if defined $std and defined $all;

$core = ((defined $std) or (defined $all)) ? 1 : 0;

if ($core) {
  $flavors = new Grid::GPT::V1::BuildFlavors(core => 1, cfg => $conf, std => $std);
} else {
  $flavors = new Grid::GPT::V1::BuildFlavors(installed => 1,
                                             locations => $locations
                                            );
}

for my $l (@{$flavors->{'flavors'}}) {
  my $f = $flavors->{$l}->build_core_configure_line();
  $f->{'env'} =~ s!;!;\\\n!g;
  print "$f->{'flavor'}:\n";
  for my $e (sort keys %{$f->{'env'}}) {
    print "\t$e=$f->{'env'}->{$e}; \\\n";
  }
  print "\tconfigure $f->{'switches'}\n";
}



=head1 NAME

B<gpt-flavor-configuration> - Permutes the build flavor choices found in the flavor configuration file. 

=head1 SYNOPSIS

B<gpt-flavor-configuration> [-installdir=<DIR> -standard -all  -conf=<flavor configuration file>]


=head1 DESCRIPTION

B<gpt-flavor-configuration> produce a list of build flavors.  The
default is a list of installed flavors.  If the B<-standard> or
B<-all> flags are used then the script permutes the flavor choices
listed in the flavor configuration file. The file
$GPT_LOCATION/etc/globus_package_management/globus_flavor_labels.conf
is the default.  The possible fields for a flavor are described in the
file
<cite>$GPT_LOCATION/etc/globus_package_management/globus_flavors.dtd</cite>

=head1 OPTIONS

=over 8

=item B<-conf="flavor configuration file">

Permute the choices found in this file rather than the default.

=item B<-all>

Permute all possible flavor choices..

=item B<-standard>

Permute the standard flavor choices.

=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 AUTHOR

Michael Bletzinger E<lt>mbletzin.ncsa.uiuc.eduE<gt> and Eric Blau
E<lt>eblau.ncsa.uiuc.eduE<gt>

=cut
