#  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: javaview_configure 7540 2006-12-21 21:04:18Z gawrilow $

package JavaView::Viewer;

# installation directory
custom $install_top;

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

# javaview version
custom $version;

use Errno;

CONFIGURE {
   return 0 unless defined($java);

   local @ENV{qw( SHELLOPTS DISPLAY )}=qw( xtrace );
   open my $jv, $install_top ? "$install_top/bin/javaview 2>&1 |" : "javaview 2>&1 |"
      or die $!{ENOENT} ? "'javaview' start script not found\n" : "error starting javaview: $!\n";
   local $/;
   local $_=<$jv>;
   close $jv;

   ($classpath, my $top)=m{ ^\+\s+ CLASSPATH=(.*) (?s: .*?) ^\+\s+ exec \s+ \S+ (?<= [\s/]java) .*? CodeBase=(\S+) }xm
      or die <<'.';
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 fill in the configuration variables in ~/polymake/customize.pl manually;
don't forget to enable the javaview.rules in each module and application.
.

   $install_top = do {
      while ($top =~ s{/(?!\.\.)[^/]+/\.\.(?=/|$)}{}g) { }
      $top;
   };
   while ($classpath =~ s{/(?!\.\.)[^/]+/\.\.(?=/|:|$)}{}g) { }

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

   # return OK
   1;
}


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