#!/bin/sh

if [ -z "$JAVA_SAGA_LOCATION" ] ; then
    export JAVA_SAGA_LOCATION="$SAGA_LOCATION"
fi

if [ -z "$JAVA_SAGA_LOCATION" ] ; then
    echo "please set JAVA_SAGA_LOCATION to the location of your SAGA installation" 1>&2
    exit 1
fi

# Jar-files from library.
SAGA_CLASSPATH=""
add_to_saga_classpath () {
    JARFILES=`cd "$1" && ls *.jar 2>/dev/null`
    for i in ${JARFILES} ; do
	if [ -z "$SAGA_CLASSPATH" ] ; then
	    SAGA_CLASSPATH="$1/$i"
	else
	    SAGA_CLASSPATH="$SAGA_CLASSPATH:$1/$i"
	fi
    done
}

add_to_saga_classpath $JAVA_SAGA_LOCATION/lib

# the java.endorsed.dirs is needed for Java 6. If not present, the Gridsam
# adaptor will not work.

java \
    -cp "$SAGA_CLASSPATH:$CLASSPATH" \
    -Djava.endorsed.dirs=$JAVA_SAGA_LOCATION/lib/adaptors/GridsamAdaptor/endorsed \
    -Dlog4j.configuration="file:$JAVA_SAGA_LOCATION/log4j.properties" \
    -Dsaga.location="$JAVA_SAGA_LOCATION" \
    "$@"
