#! 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;
my $gpt_path = $ENV{GPT_LOCATION};
my $globus_path = $ENV{GLOBUS_LOCATION};
my $verbose = 0;

if ( !defined($gpt_path) && !defined($globus_path) )
{
    die("GPT_LOCATION needs to be set before running this script");
}

if ( defined($gpt_path) )
{
    $gpath = $gpt_path;
}

if ( defined($globus_path) && !defined($gpath) )
{
    $gpath = $globus_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;

# test for the existance of a local file sorting module
my $filelistclass;
if (-f "MyFilelists.pm") {
  require MyFilelists;
  $filelistclass = "MyFilelists";
} else {
  require Grid::GPT::MyFilelists;
  $filelistclass = "Grid::GPT::MyFilelists";
}

use Cwd;

my $VERSION = 0.01;
my $srcdir = cwd();
my $flavor;
my ($help, $man, $pkgfile);

# sub pod2usage {
#   my $ex = shift;
#   print "gpt_sort_file [-verbose -help] -flavor=build_flavor -pkgfile=pkg_data_src.gpt master_filelist\n";
#   exit $ex;
# }

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

Pod::Usage::pod2usage(0) if $help;

my $masterlist = shift;
my $static = shift;

if (!defined($masterlist)) {
  print STDERR "ERROR: Need master filelist filename \n";
  Pod::Usage::pod2usage(1);
}

if (!defined($flavor)) {
  print STDERR "ERROR: Need build flavor \n";
  Pod::Usage::pod2usage(1);
}

require Grid::GPT::PackageFactory;
require Grid::GPT::V1::Package;
require Grid::GPT::V1::Definitions;

my $mangling = 1;

if (defined $pkgfile) {
  my $factory = new Grid::GPT::PackageFactory;
  my $pkg = $factory->type_of_package($pkgfile);
  $pkg->read_metadata_file($pkgfile);
  
  if (defined $pkg->ColocateLibraries()) {
    $mangling = $pkg->ColocateLibraries() ne 'no' ? 1 : undef;
  }
}

open (MASTER, $masterlist) or die "ERROR: Could not read $masterlist\n";

require Grid::GPT::Locations;
my $locations = new Grid::GPT::Locations();

my $prefix = $locations->{'installdir'};

# The filelist won't include the installation directory,
# but the pkgdir value will.  Strip it out so we can inspect
# the filelists later.
my $pkgdatasubdir = "$locations->{'pkgdir'}";
$pkgdatasubdir =~ s!$prefix/!!;
my $altpkgdatasubdir = "$locations->{'altpkgdir'}";
$altpkgdatasubdir =~ s!$prefix/!!;

my @list;
for my $line (<MASTER>) {
  $line =~ s!^.*$prefix!!;
  push @list, $line;
#  print $line;
}

if ($masterlist =~ m!^(.+)/[^/]+$!) {
  $prefix = $1;
} else {
  $prefix =".";
}


my $sort = new $filelistclass(list => \@list, flavor => $flavor,
                              mangling => $mangling,
                             );

my %funcs = ("data", sub{ $sort->data_files()}, 
	     "dev", sub {$sort->dev_files()}, 
	     "doc", sub {$sort->doc_files()},
	     "pgm", sub {$sort->pgm_files()},
	     "pgm_static", sub {$sort->pgm_static_files()},
	     "rtl", sub {$sort->rtl_files()});

for my $typ (sort keys %funcs) {
  next if ( ( $typ eq "pgm" ) && ( $static eq "static" ) );
  next if ( ( $typ eq "pgm_static" ) && ( $static eq "shared" ) );

  my $outlines = &{$funcs{$typ}}();

  if (@$outlines) {
    my $type_flavor = $flavor;
    $type_flavor = "noflavor" 
      if defined ($Grid::GPT::V1::Definitions::noflavor_pkg_types{$typ});

    # If this filelist only has package metadata, don't write it!
    my @notpkgdata;
    @notpkgdata = grep { ! m!$pkgdatasubdir!}
      grep { ! m!$altpkgdatasubdir!} @$outlines;
    next if (! @notpkgdata );

    my $out = "$prefix/$ {type_flavor}_$ {typ}.filelist";
    open (OUT, ">$out") or die "ERROR: could not open $out\n";
    for (@$outlines) {
      print OUT "$_\n";
    }
    close OUT;
  }
}


__END__
