#!/usr/bin/make -f

export JAVA_HOME := /usr/lib/jvm/java-7-openjdk-$(shell dpkg-architecture -qDEB_BUILD_ARCH)

VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ' | sed 's/-.*//')
ANT       := ant -propertyfile debian/ant.properties \
             -Ddistribution.name=$(shell lsb_release -si)

%:
	dh $@ --with maven-repo-helper --with javahelper

override_dh_auto_build:
	$(ANT) extras-jmx-remote deploy embed-jars

	# Build the Javadoc for the Servlet, JSP, EL and WebSocket APIs
	$(JAVA_HOME)/bin/javadoc -locale en_US -subpackages "javax.servlet:javax.el:javax.websocket" -d "output/api" \
		-sourcepath "java" -author -version -breakiterator -notimestamp \
		-windowtitle "Tomcat API Documentation" -doctitle "Tomcat API" \
		-bottom "Copyright &#169; 2000-2016 The Apache Software Foundation. All Rights Reserved."

	# Prepare the Maven artifacts
	mkdir -p debian/poms
	cp res/maven/*.pom debian/poms
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/$(VERSION)/' debian/poms/*.pom

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	$(ANT) test -Dtest.apr.loc=/usr/lib/$(shell dpkg-architecture --query DEB_BUILD_MULTIARCH) -Dtest.verbose=false
endif

override_dh_installchangelogs:
	cp debian/tomcat8.NEWS debian/tomcat8-user.NEWS
	dh_installchangelogs

override_dh_install-indep:
	dh_install -i --exclude=.bat --exclude=Thumbs.db

	# update the checksum for the root webapp
	unset rwmd5sum \
		&& rwmd5sum=`cat debian/default_root/index.html debian/default_root/META-INF/context.xml | md5sum - 2>/dev/null | cut -d " " -f1` \
		&& sed "s/\@ROOT_WEBAPP_MD5SUM\@/$$rwmd5sum/" debian/tomcat8.postrm.in > debian/tomcat8.postrm

override_dh_auto_clean:
	dh_auto_clean
	-$(ANT) clean
	rm -Rf output debian/poms

get-orig-source:
	-uscan --force-download --rename
