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

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

DEB_CONFIGURE_EXTRA_FLAGS=--with-dll --with-mt --without-autodep \
    --without-makefile-auto-update --with-flat-makefile --without-caution \
    --without-dbapi --without-lzo --with-runpath=/usr/lib/ncbi-blast+ \
    --with-build-root=BUILD LDFLAGS='-Wl,--as-needed -Wl,--enable-new-dtags'
proj=algo/blast/ app/ objmgr/ objtools/align_format/ objtools/blast/

# XXX - not quite right, as we get -DNDEBUG vs. -D_DEBUG
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
DEB_CONFIGURE_EXTRA_FLAGS += --without-debug
else
DEB_CONFIGURE_EXTRA_FLAGS += --with-optimization
endif

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifneq (,$(findstring mips,$(DEB_HOST_ARCH)))
DEB_CONFIGURE_EXTRA_FLAGS += CXXFLAGS=-O FAST_CXXFLAGS=-O
endif

export MAKE

llp=LD_LIBRARY_PATH
override_dh_auto_configure:
	cd c++  &&  $(llp)=$(CURDIR)/c++/BUILD/lib$${$(llp)+:$$$(llp)} \
	    CONFIG_SHELL=/bin/bash ./configure $(DEB_CONFIGURE_EXTRA_FLAGS)

override_dh_auto_build:
	cd c++/BUILD/build  &&  make -f Makefile.flat all_projects="$(proj)"

override_dh_auto_test:
	-dh_auto_test
	-c++/BUILD/build/check.sh concat_err
	-cat c++/BUILD/build/check.sh.out_err

instroot = debian/ncbi-blast+/usr
leg_bin = debian/ncbi-blast+-legacy/usr/share/ncbi-blast+/bin
override_dh_auto_install:
	cp c++/BUILD/lib/*.so $(instroot)/lib/ncbi-blast+/
	cp c++/BUILD/bin/*    $(instroot)/bin/

override_dh_install:
	# dh_install
	mv $(instroot)/bin/rpsblast $(instroot)/bin/rpsblast+
	mv $(instroot)/bin/legacy_blast.pl   $(instroot)/bin/legacy_blast
	mv $(instroot)/bin/update_blastdb.pl $(instroot)/bin/update_blastdb
	mv $(instroot)/bin/windowmasker_2.2.22_adapter.py \
	   $(instroot)/bin/windowmasker_2.2.22_adapter
# Clean up tests, demos, and internal build tools
	rm -f $(instroot)/bin/*test* $(instroot)/bin/seqdb_demo \
	    $(instroot)/bin/datatool \
	    $(instroot)/bin/project_tree_builder \
	    $(instroot)/lib/ncbi-blast+/libtest_*.so

	if test -d $(leg_bin); then cp debian/legacy/legacy.sh $(leg_bin)/; fi

override_dh_clean:
	dh_clean
	-for x in c++/src/objects/*/*.files; do \
	    (cd `dirname $$x`  &&  ../../../BUILD/build/new_module.sh \
		`basename $$x .files`.module purge_sources); \
	done
	rm -rf c++/BUILD c++/compilers/dll c++/config.log c++/Makefile
	rm -f c++/src/objects/blastxml/blastxml.module

%:
	dh $@ -Dc++ --with autotools_dev --with quilt

get-orig-source:
	. debian/get-orig-source
