#!/usr/bin/make -f

JAVA_HOME := /usr/lib/jvm/default-java
JAVA_CMD := $(JAVA_HOME)/bin/java
BLDLIB := output/build/lib
BLDBIN := output/build/bin
DEB_JARS_BASE := /usr/share/java
DEB_JARS := ant ant-launcher ant-trax ant-nodeps
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)

T_VER := $(shell dpkg-parsechangelog | egrep '^Version:' \
	| cut -f 2 -d ' ' | cut -f 2 -d ' '|sed 's/-[^-]*$$//')
T_JARS := tomcat-i18n-fr tomcat-i18n-es tomcat-i18n-ja catalina-ant
T_MAVENIZED_JARS := jasper-el annotations-api jasper catalina-ha catalina 
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)

ANT_ARGS := -Dcompile.debug=true \
	-Dant.build.javac.source=1.5 \
	-Dant.build.javac.target=1.5 \
	-Djdt.jar=/usr/share/java/ecj.jar \
	-Dversion=$(T_VER) \
    -Dversion.major="$(T_VER_MAJOR)" \
    -Dversion.minor="$(T_VER_MINOR)" \
    -Dversion.build="$(T_VER_BUILD)" \
    -Dversion.patch="0" \
    -Dversion.suffix="" 

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	ANT_ARGS += -Dcompile.optimize=false
else
	ANT_ARGS += -Dcompile.optimize=true
endif

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
	$(ANT_INVOKE) deploy
	javadoc -subpackages "javax.servlet" -d "output/api" \
		-sourcepath "java" -author -version -breakiterator \
		-windowtitle "Tomcat API Documentation" -doctitle "Tomcat API" \
		-bottom "Copyright &#169; 2000-2011 Apache Software Foundation. All Rights Reserved."
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-$(ANT_INVOKE) clean
	rm -rf "output/"
	rm -f build-stamp modules/jdbc-pool/output/resources/MANIFEST.MF
	dh_clean
	mh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep 
	dh_installdirs
	cp debian/logrotate debian/tomcat7/etc/logrotate.d/tomcat7

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installman -ptomcat7-user debian/tomcat7-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
	rm -f debian/poms/tomcat-dbcp.pom
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/3.0/'      \
	              debian/poms/tomcat-servlet-api.pom
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/2.2/'      \
	              debian/poms/tomcat-el-api.pom
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/2.2/'      \
	              debian/poms/tomcat-jsp-api.pom
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/$(T_VER)/' \
	              debian/poms/*.pom
	mh_installpoms -plibtomcat7-java
	for i in $(T_MAVENIZED_JARS); \
	do \
		mh_installjar -plibtomcat7-java -l           \
	                      debian/poms/tomcat-$$i.pom     \
	                      $(BLDLIB)/$$i.jar              \
	                      usr/share/tomcat7/lib/$$i.jar; \
	done
	mh_installjar -plibtomcat7-java -l --usj-name=catalina-tribes \
	              debian/poms/tomcat-tribes.pom \
	              $(BLDLIB)/catalina-tribes.jar \
	              usr/share/tomcat7/lib/catalina-tribes.jar
	mh_installjar -plibtomcat7-java -l --usj-name=tomcat-api \
	              debian/poms/tomcat-api.pom \
	              $(BLDLIB)/tomcat-api.jar \
	              usr/share/tomcat7/lib/tomcat-api.jar
	mh_installjar -plibtomcat7-java -l --usj-name=tomcat-coyote \
	              debian/poms/tomcat-coyote.pom \
	              $(BLDLIB)/tomcat-coyote.jar \
	              usr/share/tomcat7/lib/tomcat-coyote.jar
	mh_installjar -plibtomcat7-java -l --usj-name=tomcat-juli \
	              debian/poms/tomcat-juli.pom \
	              $(BLDBIN)/tomcat-juli.jar
	mh_installjar -plibtomcat7-java -l --usj-name=tomcat-util \
	              debian/poms/tomcat-util.pom \
	              $(BLDLIB)/tomcat-util.jar \
	              usr/share/tomcat7/lib/tomcat-util.jar
	for i in $(T_JARS); do \
		mv $(BLDLIB)/$$i.jar $(BLDLIB)/$$i-$(T_VER).jar && \
		dh_install -plibtomcat7-java \
			$(BLDLIB)/$$i-$(T_VER).jar usr/share/java && \
		dh_link -plibtomcat7-java usr/share/java/$$i-$(T_VER).jar \
			usr/share/java/$$i.jar && \
		dh_link -ptomcat7-common usr/share/java/$$i-$(T_VER).jar \
			usr/share/tomcat7/lib/$$i.jar; done
	dh_install --exclude=.bat --exclude=Thumbs.db
	dh_link
	mh_installpoms -plibservlet3.0-java
	mh_installjar -plibservlet3.0-java -l -s         \
	              debian/poms/tomcat-servlet-api.pom \
	              output/build/lib/servlet-api.jar
	mh_installjar -plibservlet3.0-java -l -s         \
	              debian/poms/tomcat-jsp-api.pom     \
	              output/build/lib/jsp-api.jar
	mh_installjar -plibservlet3.0-java -l -s         \
                      debian/poms/tomcat-el-api.pom      \
                      output/build/lib/el-api.jar
	rm -r debian/poms
	rm -rf \
           debian/tomcat7/usr/share/tomcat7/webapps/default_root/.svn \
	   debian/tomcat7/usr/share/tomcat7/webapps/default_root/META-INF/.svn
	chmod a+x debian/tomcat7-common/usr/share/tomcat7/bin/*.sh
	chmod a+x debian/tomcat7-user/usr/bin/tomcat7-instance-create
	chmod a+x debian/tomcat7-user/usr/share/tomcat7/skel/bin/*.sh
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: build install

binary: binary-indep binary-arch

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

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