# Eclass for simple bare-source Java packages
#
# Copyright (c) 2004-2008, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2
#
# $Header: $

inherit java-pkg-2

# -----------------------------------------------------------------------------
# @eclass-begin
# @eclass-summary Eclass for Java sources without build instructions
#
# This class is intended to build pure Java packages from Java sources
# without the use of any build instructions shipped with the sources.
# There is no support for resources besides the generated class files,
# or for generating source files, or for controlling the META-INF of
# the resulting jar, although these issues may be addressed by an
# ebuild between the src_compile and the src_install phase.
# -----------------------------------------------------------------------------

EXPORT_FUNCTIONS src_compile src_install

# We are only interested in finding all java source files, wherever they may be.
S="${WORKDIR}"

# -----------------------------------------------------------------------------
# @variable-external JAVA_DEPEND
# @variable-default ""
#
# Comma separated list of java packages to include in the class path.
# The packages will also be registered as runtime dependencies of this
# new package. Dependencies will be calculated transitively. See
# "java-config -l" for appropriate package names.
# -----------------------------------------------------------------------------
# JAVA_DEPEND

# ------------------------------------------------------------------------------
# @eclass-src_compile
#
# src_compile for simple bare source java packages. Finds all *.java
# sources in ${S}, compiles them with the classpath calculated from
# ${JAVA_DEPEND}, and packages the resulting classes to ${PN}.jar.
#
# variables: JAVA_DEPEND - list java packages to put on the classpath.
# ------------------------------------------------------------------------------
java-pkg-simple_src_compile() {
	find * -name \*.java > sources.lst
	mkdir target || die "Could not create target directory"
	local classpath="target"
	if [[ ${JAVA_DEPEND} ]]; then
		classpath="${classpath}:$(java-pkg_getjars --with-dependencies ${JAVA_DEPEND})"
	fi
	debug-print "CLASSPATH=${classpath}"
	echo "javac -d target -cp ${classpath} @sources.lst"
	ejavac -d target -cp "${classpath}" @sources.lst
}

# ------------------------------------------------------------------------------
# @eclass-src_install
#
# src_install for simple single jar java packages. Simply packages the
# contents from the target directory and installs it as ${PN}.jar. If
# the file target/META-INF/MANIFEST.MF exists, it is used as the
# manifest of the created jar.
# ------------------------------------------------------------------------------
java-pkg-simple_src_install() {
	local jar_args="cf ${PN}.jar"
	if [[ -e target/META-INF/MANIFEST.MF ]]; then
		jar_args="cfm ${PN}.jar target/META-INF/MANIFEST.MF"
	fi
	echo "jar ${jar_args} -C target ."
	jar ${jar_args} -C target . || die "jar failed"
	java-pkg_dojar ${PN}.jar
}

# ------------------------------------------------------------------------------
# @eclass-end
# ------------------------------------------------------------------------------