#!/bin/sh
set -e

# whichjava - determine properties of the given Java runtime
#
# Copyright (c) 2001 Ben Burton <benb@acm.org>
# Written for Debian GNU/Linux
# Released under GPL
#
# See man page for further details.

echoerr () {
	echo "$1" > /dev/stderr
}

usage () {
	echoerr "Usage: $0 <runtime> [ <action> ] [ <variable> ... ]"
	echoerr
	echoerr "Runtime: path to a Java runtime command (eg. /usr/bin/java)"
	echoerr "Actions: --eval, --print, --value (default)"
	echoerr "Variables: bootstrap, jvm"
	echoerr
	echoerr "See the man page for further details."
	echoerr
	exit 1
}

addaction () {
	if [ -z "$action" ]; then
		action="$1"
	else
		echoerr "More than one action has been specified!"
		echoerr
		usage
	fi
}

addvariable () {
	if [ -z "$variables" ]; then
		variables="$1"
	else
		variables="$variables $1"
	fi
}

classesnotfound () {
	echoerr "The core $1 classes could not be found."
	echoerr
	echoerr "Set the environment variable \$BOOTSTRAP_CLASSES to a"
	echoerr "colon-separated list of jars or directories containing"
	echoerr "the core $1 classes (such as $2)"
	echoerr "and try again."
	echoerr
	exit 1
}

# Pull the runtime command from the command line.
if [ -z "$1" ]; then
	usage
fi

JAVA="$1"
shift

if ! which "$JAVA" 2>&1 > /dev/null; then
	echoerr "The command \"$JAVA\" cannot be found."
	echoerr
	usage
fi

# Find which action and variables were specified on the command line.
action=
variables=
for param; do
	case "$param" in
		bootstrap )
			addvariable BOOTSTRAP_CLASSES
		;;
		jvm )
			addvariable JVM
		;;
		--eval )
			addaction doeval
		;;
		--print )
			addaction doprint
		;;
		--value )
			addaction dovalue
		;;
		* )
			echoerr "Unknown command-line argument: $param"
			echoerr
			usage
	esac
done

# Use default if nothing was specified.
if [ -z "$variables" ]; then
	variables="JVM BOOTSTRAP_CLASSES"
fi
if [ -z "$action" ]; then
	action=dovalue
fi

# Determine whose JVM we are using.
if [ -z "$JVM" ]; then
	version=
	version=`"$JAVA" -version 2>&1` || true
	case "$version" in
		# Search for a string not included in $JAVA so we don't accidentally
		# catch output such as "kaffe: Command not found" and the like.
		*[Bb]lackdown* )
			JVM=blackdown
		;;
		*libgcj* )
			JVM=gij
		;;
		*[Tt]ransvirtual* )
			JVM=kaffe
		;;
		*Open\ Runtime\ Platform* )
			JVM=orp
		;;
		java\ version\ \"1.1.8\" )
			JVM=sun
		;;
		* )
			# No idea.
			JVM=unknown
		;;
	esac
fi

# Determine the bootstrap classes.
if [ -z "$BOOTSTRAP_CLASSES" ]; then
	case "$JVM" in
		gij )
			if [ -e /usr/share/java/libgcj.jar ]; then
				BOOTSTRAP_CLASSES=/usr/share/java/libgcj.jar
			else
				classesnotfound "GNU Java" /usr/share/java/libgcj.jar
			fi
		;;
		kaffe )
			if [ -e /usr/lib/kaffe/lib/rt.jar ]; then
				kaffejars=`ls /usr/lib/kaffe/lib/*.jar`
				BOOTSTRAP_CLASSES=`echo $kaffejars | tr ' ' ':'`
			elif [ -e /usr/share/kaffe/Klasses.jar ]; then
				kaffejars=`ls /usr/share/kaffe/*.jar`
				BOOTSTRAP_CLASSES=`echo $kaffejars | tr ' ' ':'`
			else
				classesnotfound "Kaffe" /usr/share/kaffe/Klasses.jar
			fi
		;;
		orp )
			if [ -e /usr/share/orp/classpath/java/lang/Object.class ]; then
				BOOTSTRAP_CLASSES=/usr/share/orp/classpath
			else
				classesnotfound "ORP" /usr/share/orp/classpath
			fi
		;;
		blackdown )
			if [ -e /usr/lib/j2se/1.3/jre/lib/rt.jar ]; then
				jrejars=`ls /usr/lib/j2se/1.3/jre/lib/*.jar`
				BOOTSTRAP_CLASSES=`echo $jrejars | tr ' ' ':'`
			elif [ -e /usr/lib/j2re1.3/lib/rt.jar ]; then
				jrejars=`ls /usr/lib/j2re1.3/lib/*.jar`
				BOOTSTRAP_CLASSES=`echo $jrejars | tr ' ' ':'`
			else
				classesnotfound "Blackdown Java" /usr/lib/j2se/1.3/jre/lib/rt.jar
			fi
		;;
		sun )
			if [ -e /usr/lib/jdk1.1/lib/classes.zip ]; then
				BOOTSTRAP_CLASSES=/usr/lib/jdk1.1/lib/classes.zip
			else
				classesnotfound "Sun Java" /usr/lib/jdk1.1/lib/classes.zip
			fi
		;;
		* )
			# No idea which JVM we're using.
			BOOTSTRAP_CLASSES=
		;;
	esac
fi

# Output according to the requested action.

for v in $variables; do
	case "$action" in
		doeval )
			command="echo \"$v=\\\"\$$v\\\" ;\""
		;;
		doprint )
			command="echo $v: \$$v"
		;;
		dovalue )
			command="echo \$$v"
		;;
		* )
			command=echo
		;;
	esac
	eval $command
done

