#  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: javaview_configure,v 1.7 2005/02/02 17:53:33 gawrilow Exp $

INCLUDE
   utilities

package JavaView::Viewer;

# installation directory
custom $install_top;

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

# java interpreter suitable for javaview
custom $java;

# javaview version
custom $version;

use Errno;

CONFIGURE {
   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, $java, 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:
