#!/bin/sh
# Emulate the jdk java command using gij
# (c) 2002-2003 Bernhard Rosenkraenzer <bero@arklinux.org>
# SUSE change:
# Call gij instead of gcj.
# Reason: See Suse/Novell Bugzilla bug #155729 and bug #157655:
# "gcj" is only available when gcc-java is installed,
# which cannot be installed by default (see bug #151732).
# Calling gij is the more correct executable because
# "java" is the runtime interpreter corresponding to gij
# while gcj is the compiler corresponding to javac.
# When calling gij there is an output 'java version ...'
# so that the echo commands for gcj have been deactivated.
# Additionally empty lines in the gij output are suppressed
# to have it as much as possible the same as the gcj output.

libgcjjar="/usr/share/java/libgcj-`/usr/bin/gij --version | \
    head -n 1 | sed 's:^gij .* version \([^[:space:]]*\) .*$:\1:'`.jar"

if [ -n "$CLASSPATH" ]; then
        clp="$CLASSPATH:$libgcjjar"
else
        clp="$libgcjjar"
fi
unset HAVE_APPNAME || :
while [ "$#" != 0 ]; do
        if [ -z "$HAVE_APPNAME" -a "$1" = "-version" ]; then
                # echo 'java version "1.4.2"' 1>&2
                gij --version | tr -s '\n' 1>&2
                exit 0
        elif [ -z "$HAVE_APPNAME" -a "$1" = "-jar" ]; then
		shift
		params="$params -jar \"`echo $1 |sed -e 's,",\\\\",g'`\""
		HAVE_APPNAME=yes
        elif [ -z "$HAVE_APPNAME" -a \( "$1" = "-help" -o "$1" = "-?" \) ]; then
		gij --help
		exit 0
        elif [ -z "$HAVE_APPNAME" -a "$1" = "-fullversion" ]; then
                echo 'java full version "gcj-1.4.2"' 1>&2
                exit 0
        elif [ -z "$HAVE_APPNAME" -a "$1" = "-showversion" ]; then
                # echo 'java version "1.4.2"' 1>&2
                gij --version | tr -s '\n' 1>&2
        elif [ -z "$HAVE_APPNAME" -a \( "$1" = "-classpath" -o "$1" = "-cp" \) ]; then
                shift
                clp="`echo $1 | sed \"s~.*/lib/rt.jar~$libgcjjar~\"`"
	elif [ "`echo $1 |cut -b1-2`" = "-D" ]; then
		if [ "`echo $1 |cut -b1-20`" = "-Djava.library.path=" ]; then
			export LD_LIBRARY_PATH=`echo $1 |cut -b21-`:$LD_LIBRARY_PATH
		elif [ "`echo $1 |cut -b1-12`" = "-Djava.home=" ]; then
		    HAVE_JAVA_HOME=yes
		elif [ "`echo $1 |cut -b1-15`" = "-Djava.version=" ]; then
		    HAVE_JAVA_VERSION=yes
		fi
		params="$params \"`echo $1 |sed -e 's,",\\\\",g'`\""
        elif [ -z "$HAVE_APPNAME" -a "`echo $1 |cut -b1`" = "-" ]; then
                # Yuck. Unknown parameter. Let's pretend nothing happened.
                echo "Warning: $1 not understood. Ignoring." >&2
        else
		if [ -n "$HAVE_APPNAME" ]; then
			params="$params \"`echo $1 |sed -e 's,",\\\\",g'`\""
		else
			params="$params \"`echo $1 |sed -e 's,",\\\\",g;s,/,.,g'`\""
			HAVE_APPNAME=yes
		fi
        fi
        shift
done

if [ -z "$HAVE_JAVA_HOME" ]; then
    params="-Djava.home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre $params"
fi

if [ -z "$HAVE_JAVA_VERSION" ]; then
    params="-Djava.version=1.4.2 $params"
fi

if ! echo $clp |grep -q "$libgcjjar"; then
	# We NEED this
	clp="$clp:$libgcjjar"
fi
export CLASSPATH="$clp"
COMMAND=`mktemp /tmp/javaXXXXXX`
echo "rm $COMMAND" >$COMMAND
echo "exec /usr/bin/gij $params" >>$COMMAND
chmod +x $COMMAND
exec $COMMAND
