#!/usr/bin/make -f
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/ant.mk

# Version is everything before the first hyphen, taken from changelog
VERSION=$(shell dpkg-parsechangelog | grep ^Version | sed 's,^Version: \([^-]*\).*,\1,')
UPSTREAM_TGZ=http://personal.inet.fi/atk/fetchexc/fetchExc-src-$(VERSION).tar.gz

DEB_ANT_ARGS = -Dversion=$(VERSION) -Dant.build.javac.source=1.5
DEB_JARS = commons-codec commons-httpclient commons-logging commons-logging-api
DEB_ANT_BUILD_TARGET = build
DEB_ANT_INSTALL_TARGET = dist
DEB_DH_LINK_ARGS = /usr/share/java/fetchexc-$(VERSION).jar /usr/share/java/fetchexc.jar
DEB_INSTALL_CHANGELOGS_ALL = debian/changelog.upstream
DEB_INSTALL_DOCS_fetchexc = debian/README.html
DEB_INSTALL_MANPAGES_fetchexc = fetchexc.1
DEB_PHONY_RULES = get-orig-source
JAVA_HOME = /usr/lib/jvm/default-java

clean::
	rm -f fetchexc.1 fetchexc.1~

install/fetchexc::
	cp src/fetchexc debian/fetchexc/usr/bin/fetchexc

binary-install/fetchexc::
	# CDBS doesn't know that README is just a copyright file
	rm -f debian/fetchexc/usr/share/doc/fetchexc/README

DB2MAN=/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl
XP=xsltproc -''-nonet

build/fetchexc:: fetchexc.1

fetchexc.1:: debian/manpage.xml
	$(XP) $(DB2MAN) $<
	mv fetchexc.1 fetchexc.1~
	# Remove duplicate AUTHORS section and
	# Convert \240 (non-breaking space) to regular \040 space
	# (seems to be caused by docbook.xsl outputting ISO-8859-1).
	head -n-3 fetchexc.1~ | tr '\240' '\040' > $@
	rm -f fetchexc.1~

####
## All the rules below here are intended for manual invocation.
####

get-orig-source::
	dh_testdir
	# Remove any existing files that would get in the way
	rm  -rf  fetchexc-$(VERSION) fetchExc-src-$(VERSION).tar.gz
	wget     $(UPSTREAM_TGZ)
	tar -zxf fetchExc-src-$(VERSION).tar.gz
	rm  -f   fetchExc-src-$(VERSION).tar.gz
	# Remove unwanted files
	rm  -rf  fetchExc/Apache-LICENSE.txt fetchExc/include
	# Recreate archive
	mv fetchExc fetchexc-$(VERSION)
	tar -czf fetchexc_$(VERSION).orig.tar.gz fetchexc-$(VERSION)
	rm  -rf  fetchexc-$(VERSION)
