#  Copyright (c) 1997-2004
#  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_javaview.pl,v 1.2 2004/11/25 13:11:31 gawrilow Exp $

use strict 'vars', 'subs';

package Configure::Java;

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

sub query_javaview {
   my ($mod_common)=@_;
   while (print("Where JavaView is installed? ") &&
	  main::answer($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 {
   my $mod_common=eval { new Poly::Module("common", "javaview_configure") };
   do {
      return 0 if $@;
      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($mod_common));
}

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

   $JavaViewTop=$JavaView::Viewer::install_top;

   $JAVA ||= $JavaView::Viewer::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);

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

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

#######################################################################################
#
# prepare for the pre-configuration the java-dependent rules
#
use lib "perl", "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;

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

find_javaview && java_options;

if ($MakeJava) {
   $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
all clean: ; @$(MAKE) -C .. --no-print-directory $@-java
.
   close M;

   make_java_dir map { m'^[^/]+/(.*)' } grep { -d "$_/java" } @main::apps, @main::modules;

   foreach my $var (@noexport) {
      print main::CONF "$var=$$var\n" if defined($$var);
   }
}

