#!/usr/bin/make -f

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

include /usr/share/dpkg/default.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --with python2

override_dh_compress:
	dh_compress \
	    --exclude=.js \
	    --exclude=.sff \
	    --exclude=.qual \
	    --exclude=.fna \
	    --exclude=.fna.txt

override_dh_auto_build:
	cd qiime/support_files/denoiser/FlowgramAlignment && make
	cd doc && make html
	ronn -r --manual=QIIME --organization='Bio-Linux' debian/qiime.1.ronn
	dh_auto_build

override_dh_auto_clean:
	#Calling "python setup.py clean -a" actually triggers a
	#rebuild, so don't do it.
	rm -f *.logfile
	rm -rf build
	rm -rf doc/_build
	rm -f debian/*.1
	( cd qiime/support_files/denoiser/FlowgramAlignment && make clean ) || true
	rm -f qiime/support_files/denoiser/bin/*
	#In case the tarball was not re-packed, remove any .jars
	find -name '*.jar' -delete
	#And in case any Python script was run, remove the .pyc
	find qiime scripts -name '*.pyc' -delete
	# Remove remainings from running the test suite
	rm -f BLAST_temp_db*
	rm -rf jobs tests/jobs
	rm -rf qiime.egg-info

export ROOTDIR=debian/$(DEB_SOURCE)

#Lots of shuffling to be done...
# Python scripts go into /usr/lib/$(DEB_SOURCE)/bin (not /usr/bin)
# Helper script goes into /usr/bin
# Setup scripts go into /usr/lib/$(DEB_SOURCE)/shell
# Default configuration goes into /etc/$(DEB_SOURCE)/default_qiime_config
# ...see debian/*.install files
override_dh_install-arch:
	dh_install -a
	chmod a+x $(ROOTDIR)/usr/lib/qiime/bin/uclust || true
	chmod a+x $(ROOTDIR)/usr/lib/qiime/bin/usearch* || true
	chmod -R a+rX $(ROOTDIR)/usr/lib/qiime/shell || true
	chmod a-x $(ROOTDIR)/usr/share/qiime/support_files/R/* || true
	chmod a-x $(ROOTDIR)/usr/share/qiime/support_files/js/* || true
	#Fix lintian warning for jquery
	rm $(ROOTDIR)/usr/share/qiime/support_files/js/jquery.js && \
	    ln -s /usr/share/javascript/jquery/jquery.js $(ROOTDIR)/usr/share/qiime/support_files/js
	#Make some symlinks as Qiime assumes a single dir structure
	for path in denoiser/Data css images R js ; do \
		dh_link -pqiime /usr/share/qiime/support_files/"$$path" /usr/lib/qiime/support_files/"$$path" ;\
	done
	#Embed the Debian revision into the version which users see.
	sed -i 's/^\(__version__ = "\)1.*"/\1'$(DEB_VERSION)'"/' debian/$(DEB_SOURCE)/usr/lib/qiime/bin/print_qiime_config.py

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog.md

get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download  --destdir=../tarballs --repack --compression xz
