#  Copyright (c) 1997-2009
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program 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 General Public License for more details.
#-------------------------------------------------------------------------------
#  $Project: polymake $$Id: locate_build_dir 9362 2009-10-17 22:46:24Z gawrilow $

unless (defined ($Arch=$ENV{Arch})) {
   if ($^O eq "darwin") {
      if (defined ($FinkBase=$ENV{FINK})) {
	 unless (-x $FinkBase && $FinkBase =~ s|/bin/fink$||) {
	    die "ERROR: FINK=$ENV{FINK} does not point to the main fink program\n";
	 }

      } elsif ((($BuildDir, my $dummy)=glob( ($InstallTop && "$InstallTop/")."build.*")) == 1
	       and -f "$BuildDir/conf.make") {
	 # exactly one build tree and no explicit parameters: let's suppose that's it.
	 # the values of Arch and ARCHFLAGS will be read from the configuration file
	 return;

      } else {
	 # can't load rules yet, therefore must repeat locate_in_path here
	 foreach (split /:/, $ENV{PATH}) {
	    if (-x "$_/fink" && !-d _) {
	       require Cwd;
	       $FinkBase=Cwd::abs_path($_);
	       $FinkBase=~s|/bin$||;
	       last;
	    }
	 }

	 if (!defined($FinkBase)) {
	    if (-f "/sw/etc/fink.conf") {
	       $FinkBase="/sw";
	    } else {
	       die <<'.';
ERROR: fink command not found
Please make sure fink is installed and accessible via PATH;
alternatively specify its location explicitly:

  make configure FINK=/path/to/your/fink
.
	    }
	 }
      }

      open FINKCONF, "$FinkBase/etc/fink.conf" or die "ERROR: can't read $FinkBase/etc/fink.conf: $!\n";
      while (<FINKCONF>) {
	 if (/^\s*basepath:\s*(\S+)/i) {
	    $FinkBase=$1; last;
	 }
      }
      close FINKCONF;
      if (-f "$FinkBase/lib/libgmp.dylib") {
	 ($Platform)= `lipo -info $FinkBase/lib/libgmp.dylib` =~ /architecture: (\S+)/;
      } else {
	 die <<'.';
ERROR: fink package gmp-shlibs seems to be missing.  Further configuration is not possible.
Please install the packages gmp and gmp-shlibs and repeat the 'make configure' step.
.
      }
      $ARCHFLAGS="-arch $Platform";
      $Arch="darwin.$Platform";

   } else {
      if ($^O =~ /aix|rs6000|ibm/i) {
	 $Arch=$Config{archname};
      } else {
	 ($Platform)= `uname -p` =~ /(\S+)/g;
	 if ($Platform eq "unknown") {
	    ($Platform)= `uname -m` =~ /(\S+)/g;
	 }
	 $Arch=$Platform;
      }
   }
}

$BuildDir=($InstallTop && "$InstallTop/")."build.$Arch";
-f "$BuildDir/conf.make" or undef $BuildDir;

sub export_vars {
   join(" ", map { length($$_) ? "$_='$$_'" : () } qw(BuildDir Arch Platform FinkBase ARCHFLAGS))
}


# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
