#! /bin/sh

#
# Driver script for Kaffe.
# This script invokes the "Kaffe" executable after modifying
# CLASSPATH and LD_LIBRARY_PATH as needed.
#
# We prepend kaffe's lib directory to LD_LIBRARY_PATH.
#
# KAFFELIBRARYPATH is defined to kaffe's native lib directory
# (lib/kaffe) if not set.
#
# The effective CLASSPATH is made up of four parts, which 
# are composed in the following order:
#
#	1. a user-specified CLASSPATH or .
#	2. Klasses.jar in $datadir/kaffe
#       3. Kaffe extensions jar files in $datadir/kaffe
#	4. kjc.jar in $datadir/kaffe
#	5. classes.zip in $datadir/kaffe iff it exists.
#
# Note that as the last thing we slurp in the files $datadir/kafferc
# and $HOME/.kafferc (in that order) if they exist. This allows site
# and user specific JAR files to be automatically added to CLASSPATH.
#

# If KAFFE_DEBUG is set to either gdb or emacs, a debugging session
# will be started.  gdb will be started so that the current working
# directory and the argument list are correct.

prefix="/Swarm-2.2"
exec_prefix="${prefix}"
PATHSEP=';'
DIRSEP='\\'
: ${KAFFE_LIBDIR="${exec_prefix}/lib"}
: ${KAFFE_LIBEXECDIR="${exec_prefix}/libexec"}
: ${KAFFE_CLASSDIR="${prefix}/share/kaffe"}
: ${KAFFE_NATIVE_LIBRARY_DIR="${exec_prefix}/lib/kaffe"}
: ${KAFFE_OLD_NATIVE_LIBRARY_DIR="$KAFFE_CLASSDIR/lib/i386-cygwin32"}

if test x"${KAFFEHOME+set}"x != x"set"x; then
  KAFFEHOME="/Swarm-2.2"
fi
export KAFFEHOME

CLASSPATH="${CLASSPATH-.}"
for f in Klasses.jar comm.jar pjava.jar servlet.jar tools.jar microsoft.jar kjc.jar rmi.jar; do
  if test -f "$KAFFE_CLASSDIR/$f"; then
    CLASSPATH="$CLASSPATH$PATHSEP$KAFFE_CLASSDIR/$f"
  fi
done
if test -f "$KAFFE_CLASSDIR/classes.zip"; then
  CLASSPATH="$CLASSPATH$PATHSEP$KAFFE_CLASSDIR/classes.zip"
fi
case "$DIRSEP" in /) ;;
*) CLASSPATH=`echo "$CLASSPATH" | sed 's,/,'"$DIRSEP"',g'` ;;
esac
export CLASSPATH
KAFFELIBRARYPATH="$KAFFE_NATIVE_LIBRARY_DIR$PATHSEP$KAFFE_LIBDIR"${KAFFELIBRARYPATH+"$PATHSEP$KAFFELIBRARYPATH"}${LD_LIBRARY_PATH+"$PATHSEP${LD_LIBRARY_PATH}"}
if test -d "$KAFFE_OLD_NATIVE_LIBRARY_DIR"; then
  KAFFELIBRARYPATH="$KAFFELIBRARYPATH$PATHSEP$KAFFE_OLD_NATIVE_LIBRARY_DIR"
fi
export KAFFELIBRARYPATH
LD_LIBRARY_PATH="$KAFFE_LIBDIR"${LD_LIBRARY_PATH+"$PATHSEP$LD_LIBRARY_PATH"}
export LD_LIBRARY_PATH
PATH="$PATH:$KAFFE_LIBDIR" # MS-Windows DLLs must be in the PATH
export PATH

# Slurp in system and user specific scripts
if [ -f "${prefix}/share/kaffe/kafferc" ]; then
    . ${prefix}/share/kaffe/kafferc
fi
if [ -f "${HOME}/.kafferc" ]; then
    . "${HOME}/.kafferc"
fi

if test x"${KAFFE_DEBUG+set}"x != x"set"x; then
    exec $KAFFE_LIBEXECDIR/Kaffe ${1+"$@"}
else
    case `"$KAFFE_DEBUG" --version </dev/null 2>/dev/null` in
    *"GDB"* | *"GNU gdb"*)
	# start command-line gdb within the correct working directory,
	# and set up the specified argument list
	{
	    echo "shell rm -f /tmp/.gdbinit.$$"
	    echo "cd `pwd`"
	    echo "set args $*"
	} >/tmp/.gdbinit.$$
	exec "$KAFFE_DEBUG" -command /tmp/.gdbinit.$$ $KAFFE_LIBEXECDIR/Kaffe
	rm -f /tmp/.gdbinit.$$;;
    DDD*)
	# Tell DDD to start gdb within the correct working directory,
	# and set up the specified argument list
	{
	    echo "shell rm -f /tmp/.gdbinit.$$"
	    echo "cd `pwd`"
	    echo "set args $*"
	} >/tmp/.gdbinit.$$
        exec "$KAFFE_DEBUG" --debugger "${GDB-gdb} -command /tmp/.gdbinit.$$" \
	  $KAFFE_LIBEXECDIR/Kaffe
	rm -f /tmp/.gdbinit.$$;;
    "GNU Emacs"*)
	# start gdb within GNU Emacs, move into the current working
	# directory and set up the specified argument list
	exec "$KAFFE_DEBUG" -eval "(progn (gdb \"gdb $KAFFE_LIBEXECDIR/Kaffe\") (gud-call \"cd `pwd`\") (gud-call \"set args $*\"))";;
    XEmacs*)
	# start gdb within XEmacs, move into the current working
	# directory and set up the specified argument list
	exec "$KAFFE_DEBUG" -eval "(progn (gdb \"$KAFFE_LIBEXECDIR/Kaffe\") (gdb-call \"cd `pwd`\") (gdb-call \"set args $*\"))";;
    *) echo you must set KAFFE_DEBUG to either emacs or gdb >&2;;
    esac
fi
exit 1
