#!/usr/bin/make -f

JAVA_HOME := /usr/lib/jvm/default-java
JAVA_CMD := $(JAVA_HOME)/bin/java
BLDLIB := output/build/lib
DEB_JARS_BASE := /usr/share/java
DEB_JARS := ant ant-launcher ant-trax ant-nodeps ant-junit4 ant-junit junit4 hamcrest-1.3
DEB_CLASSPATH = $(shell for jar in $(DEB_JARS); do \
	if [ -f "$$jar" ]; then echo -n "$${jar}:"; fi; \
	if [ -f "$$jar".jar ]; then echo -n "$${jar}.jar:"; fi; \
	if [ -f $(DEB_JARS_BASE)/"$$jar" ]; then \
		echo -n "$(DEB_JARS_BASE)/$${jar}:"; fi; \
	if [ -f $(DEB_JARS_BASE)/"$$jar".jar ]; then \
		echo -n "$(DEB_JARS_BASE)/$${jar}.jar:"; fi; \
	done; \
	if [ -f "$(JAVA_HOME)/lib/tools.jar" ]; then \
		echo -n "$(JAVA_HOME)/lib/tools.jar"; fi)

# Upstream version
T_VER := $(shell dpkg-parsechangelog | egrep '^Version:' \
	| cut -f 2 -d ' ' | cut -f 2 -d ' ' | sed 's/-[^-]*$$//' | sed 's/~/-/')

T_VER_MAJOR := $(shell echo $(T_VER) | cut -d'.' -f1)
T_VER_MINOR := $(shell echo $(T_VER) | cut -d'.' -f2)
T_VER_BUILD := $(shell echo $(T_VER) | cut -d'.' -f3)

# root webapp file locations (during the build)
RWLOC := debian/default_root
RWFILES := $(RWLOC)/index.html $(RWLOC)/META-INF/context.xml

# Add distribution.name as system property to grab it when showing version
TOMCAT_DISTRIBUTION := "$(shell lsb_release -si)"

ANT_ARGS := -Dcompile.debug=true \
	-Djdt.jar=/usr/share/java/ecj.jar \
	-Djunit.jar=/usr/share/java/junit4.jar \
	-Dcglib.jar=/usr/share/java/cglib3-nodep.jar \
	-Deasymock.jar=/usr/share/java/easymock.jar \
	-Dhamcrest.jar=/usr/share/java/hamcrest-core.jar \
	-Dobjenesis.jar=/usr/share/java/objenesis.jar \
	-Dversion=$(T_VER) \
	-Dversion.major="$(T_VER_MAJOR)" \
	-Dversion.minor="$(T_VER_MINOR)" \
	-Dversion.build="$(T_VER_BUILD)" \
	-Dversion.patch="0" \
        -Ddistribution.name=$(TOMCAT_DISTRIBUTION) \
	-Dversion.suffix=""

ANT_INVOKE := $(JAVA_CMD) -classpath "$(DEB_CLASSPATH)" org.apache.tools.ant.Main $(ANT_ARGS)

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	dh_testdir
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	mkdir -p webapps/examples/WEB-INF/lib
	cp /usr/share/java/jstl1.1.jar webapps/examples/WEB-INF/lib/jstl.jar
	cp /usr/share/java/standard.jar webapps/examples/WEB-INF/lib/standard.jar
	$(ANT_INVOKE) deploy test
else
	$(ANT_INVOKE) deploy
endif
	$(JAVA_HOME)/bin/javadoc -subpackages "javax.servlet:javax.el:javax.websocket" -d "output/api" \
		-sourcepath "java" -author -version -breakiterator \
		-windowtitle "Tomcat API Documentation" -doctitle "Tomcat API" \
		-bottom "Copyright &#169; 2000-2014 The Apache Software Foundation. All Rights Reserved."
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-$(ANT_INVOKE) clean
	rm -rf "output/"
	rm -rf webapps/examples/WEB-INF/lib/*.jar
	rm -f build-stamp modules/jdbc-pool/output/resources/MANIFEST.MF
	rm -f debian/tomcat8.postrm
	dh_clean
	mh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep 
	dh_installdirs

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installman -ptomcat8-user debian/tomcat8-instance-create.1
	dh_installexamples
	dh_installinit --error-handler=true -- defaults 92 08
	dh_installdebconf
	dh_installcron
	
	mkdir -p debian/poms
	cp res/maven/*.pom debian/poms
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/$(T_VER)/' debian/poms/*.pom
	
	mh_install -plibservlet3.1-java
	mh_install -plibtomcat8-java
	
	# Install catalina-storeconfig.jar (no pom available)
	mv $(BLDLIB)/catalina-storeconfig.jar $(BLDLIB)/tomcat8-catalina-storeconfig-$(T_VER).jar
	dh_install -plibtomcat8-java $(BLDLIB)/tomcat8-catalina-storeconfig-$(T_VER).jar      usr/share/java
	dh_link -plibtomcat8-java    usr/share/java/tomcat8-catalina-storeconfig-$(T_VER).jar usr/share/java/tomcat8-catalina-storeconfig.jar
	
	dh_install --exclude=.bat --exclude=Thumbs.db
	dh_link
	
	rm -r debian/poms
	rm -rf \
           debian/tomcat8/usr/share/tomcat8/webapps/default_root/.svn \
	   debian/tomcat8/usr/share/tomcat8/webapps/default_root/META-INF/.svn
	chmod a+x debian/tomcat8-common/usr/share/tomcat8/bin/*.sh
	chmod a+x debian/tomcat8-user/usr/bin/tomcat8-instance-create
	chmod a+x debian/tomcat8-user/usr/share/tomcat8/skel/bin/*.sh
	
	# update the checksum for the root webapp
	unset rwmd5sum \
		&& rwmd5sum=`cat $(RWFILES) | md5sum - 2>/dev/null | cut -d " " -f1` \
		&& sed "s/\@ROOT_WEBAPP_MD5SUM\@/$$rwmd5sum/" debian/tomcat8.postrm.in > debian/tomcat8.postrm
	
	jh_manifest
	dh_compress
	dh_fixperms
	dh_lintian
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: build install

binary: binary-indep binary-arch

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

.PHONY: build clean binary-indep binary-arch binary install get-orig-source
