#!/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
export DEBIAN_MAINTAINER=$(dpkg-parsechangelog -SMaintainer)

pkg=mira-assembler
exampledir=usr/share/doc/mira-examples/examples/minidemo/data/bbdataset1/

include /usr/share/dpkg/default.mk

%:
	dh $@ --with autoreconf

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_installexamples:
	dh_installexamples
	# avoid duplicated files
	if cmp $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.fasta.qual $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.sanger.fasta.qual >/dev/null ; then \
	    echo "removing copy of $(exampledir)/cjejuni_demo_in.fasta.qual and create symling" ; \
	    rm -f $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.sanger.fasta.qual ; \
	    ln -s cjejuni_demo_in.fasta.qual.gz $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.sanger.fasta.qual.gz ; \
	fi
	if cmp $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.fasta $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.sanger.fasta >/dev/null ; then \
	    echo "removing copy of $(exampledir)/cjejuni_demo_in.fasta and create symling" ; \
	    rm -f $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.sanger.fasta ; \
	    ln -s cjejuni_demo_in.fasta.gz $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_in.sanger.fasta.gz ; \
	fi
	if cmp $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_NCTC11168_first40kb.gbf $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_backbone_in.gbf >/dev/null ; then \
	    echo "removing copy of $(exampledir)/cjejuni_NCTC11168_first40kb.gbf and create symling" ; \
	    rm -f $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_backbone_in.gbf ; \
	    ln -s cjejuni_NCTC11168_first40kb.gbf.gz $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_backbone_in.gbf.gz ; \
	fi
	if cmp $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_traceinfo_in.xml $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_traceinfo_in.sanger.xml >/dev/null ; then \
	    echo "removing copy of $(exampledir)/cjejuni_demo_traceinfo_in.xml and create symling" ; \
	    rm -f $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_traceinfo_in.sanger.xml ; \
	    ln -s cjejuni_demo_traceinfo_in.xml.gz $(CURDIR)/debian/mira-examples/$(exampledir)/cjejuni_demo_traceinfo_in.sanger.xml.gz ; \
	fi

override_dh_auto_test:
	make check

#Make the docs
override_dh_auto_build:
	# some *.cc files are created by old flex version which breaks the build
	# see bug #812681
	find -name '*.ll' | sed -e 's/[.]ll$$/.cc/' | xargs rm
	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
	dh_auto_configure -- --with-boost-libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_builddeb:
	dh_builddeb -- -Z xz

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

