#!/usr/bin/perl -w
#
# $Id: runjava,v 1.3 2001/08/13 23:30:52 mdb Exp $
#
# A script for invoking java. The project root is inferred to be one
# directory above the directory in which this script lives. Based on the
# root, all .jar files in the lib subdirectory are added to the classpath.
# Additionally all shared libraries in lib/$arch, where $arch is generated
# via uname and looks something like i686-Linux, are added to the
# LD_LIBRARY_PATH environment variable. Classes are assumed to live in
# $classdir as defined below.

use strict;
use Getopt::Std;

# this is where our classfiles live (relative to the project root)
my $classdir = "dist/classes";

# parse the arguments
my %opts;
getopts('p:v', \%opts);
my $pid_file = $opts{"p"};
my $verbose = $opts{"v"};

# make sure they specified a classfile
my $usage = "Usage: $0 [-p pid_file] [-v] [-- args to pass to java] class\n";
die $usage unless (@ARGV);

# get the server root by popping the /bin off of our directory
my $location;
chomp($location = `dirname $0`);
my @parts = split(/\//, $location);
pop(@parts);
my $root = join("/", @parts);

# figure out where our JVM lives
my $jhome = $ENV{"JAVA_HOME"};

# make sure JAVA_HOME is set
if (!defined $jhome) {
    warn "$0: Error: No JAVA_HOME specified!\n";
    warn "\n";
    warn "You must set your JAVA_HOME environment variable to the\n";
    warn "the absolute path of your JDK installation. For example:\n";
    warn "\n";
    warn " % JAVA_HOME=/usr/local/jdk1.2\n";
    warn " % export JAVA_HOME\n";
    die "\n";
}

my $java = "$jhome/bin/java";
my $jlib = "$jhome/lib/classes.zip";

# use rt.jar if necessary
if (! -f $jlib) {
    $jlib = "$jhome/lib/rt.jar";
}
if (! -f $jlib) {
    $jlib = "$jhome/jre/lib/rt.jar";
}

# make sure we can run the jvm
if (! -x $java) {
    die "$0: Can't find a java interpreter in '$jhome'.\n";
}

# determine our machine architecture
my $ostype = `uname -s`;
my $machtype = `uname -m`;
chomp($ostype);
chomp($machtype);
my $arch = "$machtype-$ostype";

# add our native libraries to the runtime library path
my $libs = "$root/lib/$arch";
my $libpath = $ENV{"LD_LIBRARY_PATH"};

if (defined $libpath) {
    $ENV{"LD_LIBRARY_PATH"} = "$libs:$libpath";
} else {
    $ENV{"LD_LIBRARY_PATH"} = $libs;
}

# put everything in our class path
my $classpath = "$jlib:$root/$classdir";

# if there's an environment variable set, we'll need to include that too
my $ecpath = $ENV{"CLASSPATH"};
$classpath = "$ecpath:$classpath" if (defined $ecpath);

# any zip or jar files in our lib/ directory get added to the class path
if (opendir(DIR, "$root/lib")) {
    my $lib;
    foreach $lib (grep { /.(zip|jar)/ && -f "$root/lib/$_" } readdir(DIR)) {
	$classpath .= ":$root/lib/$lib";
    }
    closedir DIR;
}

# log the pid file if requested to do so
print `echo $$ > $pid_file` if (defined $pid_file);

my $cmd = "$java -mx256M -classpath $classpath " . join(" ", @ARGV);
print "$cmd\n" if ($verbose);
exec($cmd);
