#!/bin/sh
# $Id: henplus,v 1.13 2006-03-19 22:41:56 hzeller Exp $
##

# additional flags you might want to pass to the java interpreter
# like -Dfile.encoding=ISO-8859-1
JAVA_FLAGS=

if [ -z "$JAVA_HOME" ] ; then
    # try to use java in path
    JAVA=java
else
    JAVA=$JAVA_HOME/bin/java
fi

##------------------
THISDIR=`dirname $0`
# install layout is: script in /usr/bin/henplus and libs in /usr/share/henplus
HENPLUSDIR=$THISDIR/../share/henplus

# not yet installed ? Then look in the local build directory
if [ ! -d "$HENPLUSDIR" ] ; then
	HENPLUSDIR=$THISDIR/..
	for f in $HENPLUSDIR/lib/*.jar ; do
	    CLASSPATH=$CLASSPATH:$f
	done
	CLASSPATH=$HENPLUSDIR/build/classes:$CLASSPATH
fi

# location of the readline lib.
# Modify this, if you installation stores this at a different
# position.
LD_LIBRARY_PATH=$THISDIR/../lib:/usr/lib/jni:$LD_LIBRARY_PATH
CLASSPATH=$CLASSPATH:$THISDIR/../share/java/libreadline-java.jar

# you may just throw your own jar files in this dir.
# (like additional JDBC-drivers, that are not in default
# classpath)
for f in $HENPLUSDIR/*.jar $HENPLUSDIR/*.zip ; do
    if [ -r "$f" ] ; then
        CLASSPATH=$CLASSPATH:$f
    fi
done

# starting at the current directory we look for additional
# JDBC-drivers in .henplus/lib-directories up the path. 
# if no directory like that has been found the one in the 
# home directory will be used
DIR=`pwd`
CUSTOM_HENPLUSDIR=""

while [ $DIR != "/" ] ; do
        if [ -d $DIR"/.henplus/lib" ]; then
            CUSTOM_HENPLUSDIR=$DIR"/.henplus/lib"
	    break
        fi
        DIR=`dirname $DIR`
done

if [ -z $CUSTOM_HENPLUSDIR ]; then
     if [ -d "/.henplus/lib" ]; then
         CUSTOM_HENPLUSDIR="/.henplus/lib"
     else
         CUSTOM_HENPLUSDIR=$HOME/.henplus/lib
     fi
fi

for f in $CUSTOM_HENPLUSDIR/*.jar $CUSTOM_HENPLUSDIR/*.zip ; do
    if [ -r "$f" ] ; then
        CLASSPATH=$CLASSPATH:$f
    fi
done

export CLASSPATH LD_LIBRARY_PATH

exec $JAVA $JAVA_FLAGS henplus.HenPlus "$@"
