#!/bin/sh
#
# FreeBSD-specific startup script for Jakarta Ant.
#
# See: http://jakarta.apache.org/ant/
#
# $FreeBSD: ports/devel/jakarta-ant/files/ant,v 1.1 2002/10/11 15:12:57 znerd Exp $
#


DEFAULT_JAVACMD=%%JAVA%%
DEFAULT_JAVA_HOME=%%JAVA_HOME%%
DEFAULT_JIKES=false
ANT_HOME=%%ANT_HOME%%


#-----------------------------------------------------------------------------
# Determine configuration settings
#-----------------------------------------------------------------------------

# Load system-wide configuration
if [ -f "%%PREFIX%%/etc/ant.conf" ]; then 
	. %%PREFIX%%/etc/ant.conf
fi

# Load user-specific configuration
if [ -f "${HOME}/.antrc" ]; then 
	. "${HOME}/.antrc"
fi

# Determine if Jikes should be used
if [ -z "${usejikes}" ]; then
	usejikes=${DEFAULT_JIKES};
fi

# Determine the JDK home directory
if [ -z "${JAVA_HOME}" ]; then
	JAVA_HOME=${DEFAULT_JAVA_HOME}
fi

# Set ANT_LIB location
ANT_LIB="${ANT_HOME}/lib"

# Determine 'java' executable
if [ -z "${JAVACMD}" ]; then 
	if [ -n "${JAVA_HOME}"  ]; then
		JAVACMD="${JAVA_HOME}/bin/java"
	else
		JAVACMD=${DEFAULT_JAVACMD}
	fi
fi
 
# Check if 'java' executable can be found
if [ ! -x "$JAVACMD" ]; then
	echo "Error: JAVA_HOME is not defined correctly: ${JAVA_HOME}"
	echo "       Unable to find ${JAVACMD}"
	exit 1
fi


#-----------------------------------------------------------------------------
# Determine CLASSPATH
#-----------------------------------------------------------------------------

# Initialize the classpath to the existing classpath
if [ -n "$CLASSPATH" ]; then
	LOCALCLASSPATH="${CLASSPATH}"
fi

# Add the dependency .jar files from ${ANT_HOME}/lib
for JAR_FILE in "${ANT_LIB}"/*.jar; do

	# If the directory is empty, then the input string is returned
	if [ -f "${JAR_FILE}" ]; then
		if [ -z "$LOCALCLASSPATH" ]; then
			LOCALCLASSPATH="${JAR_FILE}"
		else
			LOCALCLASSPATH="${JAR_FILE}":"$LOCALCLASSPATH"
		fi
	fi
done

# FreeBSD-specific: Add the .jar files from ${PREFIX}/share/java/classes
for JAR_FILE in "%%PREFIX%%/share/java/classes"/*.jar; do

	# If the directory is empty, then the input string is returned
	if [ -f "${JAR_FILE}" ]; then
		if [ -z "${LOCALCLASSPATH}" ]; then
			LOCALCLASSPATH="${JAR_FILE}"
		else
			LOCALCLASSPATH="${JAR_FILE}":"${LOCALCLASSPATH}"
		fi
	fi
done

# Add the JDK tools.jar or classes.zip file to the CLASSPATH
if [ -n "${JAVA_HOME}" ]; then
	if [ -f "${JAVA_HOME}/lib/tools.jar" ]; then
		LOCALCLASSPATH="${LOCALCLASSPATH}:${JAVA_HOME}/lib/tools.jar"
	elif [ -f "${JAVA_HOME}/lib/classes.zip" ]; then
		LOCALCLASSPATH="${LOCALCLASSPATH}:${JAVA_HOME}/lib/classes.zip"
	fi
else
	echo "Warning: JAVA_HOME environment variable is not set."
	echo "  If build fails because sun.* classes could not be found"
	echo "  you will need to set the JAVA_HOME environment variable"
	echo "  to the installation directory of java."
fi


#-----------------------------------------------------------------------------
# Determine all Ant options
#-----------------------------------------------------------------------------

# Add Jikes flag if appropriate
if ${usejikes}; then
	ANT_OPTS="${ANT_OPTS} -Dbuild.compiler=jikes"
fi

# Add the Jikes path to the options if necessary
if [ -n "${JIKESPATH}" ]; then
	ANT_OPTS="${ANT_OPTS} -Djikes.class.path=${JIKESPATH}"
fi

# Add option for the CLASSPATH
ANT_OPTS="${ANT_OPTS} -classpath ${LOCALCLASSPATH}"

# Add option for Ant home directory
ANT_OPTS="${ANT_OPTS} -Dant.home=${ANT_HOME}"


#-----------------------------------------------------------------------------
# Call Ant
#-----------------------------------------------------------------------------

if [ -n "${VERBOSE_ANT_SCRIPT}" ]; then
	echo "Java command: ${JAVACMD}"
	echo "Ant options: ${ANT_OPTS}"
	echo "Ant arguments: ${ANT_ARGS}"
fi

${JAVACMD} ${ANT_OPTS} org.apache.tools.ant.Main ${ANT_ARGS} $@
