#!/bin/sh
set -e

# whichjavac - determine properties of the given Java compiler
#
# 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 <compiler> [ <action> ] [ <variable> ... ]"
	echoerr
	echoerr "Compiler: path to a Java compiler (eg. /usr/bin/javac)"
	echoerr "Actions: --eval, --print, --value (default)"
	echoerr "Variables: bootstrap, variant"
	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 \$JAVAC_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 compiler command from the command line.
if [ -z "$1" ]; then
	usage
fi

JAVAC="$1"
shift

if ! which "$JAVAC" 2>&1 > /dev/null; then
	echoerr "The command \"$JAVAC\" 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 JAVAC_BOOTSTRAP_CLASSES
		;;
		variant )
			addvariable JAVAC_VARIANT
		;;
		--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="JAVAC_VARIANT JAVAC_BOOTSTRAP_CLASSES"
fi
if [ -z "$action" ]; then
	action=dovalue
fi

# Determine whose compiler we are using.
if [ -z "$JAVAC_VARIANT" ]; then
	version=
	version=`"$JAVAC" --help 2>&1` || true
	case "$version" in
		# Search for a string not included in $JAVAC so we don't accidentally
		# catch output such as "jikes: Command not found" and the like.
		*Jikes\ Compiler* )
			JAVAC_VARIANT=jikes
		;;
		*www.gnu.org* )
			JAVAC_VARIANT=gcj
		;;
		*possible\ options\ include* )
			JAVAC_VARIANT=blackdown
		;;
		*invalid\ flag:\ --help* )
			JAVAC_VARIANT=sun
		;;
		* )
			# No idea.
			JAVAC_VARIANT=unknown
		;;
	esac
fi

# Determine the bootstrap classes.
if [ -z "$JAVAC_BOOTSTRAP_CLASSES" ]; then
	case "$JAVAC_VARIANT" in
		jikes )
			# Try using whichjava.
			if [ -z "$JAVA" ]; then
				JAVA=/usr/bin/java
			fi
			if ! which "$JAVA" 2>&1 > /dev/null; then
				echoerr "The command \"$JAVA\" cannot be found."
				echoerr
				echoerr "Jikes needs to use the core Java classes that are"
				echoerr "provided by your Java interpreter."
				echoerr
				echoerr "Set the environment variable \$JAVA to your Java"
				echoerr "interpreter (eg. /usr/bin/java) and try again."
				echoerr
				exit 1
			fi
			if eval `/usr/share/jython/whichjava "$JAVA" --eval`; then
				JAVAC_BOOTSTRAP_CLASSES="$BOOTSTRAP_CLASSES"
			fi
			if [ -z "$JAVAC_BOOTSTRAP_CLASSES" ]; then
				classesnotfound "Java" /usr/share/java/libgcj.jar
			fi
		;;
		* )
			# Don't need to specify bootstrap classes.
			JAVAC_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

