#!/bin/sh

#$cb
#    This file is a part of libopenmhp.
#
#    Copyright (C) 2005 Mikko Novaro <miknov@gmail.com>
#
#    See main header file (libopenmhp/openmhp.h) for licensing and
#    contact information.
#$ce

# Script for running an xlet in OpenMHP.
# parameters: <xlet name> <xlet class> <xlet base dir> <virtual X server> <Virtual X display number> <OpenMHP output destination>

NAME=$1
CLASS=$2
BASEDIR=$3
VIRTUALX_SERVER=$4 # virtual X server: none, xvfb or xvnc
VIRTUALX_DISPLAY=$5
OPENMHP_OUTPUT=$6 # "display", "log" or "discard"

LIB_JAVA=`pwd`/PLUGINS/src/openmhp/libopenmhp/java
OPENMHP_DIR=/usr/local/src/openmhp_lib

# directory for log files:
mkdir /tmp/openmhp-log 2>/dev/null

cd $BASEDIR

echo Starting java with parameters $NAME, $CLASS, $BASEDIR, $VIRTUALX_SERVER, $VIRTUALX_DISPLAY, $OPENMHP_OUTPUT

# path for the JNI methods library:
export LD_LIBRARY_PATH=$LIB_JAVA/build/jni

case $OPENMHP_OUTPUT in
    "display")
	OPENMHP_OUT="/dev/stdout"
	OPENMHP_ERR="/dev/stderr"
	;;
    "log")
	OPENMHP_OUT="/tmp/openmhp/openmhp-$NAME.log"
	OPENMHP_ERR="2>&1"
	;;
    "discard")
	OPENMHP_OUT="/dev/null"
	OPENMHP_ERR="2>&1"
	;;
    *)
	echo Illegal OPENMHP_OUTPUT parameter. Exiting.
	exit
	;;
esac

case $VIRTUALX_SERVER in
    "none")
	echo ----Virtual X: none----
	;;
    "xvfb")
	echo ----Virtual X: Xvfb----
	# Xvfb :1 -screen 0 720x576x32 &
	Xvfb :$VIRTUALX_DISPLAY &
	DISPLAY=:$VIRTUALX_DISPLAY
	;;
    "xvnc")
	echo ----Virtual X: Xvnc----
	Xvnc :$VIRTUALX_DISPLAY &
	DISPLAY=:$VIRTUALX_DISPLAY
	;;
    *)
	echo Illegal VIRTUALX_SERVER parameter. Exiting.
	exit
	;;
esac

# Run the xlet. Some OpenMHP classes are replaced by modified versions
# in the "java" subdirectory of the libopenmhp dir
# (libopenmhp/java/org/openmhp/...). Path for the modified versions
# must be set _before_ the originals in the classpath.

CLASSPATH=".:$LIB_JAVA/build:$OPENMHP_DIR/openmhp:$OPENMHP_DIR/JMF/lib/jmf.jar:$OPENMHP_DIR/javatv_fcs/javatv.jar:$OPENMHP_DIR/xerces/xercesImpl.jar:$OPENMHP_DIR/xerces/xml-apis.jar"

PARAMS="$OPENMHP_DIR/openmhp $CLASS 0 0 $OPENMHP_DIR/openmhp"

java -classpath $CLASSPATH org.openmhp.system.RunXlet $PARAMS \
 >> $OPENMHP_OUT 2>$OPENMHP_ERR
