#!/usr/bin/perl
#
# Conductor
#
# A wrapper for the Conductor process management application.
#
#	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 or provided as the value of an environment
#	variable having the names listed here:
#
#	MySQL_JDBC - http://dev.mysql.com/downloads/connector/j/
#
#	The pathname to the MySQL JDBC driver jar file or the root directory
#	where its class files are located. Either the MySQL JDBC Driver or
#	the PostgreSQL JDBC Driver is required to provide Database support.
#	Default: $PIRL_JAVA_HOME/mysql-connector/mysql-connector.jar
#
#	PostgreSQL_JDBC - http://jdbc.postgresql.org/
#
#	The pathname to the PostgreSQL JDBC driver jar file or the root
#	directory where its class files are located. Either the PostgreSQL
#	JDBC Driver or the MySQL JDBC Driver is required to provide Database
#	support.
#	Default: $PIRL_JAVA_HOME/PostgreSQL/postgresql.jar
#
#	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
#
#	JAVAMAIL - http://www.oracle.com/technetwork/java/javamail/
#	JAF - http://java.sun.com/products/archive/javabeans/jaf11.html
#
#	The pathnames to the JavaX JavaMail and JavaBeans Activation
#	Framework jar files or the root directory where their class files
#	are located. These packages provide email message sending
#	functionality as used by the Notify class. Note: These packages are
#	only required when Notify must use an "unfriendly" email server.
#	They may not be required at all, in which case a Notify-simple.java
#	implementation is provided that only uses JFC classes and worked
#	fine for many years until a "sophisticated" email server was
#	encountered.
#
#
#	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.
#
#	Conductor's Native_Methods library is required to enable a Conductor
#	to obtain its process ID which is appended to the hostname so each
#	Conductor running on a system will be uniquely identified by the
#	Conductor_ID field value in the Sources table. Though this is not
#	required it is recommended. The Native_Methods library is built at
#	the same time that the Conductor class files are built; the library
#	can be found in the PIRL/Conductor/<OS>.<ARCH> directory, where
#	<OS> is the operating system name (from uname -s) and <ARCH> is
#	the host system architecture (from uname -m). The directory where
#	this library is located should be in the LD_LIBRARY_PATH
#	(DYLD_LIBRARY_PATH for Apple OS X systems) environment variable value
#	(a colon separated list of directory pathnames). An attempt will be
#	made to find the library in the $PIRL_JAVA_HOME and $CLASSPATH
#	pathname list. If it is found the pathname to the libarary is
#	appended to the LD_LIBRARY_PATH (or DYLD_LIBRARY_PATH) environment
#	variable value.
#
#	Note: On Apple OS X (Darwin) systems the Java Virtual Machine will
#	automatically include the ~/Library/Java/Extensions and
#	/Library/Java/Extensions directories, if they exist, in the pathnames
#	searched for dynamically linked libraries such as Conducor's
#	Native_Methods library.
#	
#
#	CVS ID: Conductor,v 2.5 2012/01/25 23:12:08 castalia Exp

$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);

$MySQL_JDBC			= "$PIRL_JAVA_HOME/mysql-connector/mysql-connector.jar"
	unless $MySQL_JDBC		= $ENV{"MySQL_JDBC"};
Add_to_CLASSPATH ($MySQL_JDBC);

$PostgreSQL_JDBC	= "$PIRL_JAVA_HOME/PostgreSQL/postgresql.jar"
	unless $PostgreSQL_JDBC	= $ENV{"PostgreSQL_JDBC"};
Add_to_CLASSPATH ($PostgreSQL_JDBC);

$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);

$JAVAMAIL			= "$PIRL_JAVA_HOME/javamail/mail.jar"
	unless $JAVAMAIL		= $ENV{"JAVAMAIL"};
Add_to_CLASSPATH ($JAVAMAIL);

$JAF				= "$PIRL_JAVA_HOME/jaf/activation.jar"
	unless $JAF				= $ENV{"JAF"};
Add_to_CLASSPATH ($JAF);


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

#	Apple's OS X JVM won't allow Java classes to be run remotely
#	if they employ any graphics (Swing) functionality
#	regardless of whether this functionally is actually used.
#	The workaround is to tell the JVM to run "headless" when
#	the monitor mode is not specified on the command line.
$Graphics_Mode = "-Djava.awt.headless=true";
foreach $arg (@ARGV)
	{
	if ($arg =~ /^-[Mm]/)
		{
		undef $Graphics_Mode;
		last;
		}
	}
push @Arguments, $Graphics_Mode
	if $Graphics_Mode;

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


#	Search for the Conductor Native_Methods library.

chomp ($OS = `uname -s`);
if ($OS == "Darwin")
	{$ld_library_path_name = "DYLD_LIBRARY_PATH"}
else
	{$ld_library_path_name = "LD_LIBRARY_PATH";}

if ($CLASSPATH)
	{
	chomp ($ARCH = `uname -m`);
	$JNI_DIR = "$OS.$ARCH";
	@paths = split /:/, $CLASSPATH;
	foreach $path (@paths)
		{
		if (-d "$path/PIRL/Conductor/$JNI_DIR")
			{
			$ld_libary_path = $ENV{$ld_library_path_name};
			if ($ld_libary_path)
				{
				$ld_libary_path = "$path/PIRL/Conductor/$JNI_DIR:$ld_libary_path";
				}
			else
				{
				$ld_libary_path = "$path/PIRL/Conductor/$JNI_DIR";
				}
			$ENV{$ld_library_path_name} = $ld_libary_path;
			last;
			}
		}
	}

exec @Arguments;


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

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