#! /usr/bin/env perl

# Automatic generation of pkg-config support files for PLplot
#
# Copyright (C) 2004  Rafael Laboissiere
#
# This file is part of PLplot.
#
# PLplot is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# PLplot is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
# License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with the file PLplot; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


$lib_tag = 'd';
$float = ($lib_tag eq "d") ? "double" : "single";
$top_builddir = '../';

$pc_file_template = 'prefix=
exec_prefix=
libdir=${exec_prefix}/lib
includedir=${prefix}/include/plplot
drvdir=${libdir}/plplot5.6.1/driversd

Name: PLplot #NAME#
Description: Scientific plotting library (#DESC#' . $float . ' precision)
Requires: #REQ#
Version: 5.6.1
Libs: -L${libdir} -l#LIB#  #DEPLIBS#
Cflags: -I${includedir} #INC#
';

$stem = "plplot";
$master_pkg = "";

%pc_files_info = (
  $master_pkg => { NAME => '',
                   DESC => '',
                   LIB  => "$stem$lib_tag",
                   INC  => '' },

  'c++' => { NAME => 'C++',
             DESC => 'C++ bindings, ',
             LIB  => "${stem}cxx$lib_tag",
             INC  => '' },

  'f77' => { NAME => 'F77',
             DESC => 'Fortran77 bindings, ',
             LIB  => "${stem}f77$lib_tag",
             INC  => '' },

  'f95' => { NAME => 'F95',
             DESC => 'Fortran95 bindings, ',
             LIB  => "${stem}f95$lib_tag",
             INC  => '' },

  'gnome2' => { NAME => 'Gnome',
       	     DESC => 'PLplot for Gnome/GTK, ',
             LIB  => "${stem}gnome2$lib_tag",
             INC  => '' },

  'tcl' => { NAME => 'Tcl/Tk',
             DESC => 'Tcl/Tk bindings, ',
             LIB  => "${stem}tcltk$lib_tag",
             INC  => ' ' });


for $pkg (keys %pc_files_info) {

 $contents = $pc_file_template;

  for $sub ('NAME', 'DESC', 'LIB', 'INC') {
    $contents =~ s/#$sub#/$pc_files_info{$pkg}->{$sub}/;
  }

  $lib = $pc_files_info{$pkg}->{LIB};
  $libname = "lib$lib";
  @lai_files = `find $top_builddir -name $libname.lai`;
  warn "More than one $libname.lai file found in build tree"
    if $#lai_files > 0;
  if ($#lai_files < 0) {
    warn "Cannot find $libname.lai";
    next;
  }
  
  $lai_file = @lai_files[0];

  if (not open (LAI, "< $lai_file")) {
    warn "Cannot open $lai_file";
    next;
  }

  while (<LAI>) {
    if (/^dependency_libs='\s*(.*)\s*'/) {
      $deplibs_str = $1;
      last;
    }
  }

  %normlibs = map {
    ((m{(.*)/lib([^/]+)\.la} ? "-L$1 -l$2" : $_), 0);
  } split (/\s+/, $deplibs_str);

  if ($pkg eq $master_pkg) {

    $contents =~ s/#REQ#//;
    %master_libs = %normlibs;
    $deplibs = join (" ", keys %normlibs);

  }
  else {

    $contents =~ s/#REQ#/$stem$lib_tag/;
    $deplibs = "";
    for $k (keys %normlibs) {
      $deplibs .= " $k" if not exists $master_libs{$k};
    }

  }

  $contents =~ s/#DEPLIBS#/$deplibs/;

  $pc_file = "$stem$lib_tag" . (not ($pkg eq "") ? "-" : "") . "$pkg.pc";

  open (PC, "> $pc_file") or die "Cannot open $pc_file";
  print PC $contents;
  close PC;

  print "Generated $pc_file\n"

}


syntax highlighted by Code2HTML, v. 0.9.1