#!/usr/bin/perl
#
# Kapellmeister
#
# A wrapper for the Kapellmeister Conductor management client.
#
#	The PIRL_JAVA_HOME enviroment variable, if it is set, will be
#	prepended to the java runtime classpath. The value of PIRL_JAVA_HOME
#	may be a directory pathname where the PIRL subdirectory and all its
#	class files is located; or it may be the pathname to a jar file -
#	e.g. PIRL.jar - containing the class files for the PIRL Java
#	Packages.
#
#	N.B.: If the PIRL_JAVA_HOME enviroment variable is not set but the
#	/opt/java/PIRL pathname exists, then /opt/java will be used for the
#	value of PIRL_JAVA_HOME.
#
#	If the CLASSPATH environment variable is set its value is appended to
#	the java runtime classpath.
#
#	The location of the following third party packages are expected to be
#	in the java runtime classpath:
#
#	JCM - http://math.hws.edu/javamath
#
#	The pathname to the Java Components for Mathematics jar file or the
#	root directory where its class files are located. This package is a
#	required dependency.
#	Default: $PIRL_JAVA_HOME/jcm/jcm_data.jar
#
#	SwingX - http://swinglabs.org/
#
#	The pathname to the SwingLabs Swing Component Extensions jar file or
#	the root directory where its class files are located. This package is
#	a required dependency.
#	Default: $PIRL_JAVA_HOME/SwingX/swingx.jar
#
#	Note: On Apple OS X (Darwin) systems the Java Virtual Machine will
#	automatically include jar files in the ~/Library/Java/Extensions and
#	/Library/Java/Extensions directories, if they exist, in the java
#	runtime classpath.
#
#	Use the KAPELLMEISTER_MEMORY environment variable to control the
#	amount of memory allocated to the java virutal machine. When working
#	with a large number of Stage_Managers and Conductors sufficient
#	memory must be allocated beyond the usual JVM size. The default, if
#	the KAPELLMEISTER_MEMORY environment variable is not used, is 256m.
#
#	CVS ID: Kapellmeister,v 2.2 2011/10/27 22:54:19 castalia Exp

$KAPELLMEISTER_MEMORY = "256m"
	unless $KAPELLMEISTER_MEMORY = $ENV{"KAPELLMEISTER_MEMORY"};

$CLASSPATH		= $ENV{"CLASSPATH"};

$PIRL_JAVA_HOME	= $ENV{"PIRL_JAVA_HOME"};
$PIRL_JAVA_HOME = "/opt/java"
	if (! $PIRL_JAVA_HOME &&
		-e "/opt/java/PIRL");
Add_to_CLASSPATH ($PIRL_JAVA_HOME);

$JCM = "$PIRL_JAVA_HOME/jcm/jcm_data.jar"
	unless $JCM = $ENV{"JCM"};
Add_to_CLASSPATH ($JCM);

$SwingX = "$PIRL_JAVA_HOME/SwingX/swingx.jar"
	unless $SwingX = $ENV{"SwingX"};
Add_to_CLASSPATH ($SwingX);


@Arguments = ("java", "-Xmx$KAPELLMEISTER_MEMORY");
push @Arguments, "-cp", $CLASSPATH
	if $CLASSPATH;

push @Arguments, "PIRL.Conductor.Maestro.Kapellmeister", @ARGV;

exec @Arguments;


sub Add_to_CLASSPATH
{
my ($pathname) = @_;

if (-e "$pathname")
	{
	if ($CLASSPATH)
		{
		$CLASSPATH = "$CLASSPATH:$pathname"
			unless "$pathname" =~ /"$pathname"/;
		}
	else
		{
		$CLASSPATH = $pathname;
		}
	}
}
