#!/bin/bash
#
# drmstarter [--rootdir <dirname>] classname [pars to be passed to the class]
#
# This script starts a class from the DRM package (a console or a node).
# If they are not defined, 
# tries to load DRM_JVM and DRM_HOME from the default config files
# /etc/drm.config and $HOME/.dream/drm.config (if the later exists, it
# overrides the first). If no value is found, DRM_HOME defaults to 
# /usr/dr-ea-m, and DRM_JVM is the application 'java' within the path.
# Some search for a java installation is performed if there is no java in
# the path.
#
# If rootdir is given then that directory together with all directories
# uder it will be given application rights, together
# with the optional libraries. The point is that agents of experiments with
# a jar file within or under rootdir will have application rights on the node 
# started this way which is necessary to write the disk, etc.
#

#
# finding DRM_CONFIG
#
unset DRM_CONFIG
if [ -r /etc/drm.config ]; then
	export DRM_CONFIG="/etc/drm.config";
fi
if [ -r ${HOME}/.dream/drm.config ]; then
	export DRM_CONFIG=${HOME}"/.dream/drm.config";
fi

###########################################################
###########################################################

if [ ! -z "$DRM_CONFIG" ]; then

#
# reading DRM_JVM from config file
#
if [ -z "$DRM_JVM" ]; then
	export DRM_JVM=`cat $DRM_CONFIG |
		while read AA BB; do 
			case $AA in (DRM_JVM) echo $BB ;; esac;
		done;`
fi

#
# reading DRM_HOME from config file
#
if [ -z "$DRM_HOME" ]; then
	export DRM_HOME=`cat $DRM_CONFIG |
		while read AA BB; do 
			case $AA in (DRM_HOME) echo $BB ;; esac;
		done;`;
fi
fi

###########################################################
###########################################################

#
# Checking DRM_HOME
#
if [ -z "$DRM_HOME" ]; then export DRM_HOME="/usr/dr-ea-m"; fi
if [ ! -r ${DRM_HOME}/lib/drm.jar ]; then
	echo $0": Directory '"$DRM_HOME"' is not a valid DRM_HOME!" 1>&2
	echo $0": Check your configuration!" 1>&2
	exit 1
fi

#
# Checking DRM_JVM
#
if [ -z "$DRM_JVM" ]; then
	if type java > /dev/null 2> /dev/null; then
		export DRM_JVM="java"
	else
		export USR_BINDIRS=`find /usr -type d -name bin -print`
		export DRM_JVM_TMP=`for i in $USR_BINDIRS; do find $i -maxdepth 1 -name java -print; done`
		IFS_ORIG=$IFS
		export IFS=" " # no newline thus
		export DRM_JVM_14=`echo $DRM_JVM_TMP | sed -n -e '/1.4/{;p;q;}'`
		export DRM_JVM_FIRST=`echo $DRM_JVM_TMP | sed -e '1q'`
		export IFS=$IFS_ORIG
		if [ ! -z "$DRM_JVM_14" ]; then
			export DRM_JVM="$DRM_JVM_14";
		else
			export DRM_JVM="$DRM_JVM_FIRST";
		fi
		if [ ! -x "$DRM_JVM"  ]; then 
			echo $0": Could not find java, set DRM_JVM!" 1>&2
			exit 1
		fi
	fi
fi

echo "DRM_JVM is \`"$DRM_JVM\`
echo "DRM_HOME is \`"$DRM_HOME\`

###########################################################
###########################################################

#
# creating classpaths
#
unset DRM_LIB_JARS
for i in `ls $DRM_HOME/lib`; do
	if [ -z "$DRM_LIB_JARS" ]; then
		export DRM_LIB_JARS=$DRM_HOME/lib/$i
	else
		export DRM_LIB_JARS=$DRM_LIB_JARS":"$DRM_HOME/lib/$i
	fi
done
export DRM_ALLLIB_JARS=$DRM_LIB_JARS
for i in `ls $DRM_HOME/optlib`; do
	export DRM_ALLLIB_JARS=$DRM_ALLLIB_JARS":"$DRM_HOME/optlib/$i
done

#
# starting the app
#
if [ $1 = "--rootdir" ]; then
	export DRMAPP_ROOTDIR="$2"
	shift 2
	$DRM_JVM -classpath $DRM_ALLLIB_JARS \
		-Dapp.home="$DRMAPP_ROOTDIR" -Ddrm.home=$DRM_HOME \
		-Djava.security.manager \
		-Djava.security.policy=${DRM_HOME}/appstarter.policy "$@"
else
	$DRM_JVM -classpath $DRM_ALLLIB_JARS \
		-Ddrm.home=$DRM_HOME \
		-Djava.security.manager \
		-Djava.security.policy=${DRM_HOME}/drm.policy "$@"
fi

