#!/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             := stax-api xml-apis
DEB_ANT_BUILD_TARGET := package
DEB_ANT_BUILDFILE    := ./debian/build.xml
DEB_ANT_ARGS         := -Dpackage=$(PACKAGE) -Dversion=$(VERSION)
CLASSPATH            := build/$(PACKAGE)-debian.jar:/usr/share/java/stax-api.jar:/usr/share/java/stax.jar:/usr/share/java/xml-apis.jar
JAVA_OPTIONS         := -Djavax.xml.stream.XMLOutputFactory=com.bea.xml.stream.XMLOutputFactoryBase

# Generation of man pages
SCRIPTS       := $(wildcard src/main/bin/mh_*)
MAN_PAGES     := $(addprefix debian/.mh/doc/,$(addsuffix .1, $(notdir $(SCRIPTS))))
mh_checkrepo_description = "Checks the Maven repository."
mh_clean_description = "Cleans the temporary files created by the other mh_* utilities."
mh_cleanpom_description = "Cleans a POM file."
mh_genrules_description = "Generates the debian/rules file with Maven support."
mh_installjar_description = "Installs a jar into /usr/share/maven-repo."
mh_installpom_description = "Installs a POM file into /usr/share/maven-repo."
mh_installpoms_description = "Installs all POMs for the package into /usr/share/maven-repo."
mh_linkjar_description = "Installs a link to the jar into /usr/share/maven-repo."
mh_linkrepojar_description = "Installs a link from the jar in /usr/share/maven-repo."
mh_lspoms_description = "Generates a list of POMs for the package."
mh_patchpom_description = "Patches one POM file using the Maven dependency rules."
mh_patchpoms_description = "Patches the POM files using the Maven dependency rules."
mh_unpatchpoms_description = "Undo the patches on the POM files."
debian/.mh/mh_lib.sh : src/main/share/mh_lib.sh
	mkdir -p debian/.mh
	cp src/main/share/mh_lib.sh debian/.mh/mh_lib.sh
debian/.mh/mh_% : src/main/bin/mh_% debian/.mh/mh_lib.sh
	mkdir -p debian/.mh
	cp $< $@
	perl -i -pe 's,\. /usr/share/maven-repo-helper/mh_lib.sh,\. debian/.mh/mh_lib.sh,' $@
	chmod +x $@
debian/.mh/doc/mh_%.1 : debian/.mh/mh_% 
	mkdir -p debian/.mh/doc
	help2man -N -n $(mh_$*_description) -o $@ $<
man-pages: $(MAN_PAGES)
build/$(PACKAGE):: man-pages

binary-post-install/$(PACKAGE)::
	mv build/$(PACKAGE)-$(VERSION).jar build/$(PACKAGE)-debian.jar
	dh_install -p$(PACKAGE) build/$(PACKAGE)-debian.jar /usr/share/maven-repo/org/debian/maven/$(PACKAGE)/debian
	dh_install -p$(PACKAGE) src/main/bin/mh_* /usr/bin
	dh_install -p$(PACKAGE) src/main/share/mh_* /usr/share/$(PACKAGE)
	dh_link -p$(PACKAGE) /usr/share/maven-repo/org/debian/maven/$(PACKAGE)/debian/$(PACKAGE)-debian.jar /usr/share/java/$(PACKAGE).jar
	dh_installman $(MAN_PAGES)
	mkdir -p debian/.mh
	java -cp $(CLASSPATH) $(JAVA_OPTIONS) org.debian.maven.repo.POMCleaner --no-parent --rules=debian/maven.rules pom.xml debian/.mh/$(PACKAGE)-debian.pom debian/.mh/pom.properties 
	dh_install -p$(PACKAGE) --sourcedir=debian/.mh $(PACKAGE)-debian.pom \
		usr/share/maven-repo/org/debian/maven/$(PACKAGE)/debian

clean::
	-rm -rf debian/.mh

