#!/usr/bin/perl

use Getopt::Long;
use Cwd;
use Config;


my ($dir, $perl, $probe, $ignore, $help, $version, $man, $archivetar,
   $strict);
$list++ if ! @ARGV;

my $options_result = GetOptions( 
                                'location=s' => \$dir,
                                'help|?' => \$help,
                                'man' => \$man,
                                'version' => \$version,
                                'perl=s' => \$perl,
                                'gtar=s' => \$mode{'gtar'},
                                'gzip=s' => \$mode{'gzip'},
                                'gunzip=s' => \$mode{'gunzip'},
                                'gmake=s' => \$mode{'gmake'},
                                'rpm=s' => \$mode{'rpm'},
                                'rpmbuild=s' => \$mode{'rpmbuild'},
                                'license=s' => \$mode{'license'},
                                'vendor=s' => \$mode{'vendor'},
                                'ftpsite=s' => \$mode{'ftpsite'},
                                'url=s' => \$mode{'url'},
                                'packager=s' => \$mode{'packager'},
                                'prefix=s' => \$mode{'prefix'},
                                'force=s' => \$mode{'force'},
                                'list' => \$list,
                                'use-archive-tar' => \$archivetar,
                                'probe' => \$probe,
                                'ignore-errors' => \$ignore,
                                'strict' => \$strict
                              );

#
# read in the main GPT environment variables
#

my $gpath = $dir;

if (!defined($gpath))
{
  $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";
}

#
# alter the include 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");
}

require Pod::Usage;

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

require Grid::GPT::GPTIdentity;



if (defined $version) {
  Grid::GPT::GPTIdentity::print_gpt_version();
  exit 0;
}

# Add path to LocalEnv module to @INC

push @INC,"$gpath/var/lib/perl";


if (defined $list) {
  die "ERROR: GPT has not been configured\n" 
    if ! defined eval "require Grid::GPT::LocalEnv;";

  require Grid::GPT::LocalEnv;

  Grid::GPT::LocalEnv::listconfig();

  exit 0;

}

require Grid::GPT::Localize;

if ( defined eval "require Grid::GPT::LocalEnv;" and ! defined $probe)
{

  require Grid::GPT::LocalEnv;


  for $m (Grid::GPT::Localize::get_rpm_settings_list()) {

    $mode{$m} = Grid::GPT::LocalEnv::get_rpm_setting($m) 
      if ! defined $mode{$m};
  }

  for $m (Grid::GPT::Localize::get_tool_list()) {

    $mode{$m} = Grid::GPT::LocalEnv::get_tool_location($m) 
      if ! defined $mode{$m};
  }

}


my $localizer = new Grid::GPT::Localize(%mode, 
                                        ignore_errors => $ignore,
                                        gpath => $gpath,
                                        systar => defined $archivetar ? undef : 1,
                                       );


if (defined $perl or defined $probe) {

  $perl = $localizer->set_perl( location => $perl, version => '5.005');
  die "ERROR: No compatible Perl executable found\n" if ! defined $perl;
  modify_gpt_scripts();

}

$localizer->probe_for_tools() if defined $probe;
$localizer->set_tools();

if ($localizer->getsub('rpm') eq 'Not Available') {
  $localizer->clear_rpm_settings();
} else {
  $localizer->set_rpm_settings();
}

for $m (Grid::GPT::Localize::get_rpm_settings_list()) {

  $localizer->setsub($m, $mode{$m}) if defined $mode{$m};

}

$localizer->check_for_usr_sbin_check();
$localizer->localize();



sub modify_gpt_scripts {
  open LIST, "$gpath/etc/gpt/gpt_scripts_list";

  my $list = <LIST>;

  my @scripts = split /\s+/, $list;

  for $s (@scripts) {
    next if $s !~ m!\w+!;

    open SCRIPT, "$gpath/sbin/$s";

    my @contents = <SCRIPT>;
    close SCRIPT;

    open SCRIPT, ">$gpath/sbin/$s";

    my $done;
    my $with_w = defined $strict ? " -w" : "";
    for $c (@contents) {
      if ($c =~ /\#!/ and ! defined $done) {
        print SCRIPT "#!$perl$with_w\n";
        $done++;
        next;
      }
        print SCRIPT $c;
    }
  }
}


__END__

=head1 NAME

B<gpt-config> - Configure the current installation of GPT

=head1 SYNOPSIS

B<gpt-config> [options]

  Help-Related Options:
    --help             brief help message
    --man              full documentation
    --location         location of the GPT installation
    --perl             location of the perl program
    --gtar             location of the GNU tar program
    --gunzip           location of the GNU unzip program
    --gmake            location of the GNU make program
    --rpm              location of the rpm program
    --rpm              location of the rpmbuild program
    --license          value used for the software license in RPM packages
    --vendor           value used for the software vendor in RPM packages
    --ftpsite          value used for the software ftp site in RPM packages
    --url              value used for the URL for the software in RPM packages
    --packager         value used for the packager of the  RPM packages
    --prefix           value used for the installation directory of RPM packages
    --use-archive-tar  Use the Archive::Tar module instead of the system tar program
    --list             list the current configuration
    --probe            probe for the various programs that GPT needs
    --ignore           ignore any missing programs

=head1 DESCRIPTION

B<gpt-config> is a runtime configuration script that is used to
localize a GPT installation.  GPT uses various programs and packaging
parameters to do its job.  These items can be listed and modified by
this script.

Configuration information for GPT is stored in the module
$GPT_LOCATION/lib/perl/Grid/GPT/LocalEnv.pm.  This module is
regenerated when gpt-config is used to change the configuration.

=head1 OPTIONS

=over 4

=item I<-builddir>

Specifies the where the source tar files should be untarred and the
code is built.


=item I<-location>

=item I<-perl>

=item I<-gtar>

=item I<-gunzip>

=item I<-gmake>

=item I<-rpm>

=item I<-rpmbuild>

Location of various programs that GPT uses.  These switches will
override any searching done by the -probe switch..


=item I<-license>

=item I<-vendor>

=item I<-ftpsite>

=item I<-url>

=item I<-packager>

=item I<-prefix>

Values that are used by GPT when generating RPM packages.  These
values are not configured when the platform does not support RPM.


=item I<-list>

List the current configuration.


=item I<-probe>

Probe for the various programs that GPT needs.  The probe examines
each directory listed in the B<$PATH> variable for these programs.


=item I<-use-archive-tar>

Skip the probes for a system tar and unzip programs.  Use Archive::Tar
module to untar source packages.  Using this modules slows down the
build noticably.

=item I<-ignore>

Ignore any missing programs and continue with the configuring.

=back

=head1 AUTHOR

Written by Chase Phillips and Michael Bletzinger.

=cut
