#!/usr/bin/perl

use strict;
use Getopt::Long;
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 = "/usr";
}
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;

use Cwd;

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

# sub pod2usage {
#   my $ex = shift;
#   print "gpt_save_flavor [-verbose -help] -flavor=flavor <build-parameters file>";
#   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;

my $file = shift;

if (!defined $flavor or ! defined $file) {
  Pod::Usage::pod2usage(1);
}

require Grid::GPT::V1::FlavorDefinition;

my $obj = new Grid::GPT::V1::FlavorDefinition(name => $flavor, 
                                        build_parameters => $file);

die "ERROR: vendorcc not supported\n" 
  if $flavor =~ m!vendorcc! and ! defined $obj->{'vendorcc'};

die "ERROR: gcc not supported\n" 
  if $flavor =~ m!gcc! and ! defined $obj->{'gcc'};

$obj->write_xml(filename => "flavor_$flavor.gpt");


__END__
