#  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: configure.pl 9369 2009-10-18 22:52:45Z gawrilow $

BEGIN {
   if ($] < 5.008) {
      print STDERR <<".";
polymake requires perl version not lower than 5.8;
your perl interpreter says it is $].

Please upgrade your perl installation;
if you already have an up-to-date perl interpreter somewhere else,
you can specify its location on the command line:

make configure PERL=/path/to/my/new/perl
.
      exit(1);
   }
}

my (@export, @noexport, $ProjectTopLink, $silent, $quick, $repeat, $bootstrap, $perlxlib, $TOP);

use Config;
use Cwd;
use IO::Handle;
use POSIX;

use strict 'vars', 'subs';

BEGIN {
   @noexport=qw( CC CXX Cflags CXXflags CsharedFlags CXXOPT CXXDEBUG
		 GCCversion ICCversion COMOversion CCache
		 LDflags LDsharedFlags Libs
		 Arch FinkBase ProcessDep
		 JAVAC JAVA JAR JAVAH JNI_PATH
		 JavaBuild PackageBase JavaSubpackages javaviewJars jrealityJars );
   @export=qw( PREFIX InstallTop InstallArch InstallBin DirMask ARCHFLAGS );
   $TOP=Cwd::getcwd;
}
use vars qw($Cflags_ $CXXflags_ $LDflags_ $Platform $BuildDir),
         map { "\$$_" } @noexport, @export;

#######################################################################################
#
#  The option parsing and architecture choice should be made before loading
#  the core polymake modules
#
sub read_vars($) {
   my ($file)=@_;
   open M, $file
   or die "$0: can't read configuration file $file: $!\n";
   while (<M>) {
      last if /end of configure/;
      if (/^(\w+)\s*=\s*(.*)$/ && exists $main::{$1}) {
	 $$1=$2;
      }
   }
   close M;
}

BEGIN {
   if ($ARGV[0] eq "-quick") {
      $quick=1;
      shift;
   }
   if ($ARGV[0] eq "-repeat") {
      $repeat=$silent=1;
      shift;
   }
   if ($ARGV[0] eq "-bootstrap") {
      $bootstrap=1;
      shift;
      $perlxlib=".bootstrap/perlx";
   }

   # Read the configuration known so far

   foreach (@ARGV) {
      if (/^(\w+)=(.*)$/ && exists $main::{$1}) {
	 $$1=$2;
      } else {
	 die "unrecognized argument: '$_'\n";
      }
   }
   unless (@ARGV) {
      do "$TOP/support/locate_build_dir";
      die $@ if $@;
   }

   if (defined $BuildDir) {
      read_vars("$BuildDir/conf.make");
      $perlxlib ||= "$BuildDir/perlx";

   } elsif (!-t STDIN) {
      $silent=$quick=1;
      read_vars("<&STDIN");
      $BuildDir="build.$Arch";

   } else {
      if (my @other=glob "build.*/conf.make") {
	 {
	    local ($Arch, $Platform, $FinkBase, $ARCHFLAGS);
	    read_vars($other[0]);
	 }
	 undef $Cflags; undef $CXXflags; undef $LDflags; undef $Libs;
	 undef $JAVA;  undef $JAVAC;  undef $JAVAH;  undef $JAR;  undef $JNI_PATH;
	 if ($InstallArch =~ m{^$InstallTop/[^/]+$}) {
	    $InstallArch="$InstallTop/$Arch";
	 }
      }
      $BuildDir="build.$Arch";
   }
}

#######################################################################################
#
# Load the necessary polymake modules
#

use lib "perllib", $perlxlib, defined($FinkBase) ? ("$FinkBase/lib/perl5") : ();

# replace the C++ interface and preference handler with stubs
$INC{"Polymake/Core/CPlusPlus.pm"}=".";
$INC{"Polymake/Core/Preference.pm"}=".";

package Polymake::Core::Preference;
sub new { bless [ ], shift }
sub DESTROY { }
sub AUTOLOAD { 0 }
sub custom { shift }
sub app_handler { shift }

package Polymake::Core::CPlusPlus;
sub app_handler { new Polymake::Core::Preference }

package Polymake;
use vars qw($InstallTop $Arch $InstallArch $DeveloperMode);

$InstallTop=$TOP;
$Arch=$main::Arch;
$DeveloperMode=-d ".svn" or $ENV{POLYMAKE_USER_DIR}="/tmp";
$Polymake::Core::Customize::reconfigure=1;

package main;
require Polymake;

my $common=new Polymake::Core::Application("common", "configure.rules");

#######################################################################################

sub compile {
   open C, ">polymake_configure.cc" or die "$0: can't create a temporary file: $!\n";
   print C @_;
   close C;
   my $errors=`$CXX $CXXflags -c polymake_configure.cc 2>&1`;
   if ($COMOversion) {
      1 while $errors =~ s/\A[^"].*\n//xm;
      $errors="" unless $errors =~ /\S/;
   }
   $errors;
}

sub build {
   open C, ">polymake_configure.cc" or die "$0: can't create a temporary file: $!\n";
   print C @_;
   close C;
   my $errors=`$CXX $CXXflags -o polymake_configure polymake_configure.cc 2>&1`;
   if ($COMOversion) {
      1 while $errors =~ s/\A[^"].*\n//xm;
      $errors="" unless $errors =~ /\S/;
   }
   $errors;
}

sub build_c {
   open C, ">polymake_configure.c" or die "$0: can't create a temporary file: $!\n";
   print C @_;
   close C;
   local $ENV{ARCHFLAGS}=$ARCHFLAGS if $ARCHFLAGS;
   my $errors=`$CC $Cflags $Cflags_ -o polymake_configure polymake_configure.c $LDflags $Libs 2>&1`;
   if ($COMOversion) {
      1 while $errors =~ s/\A[^"].*\n//xm;
      $errors="" unless $errors =~ /\S/;
   }
   $errors;
}

# path( default=$_ ), allow_readonly =>
sub make_dir(;$$) {
   my $dir=@_ ? $_[0] : $_;
   if (-d $dir) {
      -x _ && ($_[1] || -w _) or die "$0: directory $dir inaccessible\n";
   } else {
      mkdir $dir, 0755 or die "$0: can't create directory '$dir': $!\n";
   }
}

sub make_dir_path(@) {
   foreach (@_) {
      while (m{ (?<!^) / | $ }xg) {
	 make_dir substr($_, 0, $-[0]), $+[0]<length($_);
      }
   }
}

sub make_build_dir($@) {
   my $make_perl_dir=shift;
   foreach my $dir (@_) {
      make_dir "$BuildDir/$dir";
      open M, ">$BuildDir/$dir/Makefile";
      my $rel_dir=$dir;
      $rel_dir =~ s:[^/]+:..:g;
      print M <<".";
BuildDir := $rel_dir
ProjectTop := \${BuildDir}/..$ProjectTopLink
include \${BuildDir}/conf.make
include \${ProjectTop}/support/build.make
.
      close M;
      if ($make_perl_dir) {
	 make_dir "$BuildDir/$dir/perl";
      }
   }
}

sub make_java_dir {
   my $src="java/src/$PackageBase";
   foreach my $dir (@_) {
      -e "$src/$dir" or next;
      my $rel_dir="java_build/$PackageBase/$dir";
      make_dir($rel_dir);
      open M, ">$rel_dir/Makefile";
      $rel_dir =~ s:[^/]+:..:g;
      print M <<".";
ProjectTop := $rel_dir
include \${ProjectTop}/support/java.make
.
      close M;
   }
}

sub v_cmp($$) {
  eval "v$_[0] cmp v$_[1]";
}

my @apps=glob "apps/*";

STDERR->autoflush;

#######################################################################################
#
# Agree upon installation location
#
$InstallTop ||= "/usr/local/share/polymake";
if (!$silent) {
   Polymake::common::enter_filename($InstallTop,
      "\nWhere is the architecture-independent part of polymake to be installed?", "!d-");
}
{
   local $_=$InstallTop;
   $InstallArch ||= s:/local/share/:/local/lib/: ? $_ : "$InstallTop/$Arch";
   if (!$silent) {
      Polymake::common::enter_filename($InstallArch, "\nWhere are the components for this architecture to be installed?", "!d-");
   }
}

$InstallBin ||= do {
   if ($InstallArch =~ m{/lib(?:exec)?/polymake$} && -d "$`/bin" or
       $InstallArch =~ m{/polymake$} && -d "$`/bin") {
      "$`/bin"
   } else {
      "/usr/local/bin"
   }
};
if (!$silent) {
   Polymake::common::enter_filename($InstallBin, "\nWhere is the polymake script itself to be installed (should be on your PATH)?", "!d-");
}

#######################################################################################
#
# Agree upon compiler and options
#
sub check_cxx {
   my ($CXX, $silent)=@_;
   undef $GCCversion;  undef $ICCversion;  undef $COMOversion;  undef $CCache;
   if ($CXX =~ m{\bccache\s+[/\w+]}) {
      $CCache=1
   } else {
      my $absCXX= $CXX =~ m|^/| ? $CXX : Polymake::common::locate_in_path($CXX);
      while (-l $absCXX) {
	 $absCXX=readlink $absCXX;
      }
      $CCache= $absCXX =~ /\bccache$/;
   }

   if ($CXX =~ m/\b[ei]cp?c$/) {
      local $CXXflags="-V";
      $CXX =~ s/cc$/cpc/;
      $_=compile 'int main() { return 0; }';
      if (/Version\s+([\d.]+)/) {
	 $ICCversion=$1;
      }
   } elsif ($CXX =~ m/\bcomo$/) {
      local $CXXflags="--version";
      $_=compile 'int main() { return 0; }';
      if (/C\+\+\s+([\d.]+)/) {
	 $COMOversion=$1;
      }
   } elsif ($CXX =~ m/\b[gc]\+\+/) {
      $GCCversion=`$CXX --version`; chomp $GCCversion;
   }

   my $warn;
   if ($GCCversion) {
      if ($^O ne "darwin" && $GCCversion =~ /\((.*)\)$ | prerelease | snapshot /mx) {
	 $warn=<<".";
*************************************************************
*********               WARNING:                    *********

Your C++ compiler $CXX is apparently NOT an official release
of the GCC project team.
You may try to compile polymake with it, but if this fails,
please resort to one of versions recommended in the
Installation Guide before sending us a bug report.

*************************************************************
*************************************************************
.
      }
      ($GCCversion)= `$CXX -dumpspecs` =~ /\*version:\n([\d.]+)/s
      or return "Can't determine the version of $CXX";
      if (v_cmp($GCCversion,"3.3")<0) {
	 return "$CXX says its version is $GCCversion, which is rather out-of-date.\nPlease refer to the installation instructions";
      }
      if ($^O ne "darwin" && v_cmp($GCCversion,"4.0")>=0 && v_cmp($GCCversion,"4.2.1")<=0) {
	 $warn=<<".";
************************************************
*********          WARNING:            *********

Optimization in gcc 4.0...4.2.1 is not reliable.
Use it at your own risk.
Please refer to the installation instructions
for our recomendations.

************************************************
************************************************
.
      }
      unless ($CC) {
	 ($CC=$CXX) =~ s/\+\+/cc/;
      }
      $CsharedFlags="-fPIC";

   } elsif ($ICCversion) {
      if (v_cmp($ICCversion,"10.0")<0) {
	 return "$CXX says its version is $ICCversion, while the minimal required version is 10.0";
      }
      unless ($CC) {
	 ($CC=$CXX) =~ s/cpc$/cc/;
      };
      $CsharedFlags="-fPIC";

   } elsif ($COMOversion) {
      if (v_cmp($COMOversion,"4.3.3")<0) {
	 return "$CXX says its version is $COMOversion, while the minimal required version is 4.3.3";
      }
      $CC ||= $CXX;
      $CsharedFlags="-KPIC";

   } else {
      return "$CXX seems not to be any of the supported C++ compilers (GNU, Intel, Comeau)";
   }

   $LDsharedFlags=$Config{lddlflags};

   if (!$silent && defined($warn)) {
      warn $warn;
   }
   undef;
}

sub compiler_choice {
   my $newCXX=$CXX;
   Polymake::common::enter_executable_path($newCXX, "\nWhich C++ compiler to use?", sub {
					      undef $CC if $_[0] ne $CXX;
					      my $error=check_cxx($_[0]);
					      !defined($error) or do {
						 print "\n$error.\nPlease specify a path to a supported C++ compiler.\n"; 0
					      }
					   }, "!");
   if ($CXX ne $newCXX) {
      undef $CXXflags; undef $Cflags;
      $CXX=$newCXX;
   }

   Polymake::common::enter_executable_path($CC, "\nWhich C compiler to use?", "!");
   Polymake::common::enter_string($CXXOPT, "\nWhich compiler optimization level do you trust?", " ");
}

sub compiler_options {
   if ($^O eq "darwin" && !defined($CXXflags) && !defined($LDflags)) {
      $CXXflags=$Cflags="-I$FinkBase/include";
      $LDflags="-L$FinkBase/lib";
   }
   my $newCXXflags=$CXXflags;
   Polymake::common::enter_string($newCXXflags,
      "\nWhich C++ compiler flags to add (e.g. choosing specific CPU model)?", " ");
   if ($CXXflags ne $newCXXflags) {
      undef $Cflags;
      $CXXflags=$newCXXflags;
   }

   $Cflags ||= $CXXflags;
   Polymake::common::enter_string($Cflags, "\nWhich C compiler flags to add?", " ");
   Polymake::common::enter_string($LDflags, "\nWhich linker flags to add?", " ");
}

sub java_options {
   unless (defined($JAVAC)) {
      ($JAVAC=$JAVA)=~s/java$/javac/ or undef $JAVAC;
   }
   if ($Polymake::DeveloperMode || $JavaBuild || !$silent &&
       Polymake::common::enter_yes_no(0,
	 "\nAre you going to develop new Java modules for interactive visualization,\n".
	 "or do you need to recompile the shipped jar archives on this platform\n")) {

      -x $JAVAC or Polymake::common::enter_executable_path($JAVAC, "\nWhich Java compiler to use?", "!A");

      unless (defined($JAR)) {
	 ($JAR=$JAVA)=~s/java$/jar/ or undef $JAR;
      }
      -x $JAR or Polymake::common::enter_executable_path($JAR, "\nWhich Java archiver to use?", "!A");
      $JavaBuild="y";
   }

   unless (defined($JAVAH)) {
      ($JAVAH=$JAVAC)=~s/javac/javah/ or undef $JAVAH;
   }
   -x $JAVAH or Polymake::common::enter_executable_path($JAVAH, "\nWhich Java header generator to use?", "!A");

   my $javac=$JAVAC;
   while (1) {
      if ($javac =~ m|/bin/javac$|) {
	 if (-f "$`/include/jni.h") {
	    $JNI_PATH="$`/include";
	    last;
	 }
      } elsif ($javac =~ m|/Commands/javac$|) {
         # MAcOS X naming convention
         if (-f "$`/Headers/jni.h") {
	    $JNI_PATH="$`/Headers";
	    last;
	 }
      }
      if (-l $javac) {
	 $javac=readlink($javac);
      } else {
	 Polymake::common::enter_filename($JNI_PATH, "\nWhere the Java Native Interface headers (e.g. jni.h) are installed?",
	    sub { -f "$_[0]/jni.h" or do { print "Invalid path: $_[0]/jni.h does not exist\n"; 0 } }, "!")
	 and last;
      }
   }
}

$CXX ||= "g++";
$CXXOPT ||= "-O3";

my $cxx_error=check_cxx($CXX,1);

if (!($quick || $repeat) || defined($cxx_error)) {
   undef $CXX if $cxx_error;
   compiler_choice;
   if (!defined($CXXflags) && $^O ne "darwin") {
      if (v_cmp($GCCversion, "4.2")>=0) {
         $CXXflags="-march=native";
      } elsif ($Platform eq "sun4u") {
	 $CXXflags="-mcpu=ultrasparc";
      } elsif ($Platform =~ "i\d86") {
	 $_=`cat /proc/cpuinfo`;
	 if (/Athlon/s) {
	    if ($ICCversion) {
	       $CXXflags="";
	    } else {
	       $CXXflags="-march=athlon";
	    }
	 } elsif (/Pentium III/s) {
	    if ($ICCversion) {
	       $CXXflags="-march=pentiumiii -mcpu=pentiumpro";
	    } else {
	       $CXXflags="-march=pentium3";
	    }
	 } elsif (/(?:Pentium IV|Pentium\(R\) 4)/s) {
	    if ($ICCversion) {
	       $CXXflags="-march=pentium4 -mcpu=pentium4";
	    } else {
	       $CXXflags="-march=pentium4";
	    }
	 } elsif (/Pentium/) {
	    if ($ICCversion) {
	       $CXXflags="-mcpu=pentium";
	    } else {
	       $CXXflags="-march=pentium";
	    }
	 }
      } elsif ($Platform eq "x86_64") {
	 $_=`cat /proc/cpuinfo`;
	 if (/Athlon/s) {
	    if ($ICCversion) {
	       $CXXflags="";
	    } else {
	       $CXXflags="-march=athlon64";
	    }
	 } elsif (/Opteron/s) {
	    if ($ICCversion) {
	       $CXXflags="";
	    } else {
	       $CXXflags="-march=opteron";
	    }
	 }
      } elsif ($Platform eq "i86pc") {
	 $CXXflags="-march=pentiumpro";
      }
   }

   compiler_options if !$quick;
   if ($COMOversion) {
      foreach ($Cflags, $CXXflags) { ~s/(\S+)/-copt $1/g }
   }
}

$CXXDEBUG ||= $ICCversion ? "-g -Ob0" : "-g";

if (defined $CXX) {
   if ($GCCversion) {
      $CXXflags_ .= " -Wall -ftemplate-depth-200";
      $Cflags_ .= " -Wall";
      if ($ARCHFLAGS) {
	 my $allarch=qr/ -arch \s+ \S+ (?: \s+ -arch \s+ \S+)* /x;
	 $Cflags =~ s/$allarch//o;
         $Cflags_ .= ' ${ARCHFLAGS}';
	 $CXXflags =~ s/$allarch//o;
         $CXXflags_ .= ' ${ARCHFLAGS}';
         $LDsharedFlags =~ s/$allarch/\${ARCHFLAGS}/o;
      }
      $Libs = "";
      if (v_cmp($GCCversion, "4.0")>=0) {
	 $Cflags_ .= " -Wno-uninitialized -Wno-unused";
	 if (v_cmp($GCCversion, "4.2")>=0) {
	    $Cflags_ .= " -Wno-parentheses";
	    $CXXflags_ .= " -Wno-strict-aliasing -Wno-parentheses -fno-inline-functions-called-once";
	 } else {
	    $CXXflags_ .= " -Wno-strict-aliasing -Wno-uninitialized";
	 }
      }
      if ($^O eq "solaris") {
	 $CXXflags_ .= " -D__C99FEATURES__ -D_GLIBCXX_USE_C99_MATH";
      }
   } elsif ($ICCversion) {
      $CXXflags_ .= " -Wall -wd193,383,304,981,1419,279,810,171,1418,488,1572,561";
      $Cflags_ .= " -Wall -wd193,1572,561";
      $Libs = "";
   } elsif ($COMOversion) {
      $CXXflags_ .= " --diag_suppress 815 -no_A --no_display_mode -D_GNU_SOURCE -D_STL_NO_CONCEPT_CHECKS";
      $Cflags_ .= " -no_A --c --no_display_mode -D_GNU_SOURCE";
      $Libs = "-lm";
   }

   #######################################################################################
   #
   # Detect whether socket functions reside in a separate library
   #
   if ($Config{libs} =~ "-lsocket") {
      $Libs= $Libs ? "-lsocket $Libs" : "-lsocket";
   }
}

#######################################################################################
#
# Check the GMP
#
for (;;) {
   local $Libs="-lgmp $Libs";
   my $build_error=build_c <<".";
#include <gmp.h>
#include <stdio.h>
int main() {
   mpz_t a;
   mpz_init(a);
   printf("%d.%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL);
   return 0;
}
.
   if (!length($build_error) and $?==0) {
      my $is_version=`./polymake_configure`;
      if ($?==0) {
	 last if v_cmp($is_version,"3.1.1")>=0;

	 print "\nThe GNU Multiprecision Library (GMP) installed at your site is of version $is_version\n",
	       "while 3.1.1 is the minimal required version.\n";
      } else {
	 print "\nA program checking for the GNU Multiprecision Library (GMP) failed.\n",
	       "Probably it couldn't find the shared library.\n";
      }
   } else {
      print "\nA program checking for the GNU Multiprecision Library (GMP) couldn't be compiled:\n",
            $build_error;
   }
   exit 1 if $silent;

   if (length($build_error) && (length($Cflags) || length($LDflags))) {
      Polymake::common::enter_yes_no(1, "Do you want to change the compiler and/or linker flags\n")
	 or exit 1;
   } elsif (!Polymake::common::enter_yes_no(0, <<'.')) {

If you know that the required version of GMP is installed somewhere at your site
would you like to adjust compiler and/or linker options,
so that it can be found by the next try?
.
      print <<'.';
Please ask your system administrator to install the GMP library.
It is available as a ready-to-use package for the most Linux distributions
as well as for MacOS X (via Fink), FreeBSD, Solaris (e.g. via www.blastwave.org).
Note that the package is sometimes called libgmp.
In some distributions it is split into basic and developer's subpackages -
be sure to install both.

Alternatively you might decide to visit the GMP site http://www.swox.com/gmp/
and download the newest release.  Compiling GMP yourself, you can be sure
to gain the most efficient code optimized to your particular CPU model.
Please remember to run the tests included in the distribution before the
final installation.

After successful installation of GMP, run `make configure' for polymake again.
.
      exit 1;
   } else {
      print <<'.';

Please supply the path to gmp.h in the -I compiler option
and the path to libgmp.so (MacOS: libgmp.dylib) in the -L linker option
.
   }
   compiler_options;
}

######################################################################################
#
# Create the build directories and write the Makefile's
#
my $relBuildDir=$BuildDir;
if (!-e $BuildDir
    and $ENV{BuildTop} ||
        do {
	   foreach my $build (glob "build.*") {
	      if (-l $build) {
		 $build=readlink $build;
		 if (-d $build && $build =~ s|/build(?:\.[^/]+)?$||) {
		    $ENV{BuildTop}=$build;
		    last;
		 }
	      }
	   }
	   $ENV{BuildTop}
	}
    and $ENV{BuildTop} ne '.') {
   $BuildDir="$ENV{BuildTop}/$relBuildDir";
   symlink $BuildDir, $relBuildDir;
   symlink $TOP, "$ENV{BuildTop}/ProjectTop";
   $ProjectTopLink="/ProjectTop";
}
make_dir for $BuildDir, "$BuildDir/apps";
if ($repeat && -d "$BuildDir/modules") {
   system("rm -rf $BuildDir/modules");
}
$ProjectTopLink ||= -l $BuildDir && "/ProjectTop";

if ($bootstrap) {
   my @move=("perlx-$Config{version}-$Config{archname}", "perlx");
   if (-l $relBuildDir) {
      my $copy_cmd="cp -pR ".join(" ", map { ".bootstrap/$_" } @move)." $BuildDir/";
      system($copy_cmd) and die "$copy_cmd failed\n";
   } else {
      foreach (@move) {
         -e "$BuildDir/$_" && system("rm -rf $BuildDir/$_");
	 rename ".bootstrap/$_", "$BuildDir/$_" or die "mv .bootstrap/$_ $BuildDir/$_ failed: $!\n";
      }
   }
}

$DirMask="755";

### configure the java components

$common->include_rules("java_configure");
$JAVA=$Polymake::common::java;

if (defined $JAVA) {
   java_options;
   if ($JavaBuild) {
      eval { $common->include_rules(grep { -f "apps/common/rules/$_" } qw( javaview_configure jreality_configure )) };
      if ($@) {
	 print "WARNING: error loading rule files: $@\n",
               "auto-configuration of Java based components will be skipped\n";
      } else {
	 my @java_subpkg;

	 if (defined $JavaView::Viewer::install_top) {
	    push @java_subpkg, "javaview";
	    $javaviewJars="$JavaView::Viewer::install_top/jars/javaview.jar";
	 }
	 if (defined $JReality::Viewer::jar_path) {
	    push @java_subpkg, "jreality";
	    $jrealityJars="\$(addprefix $JReality::Viewer::jar_path/, @JReality::Viewer::jars)";
	 }

         $JavaSubpackages="@java_subpkg";
      }
   }

   $PackageBase="de/tuberlin/polymake";

   make_dir_path("java_build/$PackageBase");
   if ($JavaBuild) {
      open M, ">java_build/Makefile";
      print M <<'.';
# make all jar archives
.PHONY: all clean install
all clean install: ; @$(MAKE) -C .. --no-print-directory $@-java
.
      close M;
   }
   make_java_dir "console";
   my $last_limb=qr{^[^/]+/(.*)};
   my @java_src_dirs=grep { -d "$_/java" } @apps;
   make_java_dir map { /$last_limb/o } @java_src_dirs;
}

### write the conf file
open CONF, ">$BuildDir/conf.make";

foreach my $var (@noexport, @export) {
   print CONF "$var=$$var\n" if defined($$var);
   if (defined ${$var."_"}) {
      print CONF "$var+=".${$var."_"}."\n";
   }
}

print CONF "export ", join(" ", grep { defined($$_) } @export), "\n";

print CONF <<".";
# end of configure
RANLIB=$Config{ranlib}
SO=$Config{dlext}
export INSTALL_PL=$TOP/support/install.pl
DESTDIR=
export FinalInstallTop := \${InstallTop}
export FinalInstallArch := \${InstallArch}
InstallTop := \${DESTDIR}\${InstallTop}
InstallArch := \${DESTDIR}\${InstallArch}
InstallBin := \${DESTDIR}\${InstallBin}
.
close CONF;

if (@apps) {
   ### external libs build directories
   make_dir "$BuildDir/external";
   make_build_dir 0, glob "external/*";
}

### core library and applications build directories
make_build_dir 1, "lib", @apps;

END {
   unlink glob "polymake_configure*";
   unlink glob "core*";
}
