#!/bin/sh
# 2005-02-02, based on the start script from freemind package
# 2006-03-11, modified to start openjump instead of jump
# 2006-05-20, stolen from the openjump package

#
# Note that we don't need half of the complicated classpath stuff
# but who knows, one day, maybe ...
#

airportjar=IPInspector.jar

DEB_JAR_PATH="/usr/share/java"

_debug()
{
	if [ -n "${DEBUG}" ]
	then
		echo "DEBUG:   $1" >&2 
		shift
		for text in "$@"
		do
			echo "         ${text}" >&2
		done
	fi
}

_error()
{
	echo "ERROR:   $1" >&2
	shift
	for text in "$@"
	do
		echo "         ${text}" >&2
	done
}

findjava()
{
	# We try hard to find the proper 'java' command
	if [ -n "${JAVACMD}" ] && [ -x "${JAVACMD}" ]
	then
		_debug "Using \$JAVACMD to find java virtual machine."
	elif [ -n "${JAVA_BINDIR}" ] && [ -x "${JAVA_BINDIR}/java" ]
	then
		JAVACMD="${JAVA_BINDIR}/java"
		_debug "Using \$JAVA_BINDIR to find java virtual machine."
	elif [ -n "${JAVA_HOME}" ] && [ -x "${JAVA_HOME}/bin/java" ]
	then
		JAVACMD="${JAVA_HOME}/bin/java"
		_debug "Using \$JAVA_HOME to find java virtual machine."
	else
		JAVACMD=$(which java)
		if [ -n "${JAVACMD}" ] && [ -x "${JAVACMD}" ]
		then
			_debug "Using \$PATH to find java virtual machine."
		elif [ -x /usr/bin/java ]
		then
			_debug "Using /usr/bin/java to find java virtual machine."
			JAVACMD=/usr/bin/java
		fi
	fi

	# if we were successful, we return 0 else we complain and return 1
	if [ -n "${JAVACMD}" ] && [ -x "${JAVACMD}" ]
	then
		_debug "Using '$JAVACMD' as java virtual machine..."
		return 0
	else
		_error "Couldn't find a java virtual machine," \
		       "define JAVACMD, JAVA_BINDIR, JAVA_HOME or PATH."
		return 1
	fi
}

_debug "AirPort utility parameters are '${@}'."

findjava
if [ $? -ne 0 ]
then
	exit 1
fi

# List of java jar libraries from $DEB_JAR_PATH to load
JARS=""

AP_CLASSPATH=
for jar in $JARS; do
    if [ -f $DEB_JAR_PATH/$jar.jar ] ; then
	AP_CLASSPATH="$AP_CLASSPATH:$DEB_JAR_PATH/$jar.jar"
    else
	_error "missing $DEB_JAR_PATH/$jar.jar"
    fi
done

_debug "Used classpath is '${AP_CLASSPATH}'"

if [ -z "$JAVA_ARGS" ] ; then
# No special args required yet
    JAVA_ARGS=""
fi

exec ${JAVACMD} \
    ${JAVA_ARGS} \
    -jar /usr/share/java/airport-utils/$airportjar
