#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../java-dirtree/java-conf.in
# Copyright (C) 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

# Common java configuration is needed as well.
. $base/package/*/*/java-common-conf.in

# Defaults
auto_detect=on
build_type=

# Prevent executing normal make and install build steps.
# Java packages have custom make and install.
makeopt=
makeinstopt=

# Set variables used for building java sources to there default values.
#  builddocdir - Location where documentation can be found after building.
#  buildjardir - Location where build results (jarfiles) can be found.
#  buildtarget - target(Ant)/goal(Maven) to be used for building.
#  buildfile   - buildfile containing the build rules.
builddocdir=dist/docs
buildjardir=dist
buildtarget=dist
buildfile=

# -------------------------------------------------------------------------
#
# -------------------------------------------------------------------------
maven1_build() {
	echo_status "Java buildstyle: Maven (version 1)"

	# Maven 2 needs to be installed to be able to use it.
	pkgprefix -t maven1

	# Invoke maven to start building. However, we strictly
	# forbid downloading dependancies from remote repositories.
	# All dependancies should be build locally and added to
	# the local repository.
	$root/$(pkgprefix bindir maven1)/maven \
		-Dmaven.repo.remote.enabled=false \
		$mavengoals --pom $1
}

# -------------------------------------------------------------------------
#
# -------------------------------------------------------------------------
maven2_build() {
	echo_status "Java buildstyle: Maven (version 2)"

	# Maven 2 needs to be installed to be able to use it.
	pkgprefix -t maven

	abort "MAVEN2 building NOT yet implemented."
}

# -------------------------------------------------------------------------
# ant_build: Starts the apache ant build tool.
#
# Besides starting the apache build tool. The results of building are
# gathered and moved to right place. For this two things are considered.
#   dist/docs	for documentation.
#   dist/*.jar  for java jar files/libraries.
#
# IMPORTANT VARIABLES:
#   builddocdir - Location where documentation will be.
#   buildjardir - Location where build result will be.
#   buildtarget - Ant target to be used for building.
# -------------------------------------------------------------------------
ant_build() {
	echo_status "Java buildstyle: Apache Ant"

	# Ant needs to be installed to be able to use it.
	pkgprefix -t apache-ant

	# Invoke Ant to start building.
	$root/$(pkgprefix bindir apache-ant)/ant -buildfile $1 \
		-lib $(pkgprefix libdir java-dirtree) \
		$antopt $buildtarget

	# Copy all created jar files to the package libdir.
	echo "Trying to copy package jar files."
	if ls $buildjardir/*.jar 2> /dev/null ; then
		cp -v $buildjardir/*.jar $root/$libdir
	else
		# Strange. No jar files available?
		echo "Package $pkg produces no jar files in $buildjardir/."
	fi

	# Copy all documentation to the package docdir.
	echo "Trying to copy package documentaion."
	tempdir=$builddocdir
	if [ -d $tempdir ] ; then
		( cd $tempdir; tar -c * | tar -x -C $root/$docdir )
	fi
	unset tempdir
}

# -------------------------------------------------------------------------
# set_build_type: Sets the java build type.
#
# Sets the java build type to be used for building the current package.
# -------------------------------------------------------------------------
set_build_type() {
	local builder_func=

	# Build type can only be set once per package, check if it
	# has already been set.
	if [ -n "$build_type" ] ; then
		echo "Buildtype already set ($build_type), can't set it to '$1'"
		Abort "java-conf.in: Buildtype can only be set once."
	fi
	build_type="$1"

	case "$build_type" in
		ANT)	builder_func="ant_build ${buildfile:-build.xml}" ;;
		MAVEN)  todo ;;
		SCRIPT) todo ;;
		*)	abort "java-conf.in: Unknown buildtype $1 specified." ;;
	esac

	# Since the built type is set, auto detection is no longer needed.
	auto_detect=off

	# Set the inmake hook to use the given builder type.
	hook_add inmake 5 "$builder_func"
}

# Before we continue lets process all command line options.
while [ "$1" ] ; do
	case "$1" in
		NO_AUTO_DETECT)		auto_detect=off ;
					echo_status "Java buildstyle: Autodetect disabled." ;;
		BUILD_TYPE=*)		set_build_type ${1#*=} ;;
		BUILD_FILE=*)		buildfile=${1#*=} ;;
		*)			abort "java-conf.in: Unknown arguments" ;;
	esac
	shift
done

# Check if at least one of the jdk's is available right now.
if [ -z $JAVA_HOME ]; then
	# No jdk available, continueing is pointless.
	abort "At least one of the JDK's need to be installed."
fi

# This function determines the maven version to be used on
# the given input file.
# param: pom/project filename
# return: the pom/project file major version number.
detect_maven_version() {
	# Todo:  look inside the file to determine the version.
	echo "3"
}

# We know how to build Ant, Maven and Maven 2 style projects.
# build and build.sh scripts are ignored on purpose. By controlling
# the build in here we might make it easier to build packages using
# exotic compilers like gcj or jikes.
determine_build_type() {
	local buildtype=

	# Check if the Ant build.xml file is available.
	if [ -f build.xml ]; then
		# Package can be build using Ant. However this might be
		# overruled by any of the others.
		buildtype="ANT"
	fi

	# Check if the Maven pom.xml or project.xml file is available.
	for mavenfile in pom.xml project.xml; do
		# Check if the maven file exists
		if [ -f $mavenfile ]; then
			# A maven file is available, but what maven
			# version should be used? The projectfile
			# version can tell us.
			pomversion=`detect_maven_version $mavenfile`
			case "$pomversion" in
				4*) builder_func="maven2_build $mavenfile" ;;
				# In all other cases we use maven 1.
				*) builder_func="maven1_build $mavenfile" ;;
			esac
		fi
	done

	# Check if we have found an appropriate java builder.
	if [ -n "$buildtype" ]; then
		# Set the selected build type.
		set_build_type $buildtype
	else
		# Auto detection of the build style resulted into
		# nothing. So from here on it is up to the package
		# to decide how to continue building the package.
		echo_status "Java buildstyle: Unknown buildstyle"
	fi
}

# Check if autodetection of the build process is required.
if [ "$auto_detect" == "on" ] ; then
	# We use a postpatch hook to determine what kind of build process
	# is needed. When we know we set the inmake hook appropriately.
	hook_add postpatch 5 determine_build_type
fi