#  Copyright (c) 1997-2006
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.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_java.pl,v 1.5 2006/02/27 00:42:39 gawrilow Exp $

use strict 'vars', 'subs';

package Configure::Java;

use vars qw( @noexport @unconfigured @jars );
BEGIN {
   @noexport=qw( PackageBase JAVAC JAVA JAR JavaBuildDir AutoconfFile );
}
use vars map { "\$$_" } @noexport;

my @rules=grep { -f "modules/common/rules/$_" } qw( utilities javaview_configure jreality_configure );
my $mod_common;

sub query_javaview {
   while (print("Where JavaView is installed? ") &&
	  main::answer_path($JavaView::Viewer::install_top) &&
	  defined($JavaView::Viewer::install_top)) {

      # for those who don't read the prompt carefully;
      $JavaView::Viewer::install_top =~ s{/bin(?:/javaview)?$}{};
      $JavaView::Viewer::install_top =~ s{/$}{};

      if (-d $JavaView::Viewer::install_top  and
	  -e "$JavaView::Viewer::install_top/_jarVersion.txt") {

	 # remove 'config' flag
	 $mod_common->custom->set('$JavaView::Viewer::install_top');
	 $mod_common->repeat_configure('javaview_configure')
	    and return 1;

	 print <<'.';
The configuration script has failed.
If this was caused by the wrong installation path, please specify a correct one.
Otherwise, type "none" to skip the JavaView configuration.
.
      } else {
	 print "\"$JavaView::Viewer::install_top\" does not look like JavaView installation directory\n";
      }
   }
}

sub find_javaview {
   do {
      if (!$mod_common->rulefiles->{javaview_configure}) {
	 print <<'.';

JavaView, the preferred visualization tool, could not be found along your PATH.

If it is installed somewhere else on your site, please give the top directory
at the next prompt.

If not, but you are going to download it from www.javaview.de right now, rerun
this configuration after you have installed it.

If you are not going to use JavaView, type "none" at the next prompt.
Should you revise your decision later, remember to run
'polymake --reconfigure' after JavaView installation.

.
      } elsif (eval("v$JavaView::Viewer::version") ge v3.0.3) {
	 return 1;
      } else {
	 print <<"."

JavaView installed under $JavaView::Viewer::install_top is of an old version $JavaView::Viewer::version,
while the interactive visualization requires at least the version 3.03.

If a new version is installed somewhere else on your site, please give the top directory
at the next prompt.

If not, but you are going to download it from www.javaview.de right now, rerun
this configuration after you have installed it.

If the static visualization is good enough for you, type "none" at the next prompt.
Should you revise your decision later, remember to run
'polymake --reconfigure' after JavaView upgrade.

.
      }
   }
   while (query_javaview);
}

sub find_jreality {
   if ($mod_common->rulefiles->{jreality_configure}) {
      return 1;
   }
   # until JReality becomes publicly available
   if ($main::developer) {
      FIND:
      {
	 print <<'.';

JReality components could not be found.
Please specify the directory where the JAR files reside,
.
	 print "or \"none\" if JReality is not installed: ";
	 if (main::answer_path($JReality::Viewer::jar_path) &&
	     defined($JReality::Viewer::jar_path)) {

	    if (-d $JReality::Viewer::jar_path) {

	       # remove 'config' flag
	       $mod_common->custom->set('$JReality::Viewer::jar_path');
	       $mod_common->repeat_configure('jreality_configure')
	          and return 1;

	       print <<".";
The configuration script has failed again: $@.
.
	    } else {
	       print "\"$JReality::Viewer::jar_path\" is not a directory\n";
	    }
	    redo FIND;
	 }
      }
   }
   0;
}

sub java_options {
   if (!$main::developer) {
      print "\nAre you going to develop new Java modules for interactive visualization,\n".
	    "or do you need to recompile the shipped jar archives",
	    ($main::multi && " on this platform"), "? ";
      return 0 unless main::yes_not(0);
   }

   $JAVA ||= $JavaView::Viewer::java || Modules::common::find_via_path("java");
   print "\nWhich Java interpreter should be used? ";
   main::answer($JAVA);

   $JAVAC or ($JAVAC=$JAVA)=~s/java$/javac/;
   $JAR or ($JAR=$JAVA)=~s/java$/jar/;
   print "\nWhich Java compiler should be used? ";
   main::answer($JAVAC);
   print "\nWhich Java archiver should be used? ";
   main::answer($JAR);

   defined($JAVAC) && defined($JAVA) && defined($JAR);
}

sub make_java_dir {
   my $src="java/src/$PackageBase";
   main::make_dir_path $src;
   (my $back=$src) =~ s|[^/]+|..|g;
   
   foreach my $dir (@_) {
      my $rel_dir="$JavaBuildDir/$PackageBase/$dir";
      main::make_dir $rel_dir;
      if (-d "apps/$dir/java") {#FIXME correct paths
	symlink "$back/apps/$dir/java", "$src/$dir";
      } else {
	symlink "$back/modules/$dir/java",  "$src/$dir";
      }
      open M, ">$rel_dir/Makefile";
      $rel_dir =~ s:[^/]+:..:g;
      print M <<".";
ProjectTop := $rel_dir
include \$(ProjectTop)/support/java.make
.
      close M;
   }
}

#######################################################################################
#
# prepare for the pre-configuration the java-dependent rules
#
use lib "perl", "$main::BuildDir/perlx";
use Poly::Module;
BEGIN {
   $Switches::reconfigure=1;
   $AutoconfFile="configured.pl";
}
package Poly::Module;
use Poly::Customize $Configure::Java::AutoconfFile;
@lookup=(".");
$reconfigure_hint="";

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

package Configure::Java;

$mod_common=eval { new Poly::Module("common", @rules) };
if ($@) {
   print "WARNING: error loading module common: $@",
         "autoconfiguration of java components will be skipped\n";
   return;
}

my $javaview=find_javaview;
my $jreality=find_jreality;

if ($javaview) {
   push @jars, "$JavaView::Viewer::install_top/jars/javaview.jar";
} else {
   push @unconfigured, "if_JavaView";
}
if ($jreality) {
   push @jars, map { "$JReality::Viewer::jar_path/$_" } @JReality::Viewer::jars;
} else {
   push @unconfigured, "if_JReality";
}

if (($javaview || $jreality) && java_options) {
   $JavaBuildDir="java_build";
   $PackageBase="de/tuberlin/polymake";

   main::make_dir_path "$JavaBuildDir/$PackageBase";
   open M, ">$JavaBuildDir/Makefile";
   print M <<'.';
# make all jar archives
.PHONY: all clean install
all clean install: ; @$(MAKE) -C .. --no-print-directory $@-java
.
   close M;

   my $last_limb=qr{^[^/]+/(.*)};
   my @java_src_dirs=grep { -d "$_/java" } @main::apps, @main::modules;
   make_java_dir map { /$last_limb/ } @java_src_dirs;
   
   if ($javaview) {
      my @javaview_src_dirs = grep { -d "$_/java/javaview" } @java_src_dirs;
      make_java_dir map { /$last_limb/; "$1/javaview" } @javaview_src_dirs;
   }
   if ($jreality) {
      my @jreality_src_dirs = grep { -d "$_/java/jreality" } @java_src_dirs;
      make_java_dir map { /$last_limb/; "$1/jreality" } @jreality_src_dirs;
   }

   foreach my $var (@noexport) {
      print main::CONF "$var=$$var\n" if defined($$var);
   }
   print main::CONF <<".";
JavaUnconfigured := @unconfigured
AddJars := @jars
.
}

