#! /usr/bin/perl

# This script parses a source package gpt metadata file ($ARGV[0]) and
# generates a pkg-config file containing equivalent metadata with package names
# modified so that _ becomes -. The resulting pkg-config file is written
# to pkg_data_src.pc.in in the directory where $ARGV[0] was located. This
# must be run through an autoconf substitution (or equivalent) to resolve
# paths to $prefix, $exec_prefix, $libdir, $includedir, and $flavorincludedir
#
# This script ignores version aging and assumes that any version >= than the
# version named in the metadata is compatible.
# 
# This script is based on one by Mattias Ellert, but rewritten to use
# XML::Parser for compatibility with certain linux distributions without
# requiring perl modules that are in their package sets

use XML::Parser;

my $parser = new XML::Parser(Style => 'Objects', Pkg => 'GPT');
my $doc = $parser->parsefile($ARGV[0]);
my $gptmeta = $doc->[0];

my $gptname = $gptmeta->{Name};
my $gptagingver = (grep { ($_->isa('GPT::Aging_Version')) } @{$gptmeta->{Kids}})[0];
my $gptmajorver = $gptagingver->{Major};
my $gptminorver = $gptagingver->{Minor};

my $description_element = (grep
                    { ($_->isa('GPT::Description')) }
                    @{$gptmeta->{Kids}})[0];
my $description = $description_element->{Kids}->[0]->{Text};

my %deps;
my $srcpkg = (grep
                    { ($_->isa('GPT::src_pkg')) }
                    @{$gptmeta->{Kids}})[0];
my $sourcedeps_element = (grep
                    { ($_->isa('GPT::Source_Dependencies')) }
                    @{$srcpkg->{Kids}});
for my $bindep (grep
                    { ($_->isa('GPT::Source_Dependencies')) }
                    @{$srcpkg->{Kids}})
{
    my $type = $bindep->{Type};
    next unless $type eq 'compile';

    for my $dep (grep { $_->isa('GPT::Dependency') } @{$bindep->{Kids}})
    {
        my $depname = $dep->{Name};
        $depname =~ tr/_/-/;
        my $depversion = (grep {$_->isa('GPT::Version')} @{$dep->{Kids}})[0];
        my $depverelement = (grep {$_->isa('GPT::Simple_Version')}
            @{$depversion->{Kids}})[0];
        my $depver = $depverelement->{Major};
        $deps{$depname} = $depver;
    }
}

my $buildenv = (grep {$_->isa('GPT::Build_Environment')} @{$srcpkg->{Kids}})[0];

my $cflagselem = (grep { $_->isa('GPT::cflags') } @{$buildenv->{Kids}})[0];
my $cflags = $cflagselem->{Kids}->[0]->{Text};

my $extincelem = (grep { $_->isa('GPT::external_includes') } @{$buildenv->{Kids}})[0];
my $extinc = $extincelem->{Kids}->[0]->{Text};

my $pkglibselem = (grep { $_->isa('GPT::pkg_libs') } @{$buildenv->{Kids}})[0];
my $pkglibs = $pkglibselem->{Kids}->[0]->{Text};

my $extlibselem = (grep { $_->isa('GPT::external_libs') } @{$buildenv->{Kids}})[0];
my $extlibs = $extlibselem->{Kids}->[0]->{Text};


my $name = $gptname;
$name =~ tr/_/-/;

$description =~ s!^\s+|\s+$!!g;
$description =~ s!\s+! !g;

my $version = "$gptmajorver.$gptminorver";
my $libs = "-L\${libdir} $pkglibs";
$libs =~ s!^\s+|\s+$!!g;
$libs =~ s!\s+! !g;

$extlibs =~ s!^\s+|\s+$!!g;
$extlibs =~ s!\s+! !g;

$cflags = "$cflags -I\${includedir} -I\${flavorincludedir} $extinc";
$cflags =~ s!^\s+|\s+$!!g;
$cflags =~ s!\s+! !g;

my $outfile = $ARGV[0];
if ($outfile =~ m|/|)
{
    $outfile =~ s|/[^/]*$|/pkg_data_src.pc.in|;
}
else
{
    $outfile = 'pkg_data_src.pc.in';
}


# Add fake dependency for other things that were detected in autoconf using
# pkg-config autoconf macros
$deps{'@GPT_PKGCONFIG_DEPENDENCIES@'} = 0;

local(*OUTFILE);
open(OUTFILE, ">$outfile");
print OUTFILE "prefix=\@prefix\@\n";
print OUTFILE "exec_prefix=\@exec_prefix\@\n";
print OUTFILE "libdir=\@libdir\@\n";
print OUTFILE "includedir=\@includedir\@\n";
print OUTFILE "GLOBUS_FLAVOR_NAME=\@GLOBUS_FLAVOR_NAME\@\n";
print OUTFILE "flavorincludedir=\@flavorincludedir\@\n";
print OUTFILE "\n";
print OUTFILE "Name: $name\n";
print OUTFILE "Description: Globus Toolkit - $description\n";
print OUTFILE "Version: $version\n";
print OUTFILE "Requires.private:";
while (($key, $value) = each(%deps)) {
        print OUTFILE ($value > 0 ? " $key >= $value" : " $key");
}
print OUTFILE "\n";
print OUTFILE "Libs: $libs\n";
print OUTFILE "Libs.private: $extlibs\n";
print OUTFILE "Cflags: $cflags\n";
