#!/usr/bin/make -f

#export DH_VERBOSE=1

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/maven.mk

JAVA_HOME := /usr/lib/jvm/default-java
# Parse upstream version without +dfsg for source download+
#DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^+]+).*,\1,p')
DEB_MAVEN_BUILD_TARGET := install
DEB_MAVEN_ARGS := -e -Dbuild.version="$(DEB_UPSTREAM_VERSION)"
DEB_MAVEN_INSTALL_TO_USJ := false

# Fetch the versions of the dependencies bundled in jenkins.war
ACEGI_VERSION                  := $(shell dpkg-query -f '$${source:Version}' --show libacegi-security-java)
BOUNCYCASTLE_VERSION           := $(shell dpkg-query -f '$${source:Version}' --show libbcpkix-java)
FILEUPLOAD_VERSION             := $(shell dpkg-query -f '$${source:Version}' --show libcommons-fileupload-java)
HTTPCLIENT_VERSION             := $(shell dpkg-query -f '$${source:Version}' --show libcommons-httpclient-java)
SPRING_VERSION                 := $(shell dpkg-query -f '$${source:Version}' --show libspring-core-java)
STAPLER_VERSION                := $(shell dpkg-query -f '$${source:Version}' --show libstapler-java)
GUICE_VERSION                  := $(shell dpkg-query -f '$${source:Version}' --show libguice-java)
WINSTONE_VERSION               := $(shell dpkg-query -f '$${source:Version}' --show libjenkins-winstone-java)
ANT_PLUGIN_VERSION             := $(shell dpkg-query -f '$${source:Version}' --show jenkins-ant-plugin)
ANTISAMY_PLUGIN_VERSION        := $(shell dpkg-query -f '$${source:Version}' --show jenkins-antisamy-markup-formatter-plugin)
MAILER_PLUGIN_VERSION          := $(shell dpkg-query -f '$${source:Version}' --show jenkins-mailer-plugin)
MATRIX_AUTH_PLUGIN_VERSION     := $(shell dpkg-query -f '$${source:Version}' --show jenkins-matrix-auth-plugin)
MATRIX_PROJECT_PLUGIN_VERSION  := $(shell dpkg-query -f '$${source:Version}' --show jenkins-matrix-project-plugin)

DEB_DH_GENCONTROL_ARGS := -- -Vacegi:Version=${ACEGI_VERSION} \
                             -Vbouncycastle:Version=${BOUNCYCASTLE_VERSION} \
                             -Vfileupload:Version=${FILEUPLOAD_VERSION} \
                             -Vguice:Version=${GUICE_VERSION} \
                             -Vhttpclient:Version=${HTTPCLIENT_VERSION} \
                             -Vspring:Version=${SPRING_VERSION} \
                             -Vstapler:Version=${STAPLER_VERSION} \
                             -Vwinstone:Version=${WINSTONE_VERSION}
# Jenkins plugins
DEB_DH_GENCONTROL_ARGS +=    -Vant-plugin:Version=${ANT_PLUGIN_VERSION} \
                             -Vantisamy-plugin:Version=${ANTISAMY_PLUGIN_VERSION} \
                             -Vmailer-plugin:Version=${MAILER_PLUGIN_VERSION} \
                             -Vmatrix-auth-plugin:Version=${MATRIX_AUTH_PLUGIN_VERSION} \
                             -Vmatrix-project-plugin:Version=${MATRIX_PROJECT_PLUGIN_VERSION}

# Debian package version use daemon to control jenkins processes
# Ubuntu does not need this - managed by upstart
ifeq ($(shell dpkg-vendor --derives-from ubuntu || echo debian ),debian)
DEB_DH_GENCONTROL_ARGS += -Tdebian/debian_control_vars
endif

# Generate POM file for plugins
debian/plugin-debian.pom: debian/plugin-debian.pom.in
	sed -e "s|__VERSION__|$(DEB_UPSTREAM_VERSION)|g" $< > debian/plugin-debian.pom

# Install the POM for plugin-parent manually as not part
# of main build - manually installed as not managed by 
# m-d-h as breaks build elements for re-use.
binary-post-install/libjenkins-plugin-parent-java:: debian/plugin-debian.pom
	dh_install -plibjenkins-plugin-parent-java debian/plugin-debian.pom \
		usr/share/maven-repo/org/jenkins-ci/plugins/plugin/debian

# Sort out MANIFEST.MF entries in jenkins cli .jar files
# and create handy links for script wrapper
binary-post-install/jenkins-cli::
	dh_link -pjenkins-cli usr/share/jenkins/cli/java/cli-$(DEB_UPSTREAM_VERSION)-jar-with-dependencies.jar \
		usr/share/jenkins/cli/java/cli.jar
	jh_classpath -pjenkins-cli
	jh_manifest -pjenkins-cli

# Install POM files directly
binary-post-install/libjenkins-java::
	mh_installpoms -plibjenkins-java

# Tidy up any upstart configuration files lying around
clean::
	[ ! -f dummy.keystore ] || rm dummy.keystore

get-orig-source:
	uscan --download-version $(DEB_UPSTREAM_VERSION) --force-download --rename
	mv ../jenkins_$(DEB_UPSTREAM_VERSION).orig.tar.gz \
        ../jenkins_$(DEB_UPSTREAM_VERSION)+dfsg.orig.tar.gz

before-mvn-build:: dummy.keystore
	# Purge out links relating to jenkins from the copy of the maven repo
	# fixes issue with circular dependency and the need to use the install
	# maven target
	for module in jenkins-core cli pom jenkins-war; do \
		find debian/maven-repo/org/jenkins-ci/main/$${module} -type l -delete || true ;\
	done

dummy.keystore:
	keytool -genkeypair -dname "cn=Jenkins, ou=Jenkins Project, o=Debian, c=US" \
    	-alias jenkins -keypass jenkins -keystore dummy.keystore \
      	-storepass jenkins -validity 3650
