#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

JAVA_HOME=/usr/lib/jvm/default-java

CUPBOARD = biojava				\
           j2ssh-core				\
           ibatis				\
           log4j-1.2				\
           postgresql-jdbc3			\
           picard				\
           htsjdk				\
           commons-logging			\
           commons-net				\
           commons-lang3			\
           cglib	        		\
           batik-codec			        \
           batik-dom				\
           batik-ext				\
           batik-svggen				\
           batik-util

# hack to be able to use a space as an argument to the subst function
space=
space+=

CLASSPATH := $(foreach jar, $(CUPBOARD), /usr/share/java/$(jar).jar)
CLASSPATH += /usr/share/EMBOSS/jemboss/lib/jemboss.jar \
             $(CURDIR)
export CLASSPATH := $(subst $(space),:,$(strip $(CLASSPATH)))

%:
	dh $@

override_dh_auto_build:
	dh_auto_build
	# Now, 4 jars are meant to be created: they are similar to each other but
	# do not exactly have the same contents. We design this with the pom.xml file,
	# but we don't use it since it creates shaded jars.
	cd target/ && mkdir jarsBuild && mkdir modifiedJars && cd jarsBuild && \
	        jar xf ../artemis-*.jar && \
	        $(RM) -rf META_INF/ && \
	        jar cf artemis.jar * && \
	        mv artemis.jar ../modifiedJars/ && \
	        cp ../modifiedJars/artemis.jar ../modifiedJars/act.jar && \
	        $(RM) -rf artemis_sqlmap/ && \
	        jar cf dnaplotter.jar * && \
	        mv dnaplotter.jar ../modifiedJars/ && \
	        cp ../modifiedJars/dnaplotter.jar ../modifiedJars/bamview.jar
	# Preparing the classpath for jh_manifest, which will fix the classpath and
	# the main class.
	sed 's|@CLASSPATH@|$(subst :,$(space),$(subst $(CURDIR),/usr/share/java/artemis.jar,$(CLASSPATH)))|g' debian/artemis.manifest.in > \
		debian/artemis.manifest

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# EMBOSS_ROOT has to be passed to Maven as an environment variable.
	EMBOSS_ROOT="/usr" dh_auto_test
endif

override_dh_auto_clean:
	dh_auto_clean
	$(RM) debian/artemis.manifest
	$(RM) images/helix.png
	$(RM) -rf target/jarsBuild
	$(RM) -rf target/modifiedJars

override_dh_install:
	dh_install
	# Using imagemagick to convert the icon from gif to png.
	mkdir -p debian/$(DEB_SOURCE)/usr/share/icons/hicolor/96x96/apps/
	convert src/main/resources/images/helix.gif -resize 96x96\! debian/$(DEB_SOURCE)/usr/share/icons/hicolor/96x96/apps/helix.png
	# Defining the right classpaths and main classes.
	jh_manifest
