#!/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
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 := $(echo $(T_VER) | cut -d'.' -f1)
T_VER_MINOR := $(echo $(T_VER) | cut -d'.' -f2)
T_VER_BUILD := $(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-stamp
build-stamp:
	dh_testdir
	$(ANT_INVOKE) deploy-webapps
	javadoc -subpackages "javax.servlet" -d "output/api" \
		-sourcepath "java" -author -version -breakiterator \
		-windowtitle "Tomcat API Documentation" -doctitle "Tomcat API" \
		-bottom "Copyright &#169; 2000-2008 Apache Software Foundation. All Rights Reserved."
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-$(ANT_INVOKE) clean
	rm -rf "output/"
	rm -f build-stamp
	dh_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 -ptomcat6-user debian/tomcat6-instance-create.1
	dh_installexamples
	dh_installinit --error-handler=true -- defaults 92 08
	dh_installcron
	mkdir -p debian/poms
	cp res/maven/*.pom debian/poms
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/2.5/' debian/poms/servlet-api.pom
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/2.1/' debian/poms/el-api.pom
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/2.1/' debian/poms/jsp-api.pom
	perl -p -i -e 's/\@MAVEN.DEPLOY.VERSION\@/$(T_VER)/' debian/poms/*.pom
	mh_installpoms -plibtomcat6-java
	for i in $(T_MAVENIZED_JARS); do \
		mh_installjar -plibtomcat6-java -l debian/poms/$$i.pom $(BLDLIB)/$$i.jar usr/share/tomcat6/lib/$$i.jar; done
	mh_installjar -plibtomcat6-java -l --usj-name=catalina-tribes debian/poms/tribes.pom \
		$(BLDLIB)/catalina-tribes.jar usr/share/tomcat6/lib/catalina-tribes.jar
	mh_installjar -plibtomcat6-java -l --usj-name=tomcat-coyote debian/poms/coyote.pom \
		$(BLDLIB)/tomcat-coyote.jar usr/share/tomcat6/lib/tomcat-coyote.jar
	mh_installjar -plibtomcat6-java -l --usj-name=tomcat-juli debian/poms/juli.pom $(BLDBIN)/tomcat-juli.jar
	for i in $(T_JARS); do \
		mv $(BLDLIB)/$$i.jar $(BLDLIB)/$$i-$(T_VER).jar && \
		dh_install -plibtomcat6-java \
			$(BLDLIB)/$$i-$(T_VER).jar usr/share/java && \
		dh_link -plibtomcat6-java usr/share/java/$$i-$(T_VER).jar \
			usr/share/java/$$i.jar && \
		dh_link -ptomcat6-common usr/share/java/$$i-$(T_VER).jar \
			usr/share/tomcat6/lib/$$i.jar; done
	dh_install --exclude=.bat --exclude=Thumbs.db
	dh_link
	mh_installpoms -plibservlet2.5-java
	mh_installjar -plibservlet2.5-java -l -s debian/poms/servlet-api.pom output/build/lib/servlet-api.jar
	mh_installjar -plibservlet2.5-java -l -s debian/poms/jsp-api.pom output/build/lib/jsp-api.jar
	mh_installjar -plibservlet2.5-java -l -s debian/poms/el-api.pom output/build/lib/el-api.jar
	rm -r debian/poms
	rm -rf debian/tomcat6/usr/share/tomcat6/webapps/default_root/.svn \
		debian/tomcat6/usr/share/tomcat6/webapps/default_root/META-INF/.svn
	chmod a+x debian/tomcat6-common/usr/share/tomcat6/bin/*.sh
	chmod a+x debian/tomcat6-user/usr/bin/tomcat6-instance-create
	chmod a+x debian/tomcat6-user/usr/share/tomcat6/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
