#! /usr/bin/perl

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

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

# For some systems, the dev pkg will require a shared library symlink that will
# not be used at runtime
my $extradev_map =
{
    'linux' => [ '\.so$' ],
    'gnu' => [ '\.so$' ],
    'gnukfreebsd' => [ '\.so$' ],
    'solaris' => [ '\.so$' ],
    'darwin' => [ '^lib[^\.]*.dylib$' ],
};

foreach my $libvar (@{$library_map->{$^O}})
{
    &append_path(\%ENV, $libvar, $Globus::Core::Paths::libdir);
}

my ($old, $shared) = (0,0);
if (scalar(@ARGV) == 3)
{
    if ($ARGV[2] eq '-old')
    {
        $old = 1;
    }
    elsif ($ARGV[2] eq '-dynamic')
    {
        $shared = 1;
    }
    else
    {
        print STDERR "Usage: $0 library directory [-old|-dynamic]\n";
        exit 1;
    }
}
else
{
    $old = $shared = 1;
}

open(FILE, "< $ARGV[0]")   or die "Can't open $ARGV[0] for reading: $!\n";
 
my @filecontents= <FILE>; 
my ($libnames, @rest) = grep /^library_names/, @filecontents; 
$libnames =~ s/'//g;
$libnames =~ s/\n//g;
my ($ident, $libs)= split /=/, $libnames; 
my @sonames = split / /, $libs;
my %seen = ();
my @uniq;
foreach $item (@sonames)
{
    push(@uniq, $item) unless $seen{$item}++;
}
SONAME: foreach $soname (@uniq)
{
    chomp $soname;
    if ((exists $extradev_map->{$^O}))
    {
        foreach my $re (@{$extradev_map->{$^O}})
        {
            if ($soname =~ $re)
            {
                print "$ARGV[1]/$soname\n" if ($old);
                next SONAME;
            }
        }
    }
    print "$ARGV[1]/$soname\n" if ($shared);
}

($staticnames, @rest) = grep /^old_library/, @filecontents;
$staticnames =~ s/'//g;
($ident, $staticname)= split /=/, $staticnames;
chomp $staticname;
print "$ARGV[1]/$staticname\n" if ($old && ($staticname ne ''));
print "$ARGV[1]/$ARGV[0]\n" if ($old);
