#!/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 classworlds doxia-core doxia-module-itext doxia-module-xhtml \
  doxia-sink-api doxia-logging-api doxia-module-fo itext1 plexus-classworlds plexus-container-default \
  plexus-i18n plexus-utils plexus-interpolation plexus-velocity velocity xalan2 \
  google-collections xbean-reflect fop
DEB_ANT_BUILD_TARGET := package
DEB_ANT_BUILDFILE    := debian/build.xml
DEB_ANT_ARGS         := -Dpackage=$(PACKAGE) -Dversion=$(VERSION)

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

makebuilddir/lib$(PACKAGE)-java::
	set -e; for COMPONENT in doxia-decoration-model doxia-site-renderer; 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
	mh_installjar -plib$(PACKAGE)-java -l doxia-doc-renderer/pom.xml doxia-doc-renderer/build/doxia-doc-renderer-$(VERSION).jar
	mh_installjar -plib$(PACKAGE)-java -l doxia-site-renderer/pom.xml doxia-site-renderer/build/doxia-site-renderer-$(VERSION).jar
	mh_installjar -plib$(PACKAGE)-java -l doxia-decoration-model/pom.xml doxia-decoration-model/build/doxia-decoration-model-$(VERSION).jar

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)
