#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
#Hacky thing to allow building against tcmalloc without installing
#troublesome libbgoogle-perftools-dev, see README.Debian
export DEB_CFLAGS_MAINT_APPEND=-L$(CURDIR)/lib

pkg=mira-assembler

%:
	dh $@

override_dh_auto_install :
	make install DESTDIR=$(CURDIR)/debian/$(pkg)
	#There is no installation target for the docs
	mkdir -p $(CURDIR)/debian/tmp/usr/share/doc/$(pkg)
	cp -r -t $(CURDIR)/debian/tmp/usr/share/doc/$(pkg) doc/docbook/bookfigures doc/docbook/images \
	    doc/docbook/doccss doc/docbook/DefinitiveGuideToMIRA.html || true

override_dh_auto_test:
	make check

#Make the docs
override_dh_auto_build:
	dh_auto_build --parallel
ifneq (,$(findstring mira-doc, $(shell dh_listpackages)))
	dh_auto_build --sourcedir=doc --parallel
endif

#Remove unneeded .tcl extensions from scripts
override_dh_install:
	dh_install
	if [ -d debian/$(pkg) ] ; then \
	  for f in $$(ls debian/$(pkg)/usr/bin/*.tcl) ; do mv "$$f" "$${f%.tcl}"; done \
	fi
	# In version 3.9.17 executing miraSearchESTSNPs just leads to "De-activated atm, sorry."
	# If this is the case, it should not be installed
	for deactivated in miraSearchESTSNPs miraclip ; do \
	    if [ -e debian/$(pkg)/usr/bin/$${deactivated} ] ; then \
	        if [ "`debian/$(pkg)/usr/bin/$${deactivated} 2>&1 | tee`" = "De-activated atm, sorry." ] ; then rm -f debian/$(pkg)/usr/bin/$${deactivated} ; fi \
	    else \
	        echo "debian/$(pkg)/usr/bin/$${deactivated} does not exist" ; \
	    fi ; \
	done

#Must clean up the docs before cleaning out config.status
#Then scrub some junk not picked up by 'make clean'
override_dh_auto_clean:
	( cd doc ; make clean ) || true
	dh_auto_clean
	find * -name Makefile -exec rm '{}' ';'
	find * -name '*.xxd.H' -exec rm '{}' ';'
	find * -name '*.par.H' -exec rm '{}' ';'
	find * -name 'compileinfo.*' -exec rm '{}' ';'

override_dh_clean:
	dh_clean
	rm -rf lib

override_dh_auto_configure :
	mkdir lib
	ln -s /usr/lib/libtcmalloc_minimal.so.4 lib/libtcmalloc_minimal.so
	dh_auto_configure

override_dh_builddeb:
	dh_builddeb -- -Z xz

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

