#!/usr/bin/perl
# -*- perl -*-

eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if 0;

use strict;
use FindBin qw($RealBin);

my $srcpath='/home/mandrake/rpm/BUILD/DyALog-1.12.0';
my $prefix="/usr";
my $exec_prefix="/usr";

my $includedir="/usr/include";
my $libdir="/usr/lib";

my $pkglibdir;
my $datadir;
my $pkgdatadir;
my $pkgincludedir;

my $dyalogdir;

my $pkg;
my $pkginfo = ();

sub set_vars {
  $dyalogdir="${prefix}/lib/DyALog";
  $libdir = "${dyalogdir}";
  $includedir = "${dyalogdir}";
  $pkglibdir="${dyalogdir}";
  $datadir = "${dyalogdir}";
  $pkgdatadir="${dyalogdir}";
  $pkgincludedir="${dyalogdir}";
}

set_vars;

my $dev=0;

# Detect if the script is run from the package or the installation place
$dev=1 if ($RealBin =~ m{^$srcpath});

usage(1) unless (@ARGV);

while (@ARGV) {
  $_=shift;

  if (/^--help$/) {
    usage(0);
    next;
  }

  if (/^--prefix=(.*)/) {
    $prefix=$1;
    set_vars;
    next;
  }

  if (/^--exec-prefix=(.*)/) {
    $exec_prefix=$1;
    next;
  }

  if (/^--prefix$/) {
    echo($prefix);
    next;
  }

  if (/^--exec-prefix$/) {
    echo($exec_prefix);
    next;
  }

  if (/^--pkg(?:=|\s+)(\S+)/){
    $pkg = $1;
    $ENV{PKG_CONFIG_PATH} = "${prefix}/lib/pkgconfig/:${dyalogdir}/${pkg}/:$ENV{PKG_CONFIG_PATH}";
##    print "SEARCH IN $ENV{PKG_CONFIG_PATH}\n";
##    print `pkg-config --exists $pkg && echo "yes"`;
    unless (`pkg-config --exists $pkg && echo "yes"`) {
      print <<EOF;
DyALog Package $pkg not found.
EOF
      exit 1;
    }
    next;
  }

  if (/^--version$/) {
    if ($pkg) {
      chomp(my $answer = `pkg-config $pkg --modversion --silence-errors`);
      echo($answer);
    } else {
      echo('1.12.0');
    }
    next;
  }

  if (/^--cflags/) {
    if ($pkg) {
      chomp(my $answer = `pkg-config $pkg --cflags --silence-errors`);
      echo($answer);
    } else {
      echo($dev ? "-I$srcpath/Runtime" : " -I$pkgincludedir");
    }
    next;
  }

  if (/^--dflags/) {
    if ($pkg) {
      chomp(my $answer = `pkg-config $pkg --cflags --silence-errors`);
      $answer =~ s/-I(\S+)/-I $1/og;
      echo($answer);
    } else {
      echo($dev ? "-I $srcpath/Runtime" : " -I $pkgincludedir");
    }
    next;
  }

  if (/^--libs/) {
    if ($pkg){
      chomp(my $answer = `pkg-config $pkg --libs --silence-errors`);
      echo($answer);
    } else {
      echo($dev ? "$srcpath/Runtime/libdyalog.la $srcpath/Builtins/libbuiltins.la" : "-L$pkglibdir -lbuiltins -ldyalog -lgc");
    }
    next;
  }

  if (/^--cc/) {
    echo($dev ? "/home/mandrake/rpm/BUILD/DyALog-1.12.0/Compiler/dyacc" : "$exec_prefix/bin/dyacc");
    next;
  }

  if (/^--data/) {
    echo($dev ? "/home/mandrake/rpm/BUILD/DyALog-1.12.0/Templates" : "$pkgdatadir");
    next;
  }

  usage(1) unless ($pkg);
  
}

exit 0;

sub usage($)
{
  my $status = shift;
    print <<EOF ;
Usage: dyalog-config [OPTION]

Known values for OPTION are:

  --prefix=DIR		change DyALog prefix [default $prefix]
  --exec-prefix=DIR	change DyALog exec-prefix [default $exec_prefix]
  --data		print path to DyALog data (for templates)
  --libs		print library linking information
  --cflags		print pre-processor and compiler flags
  --dflags		print DyALog compiler flags
  --cc                  print DyALog compiler path
  --help		display this help and exit
  --version		output version information
EOF

    exit $status
}

sub echo {
  print "@_\n";
}

