#!/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 --without-debug
proj=algo/blast/ app/ objmgr/ objtools/align_format/ objtools/blast/

ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
DEB_CONFIGURE_EXTRA_FLAGS += --with-symbols
endif

export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

CFLAGS   := $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

ifneq (,$(findstring mips,$(DEB_HOST_ARCH)))
CXXFLAGS := $(CXXFLAGS:-O%=-O)
DEB_CONFIGURE_EXTRA_FLAGS += FAST_CXXFLAGS=-O
endif

export CFLAGS CPPFLAGS CXXFLAGS LDFLAGS 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)" \
	      APP_LDFLAGS='-Wl,-E -fPIE -pie'

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/seedtop $(instroot)/bin/seedtop+
	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 \
	  if [ -d $$x ]; then \
	    (cd `dirname $$x`  &&  ../../../BUILD/build/new_module.sh \
		`basename $$x .files`.module purge_sources); \
	  fi \
	done
	rm -rf c++/BUILD c++/compilers/dll c++/config.log c++/Makefile
	rm -f c++/configure.lineno c++/src/objects/blastxml/blastxml.module

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

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