#!/usr/bin/make -f

# DH_VERBOSE := 1

include /usr/share/dpkg/default.mk

# Fails to build when trying to enhance hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

CMAKE_EXTRA_FLAGS += -DDEBIAN_BUILD=1 \
                     -DCONDA_BUILD=1 \
                     -DFETCHED_RAPMAP=1 \
                     -DBZIP2_LIBRARIES=-lbz2 \
                     -DBZIP2_INCLUDE_DIR=/usr/include \
                     -DLIBLZMA_INCLUDE_DIR=/usr/include/ \
                     -DLIBLZMA_LIBRARY=lzma \
                     -DLIBLZMA_HAS_AUTO_DECODER=1 \
                     -DLIBLZMA_HAS_EASY_ENCODER=1 \
                     -DLIBLZMA_HAS_LZMA_PRESET=1 \
                     -DCMAKE_MODULE_PATH=/usr/share/cmake/Modules \
                     -DTBB_WILL_RECONFIGURE=FALSE \
                     -DBOOST_WILL_RECONFIGURE=FALSE

%:
	dh $@ --with sphinxdoc

override_dh_auto_configure:
	# See here
	#    https://github.com/COMBINE-lab/salmon/issues/87
	# for a discussion with upstream why it is necessary to include rapmap source
	mkdir -p $(CURDIR)/external/install/src/rapmap
	cp -av /usr/share/rapmap/src/* $(CURDIR)/external/install/src/rapmap
	dh_auto_configure -- $(CMAKE_EXTRA_FLAGS)

override_dh_auto_build:
	dh_auto_build
	mv doc/source/license.rst doc/ # unused
	PYTHONPATH=. http_proxy='127.0.0.1:9' sphinx-build -N -bhtml doc/source doc/build/html
	mv doc/license.rst doc/source/
	cd obj*/src && \
	help2man --no-discard-stderr --no-info --name="highly-accurate, transcript-level quantification estimates from RNA-seq data"\
		./salmon > ../../debian/salmon.1 && \
	for option in index quant swim; do help2man --no-discard-stderr \
		--version-string="$(DEB_VERSION_UPSTREAM)" --no-info "./salmon --no-version-check $${option}" \
		--include=../../debian/salmon-$${option}-man-include \
		> ../../debian/salmon-$${option}.1; done

# tests may fail when run out of order or concurrently
override_dh_auto_test:
	DEB_BUILD_OPTIONS="parallel=1" dh_auto_test

override_dh_auto_clean:
	dh_auto_clean
	rm -rf external
	rm -f debian/*.1
	rm -Rf sample_data
	cd doc && $(MAKE) clean
