#  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: javaview_configure 9298 2009-09-02 23:10:56Z gawrilow $

INCLUDE
   java_configure

CONFIGURE : java_configure

package JavaView::Viewer;

# installation directory
custom $install_top;

# CLASSPATH listing all jar files belonging to javaview
custom $classpath;

CONFIGURE {
   my $script;
   undef $classpath;

   unless (defined($install_top) && -x ($script="$install_top/bin/javaview")) {
      find_via_path($script,"javaview") or return;
   }
   for (;; undef $script) {
      unless (defined($script)) {
	 enter_executable_path($script, "Please enter a full path to the `javaview' script", "A") or return;
      }
      local @ENV{qw( SHELLOPTS DISPLAY )}=qw( xtrace );
      my $jv;
      unless (open $jv, "$script 2>&1 |") {
	 print "error starting javaview: $!\n";
	 next;
      }
      local $/;
      local $_=<$jv>;
      close $jv;

      if (($classpath, $install_top)=m{ ^\+\s+ CLASSPATH=(.*) (?s: .*?) ^\+\s+ exec \s+ \S+ (?<= [\s/]java) .*? CodeBase=(\S+) }xm) {
	 while ($install_top =~ s{/(?!\.\.)[^/]+/\.\.(?=/|$)}{}g) { }
	 while ($classpath =~ s{/(?!\.\.)[^/]+/\.\.(?=/|:|$)}{}g) { }
      } else {
	 print <<".";
Your JavaView installation seems to be customized to a high extent.
The auto-configuration routine was not able to parse the JavaView start script.
Please study the script $script and type in the following paths manually:

.
	 ($install_top=$script)=~s|/bin/javaview||;
	 enter_filename($install_top, "Please enter the JavaView top installation directory",
			sub { -f "$_[0]/_jarVersion.txt" or do { print <<"."; 0 } }) or return;
$_[0] does not look like JavaView top directory: _jarVersion.txt missing
.
	 $classpath=join(":", $install_top, map { "$install_top/jars/$_" } qw(javaview.jar jvx.jar vgpapp.jar));
	 enter_string($classpath, "Please enter the CLASSPATH setting", "!");
      }

      # check the version
      unless (open $jv, "$install_top/_jarVersion.txt") {
	 print "could not check the version in the file $install_top/_jarVersion.txt: $!\n";
	 next;
      }
      my $version=<$jv>;
      close $jv;
      $version=~/VERSION\s*=\s*[\"\']?([\d.]+)/ and $version=$1;
      while ($version =~ s/(?<!\d)0(?=\d)/0./g) { }

      return 1 if eval("v$version") ge v3.95;
      print <<'.';
polymake requires JavaView v3.95 or newer;
Please download the current version from www.javaview.de
and proceed with the auto-configuration.
.
   }
}


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