#  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.6 2004/10/26 20:36:17 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;


CONFIGURE {
   # first locate the javaview script
   my $script_path= $install_top
      ? "$install_top/bin/javaview"
      : (find_via_path("javaview")
         or die "'javaview' start script not found\n");

   open my $jv, $script_path
   or die "could not read the start script $script_path: $!\n";
   local $/;
   local $_=<$jv>;
   close $jv;

   # now try to extract useful variables from the script
   $install_top ||= /^JAVAVIEW_HOME=(\S+)/m  &&  $1 ne "`dirname" ? $1 : ($script_path =~ m'^(.*)/bin/javaview')[0];

   if (/^(\#?)JAVA_HOME=(\S+)/m  and  !$1) {
      # take this only if uncommented by the user in the first lines of the script
      $java="$2/java";
   } else {
      $java="java";
   }

   my ($jars)= /^JAVAVIEW_JARS=(\S+)/m;
   $jars =~ s/\$JAVAVIEW_HOME/$install_top/g;
   ($classpath)= /^\s*CLASSPATH=(\S+)/m;
   die "could not extract the CLASSPATH information from the script $script_path\n"
      unless defined $classpath;
   $classpath =~ s/\$JAVAVIEW_HOME/$install_top/g;
   $classpath =~ s/\$JAVAVIEW_JARS/$jars/g;

   # check the version
   open $jv, "$install_top/_jarVersion.txt"
   or die "could not check the version in the file $install_top/_jarVersion.txt: $!";
   $version= do { local $/; <$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:
