#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# include series of variables to become more version-independent
# in this debian/rules file.
include /usr/share/cdbs/1/rules/buildvars.mk

export JAVA_HOME := $(shell /usr/sbin/update-java-alternatives -l | head -1 | cut -d' ' -f 3)

# version are the first two decimals
SOVERSION=$(shell echo $(DEB_UPSTREAM_VERSION) | cut -f1,2 -d. )

build: build-stamp

build-stamp:
	dh_testdir
	find . -name "*.java" -o -name ReadMe -o -name "*.fa" -o -name "*.html" | xargs -r chmod 644 
	# circumvent Debian's difficulties with libjgrapht - deleting all usage of that library
	rm -rf src/org/biojavax/bio/phylo/io/nexus/ \
	       src/org/biojavax/bio/phylo/DistanceBasedTreeMethod.java \
	       src/org/biojavax/bio/phylo/ParsimonyTreeMethod.java

	# Add here commands to compile the package.
	ANT_OPTS="-Xms1024m -Xmx1024m" ant package-biojava
	#ant compile-demos
	ant javadocs-biojava

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	ant clean

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	cp ant-build/biojava.jar $(CURDIR)/debian/libbiojava$(SOVERSION)-java/usr/share/java/biojava$(DEB_UPSTREAM_VERSION).jar
	d=$(CURDIR)/debian/libbiojava-java-doc/usr/share/doc/libbiojava-java/api; \
	[ -d $$d ] || mkdir -p $$d ; \
	cp -r ant-build/docs/biojava/* $$d
	for i in $(CURDIR)/debian/libbiojava-java*; do \
		if [ -d "$i" ]; then \
			find $i -name ".svn" | xargs -r rm -r ;	 \
		fi ; \
	done

	ant runtests


binary-arch:
# We have nothing to do by default.

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples -plibbiojava-java-demos demos/*
	#dh_installman
	dh_link -p libbiojava-java /usr/share/java/biojava$(DEB_UPSTREAM_VERSION).jar /usr/share/java/biojava.jar
	# thinking about svn snapshots
	if [ "$(SOVERSION)" != "$(DEB_UPSTREAM_VERSION)" ]; then \
		dh_link -p libbiojava$(SOVERSION)-java \
			   /usr/share/java/biojava$(DEB_UPSTREAM_VERSION).jar \
			   /usr/share/java/biojava$(SOVERSION).jar; \
	fi
	dh_link # use <pkg>.link files for version independant linking
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

get-orig-source:
	version=$(DEB_UPSTREAM_VERSION).svn.`date +"%Y%m%d"|tr -d '\n'` ; \
	targetdir=biojava-live-$$version ; \
	origtargz=biojava-live_$$version.orig.tar.gz ; \
	cd .. ; \
	svn co svn://code.open-bio.org/biojava/biojava-live/trunk $$targetdir ; \
	find $$targetdir -name ".svn" | xargs -r rm -r ; \
	find $$targetdir -name "*.jar" | xargs -r rm  ; \
	tar czf $$origtargz $$targetdir ; \
	rm -rf $$targetdir

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