#!/usr/bin/perl -w
#
# Starts a java runtime, ensuring that the debian JNI module directory is on
# the JNI search path.
#
# Variable $javaRuntime should be modified to suit the particular java
# runtime being used.
#
# Usage: As for the particular java runtime being used.
#
# Copyright (C) 2002 by Ben Burton <benb@acm.org>

use strict;

# The real java runtime:
my $javaRuntime = '/usr/bin/myjavaruntime';

# The debian JNI module directory:
my $debianJNIDir = '/usr/lib/jni';

# The command-line options to pass to the real java runtime:
my @commandLine;

# The full JNI search path to use:
my $JNIPath = '';

# Build the command-line from the arguments given.
foreach my $arg (@ARGV) {
  if ($arg =~ /^-Djava.library.path=(.+)$/) {
    # A component of the JNI search path has been given.
    if ($JNIPath) {
      $JNIPath = $JNIPath . ':' . $1;
    } else {
      $JNIPath = $1;
    }
  } else {
    # Some other argument has been given.
    push @commandLine, $arg;
  }
}

# Add the debian JNI module directory to the JNI search path if it's not
# already there.
if ($JNIPath !~ /(^|:)$debianJNIDir($|:)/) {
  if ($JNIPath) {
    $JNIPath = $JNIPath . ':' . $debianJNIDir;
  } else {
    $JNIPath = $debianJNIDir;
  }
}

# Call the real Java runtime.
my @fullCommandLine = ( $javaRuntime, "-Djava.library.path=$JNIPath" );
push @fullCommandLine, @commandLine;
exec @fullCommandLine or exit(1);

