#!/usr/bin/make -f

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/ant.mk

PACKAGE              := $(DEB_SOURCE_PACKAGE)
VERSION              := $(DEB_UPSTREAM_VERSION)
JAVA_HOME            := /usr/lib/jvm/default-java
DEB_JARS             := ant-nodeps
DEB_ANT_BUILD_TARGET := package javadoc
DEB_ANT_BUILDFILE    := debian/build.xml
DEB_ANT_ARGS         := -Dpackage=$(PACKAGE) -Dversion=$(VERSION)
API_DOCS             := build/api
CORE_LIBS            := core book logging-api sink-api
MODULES              := apt confluence docbook-simple fml fo itext latex rtf twiki xdoc xhtml

get-orig-source:
	-uscan --download-version $(VERSION) --force-download --rename

makebuilddir/lib$(PACKAGE)-java::
	set -e; for COMPONENT in doxia-core doxia-book $(addprefix doxia-modules/doxia-module-, $(MODULES)); do \
		COMPONENT_FILE=debian/$$(echo $$COMPONENT | cut -d'/' -f2 -)-components.xml; \
		if [ -e $$COMPONENT_FILE ]; then \
			install -d $$COMPONENT/build/classes/META-INF/plexus; \
			install -m644 -T $$COMPONENT_FILE $$COMPONENT/build/classes/META-INF/plexus/components.xml; \
		fi \
	done

binary-post-install/lib$(PACKAGE)-java::
	mh_installpoms -plib$(PACKAGE)-java
	set -e; for LIB in $(CORE_LIBS); do \
	  mh_installjar -plib$(PACKAGE)-java -l --usj-name=$(PACKAGE)-$$LIB $(PACKAGE)-$$LIB/pom.xml $(PACKAGE)-$$LIB/build/$(PACKAGE)-$$LIB-$(VERSION).jar; \
	done
	set -e; for MODULE in $(MODULES); do \
	  mh_installjar -plib$(PACKAGE)-java -l --usj-name=$(PACKAGE)-module-$$MODULE $(PACKAGE)-modules/doxia-module-$$MODULE/pom.xml $(PACKAGE)-modules/doxia-module-$$MODULE/build/$(PACKAGE)-module-$$MODULE-$(VERSION).jar; \
	done

binary-post-install/lib$(PACKAGE)-java-doc::
	dh_install -plib$(PACKAGE)-java-doc $(API_DOCS) usr/share/doc/lib$(PACKAGE)-java

clean::
	-rm -rf debian/tmp

# Extract plexus components.xml files generated from a standard Maven build
PLEXUS_COMPONENTS=$(shell find . -name components.xml | grep "target/classes" | sed -re "s,.*/([a-z-]+)/target/.*,debian/\1-components.xml,")

debian/%-components.xml:
	# In $*
	find . -type f -path *$*/target/classes/META-INF/plexus/components.xml -exec cp '{}' $@ \;
	if [ -e $*/target/classes/META-INF/plexus/components.xml ]; then \
		cp $*/target/classes/META-INF/plexus/components.xml $@; \
	fi

copy-plexus-components: $(PLEXUS_COMPONENTS)
