#!/usr/bin/make -f
# Based on the initial work of Joey Hess and Craig Small.

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

CMAKE=/usr/bin/cmake

DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_MULTIARCH	?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

DEBIAN_DIR = $(CURDIR)/debian

BUILD_DIR = $(DEBIAN_DIR)/build

# Path to the tools subdirectory in the top source dir to build the
# documentation.
export PATH := $(BUILD_DIR)/bin:$(CURDIR)/tools:$(PATH)
export LD_LIBRARY_PATH := $(BUILD_DIR)/lib/:$(LD_LIBRARY_PATH)

OPENMS_LIB_PACKAGE = libopenms1.11
OPENMS_LIB_VERSION = 1.11.1

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CXXFLAGS += -O0
else
	CXXFLAGS += -O2
endif

# Commented out to check if the parallel build mode triggers "race
# conditions" with the linker trying to link an incomplete set of
# object files.

# Although the .NOTPARALLEL: special make target below should do
# enough by itself.

# ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#          NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#          MAKEFLAGS += -j$(NUMJOBS)
# endif

$(info "---------------------------------------")
$(info NUMJOBS: $(NUMJOBS))
$(info MAKEFLAGS: $(MAKEFLAGS))
$(info DEB_BUILD_OPTIONS: $(DEB_BUILD_OPTIONS))
$(info "---------------------------------------")

# Hardening stuff, see http://wiki.debian.org/Hardening
# export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

.NOTPARALLEL: 

.PHONY: clean
clean: 
	dh_testdir
	dh_testroot

	dh_clean

	rm -rf $(BUILD_DIR)

# Remove the generated xpm icon files:
	rm -f debian/TOPPAS.xpm
	rm -f debian/TOPPView.xpm

# Remove the generated topp.1 man page:
	rm -f debian/topp.1

build-arch-stamp:
	dh_testdir
	dh_prep -a

	mkdir -p $(BUILD_DIR)

# On the command line, use 
# -DCONTRIB_LIB_DIR="/usr/lib/$(dpkg-architecture -qDEB_BUILD_GNU_TYPE);/usr/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH);" 
# see debian/changelog for the rationale.

	@echo "DEB_BUILD_GNU_TYPE: $(DEB_BUILD_GNU_TYPE)"
	@echo "DEB_HOST_MULTIARCH: $(DEB_HOST_MULTIARCH)"

	cd $(BUILD_DIR) && \
	 $(CMAKE) -DCMAKE_FIND_LIBRARY_SUFFIXES=".so" \
	 -DCONTRIB_LIB_DIR="/usr/lib;/usr/lib/$(DEB_BUILD_GNU_TYPE);/lib/$(DEB_BUILD_GNU_TYPE);/usr/lib/$(DEB_HOST_MULTIARCH);/lib/$(DEB_HOST_MULTIARCH)" \
	 -DCF_OPENMS_DATA_PATH=/usr/share/openms-common/OpenMS/ \
	 -DCF_OPENMS_DOC_PATH=/usr/share/doc/openms-doc/ \
	 -DCMAKE_BUILD_TYPE=release \
	 -DDEBIAN_BUILD=ON -DBOOST_USE_STATIC=OFF ../.. 

# Build the libs and executable binaries
	VERBOSE=1 $(MAKE) -C $(BUILD_DIR) \
	OpenMS TOPP UTILS GUI test_build Tutorials_build 

# Prepare the xpm-formatted pixmaps for the Debian menu system.
# Setting imagemagick to Build-Depends, for use of convert.
	convert source/VISUAL/ICONS/TOPPAS.png -resize 32x32 debian/TOPPAS.xpm
	convert source/VISUAL/ICONS/TOPPView.png -resize 32x32 debian/TOPPView.xpm

# Prepare the topp.1 man page with all the one-line description got
# from calling individually each program in $(BUILD_DIR)/bin with
# --help switch. The newly built libs need be in the path so that the
# binaries can execute. Same for the shared data path, because the
# binaries need it to run. Note the calling in sh ./ with the script
# shell being itself executable, otherwise git-buildpackage would
# fail.
	@echo $(shell pwd)
	cd $(DEBIAN_DIR) && \
	OPENMS_DATA_PATH=$(CURDIR)/share/OpenMS/ \
	LD_LIBRARY_PATH=$(BUILD_DIR)/lib/:$(LD_LIBRARY_PATH)\
	sh ./binaries-extract-one-line-man-desc.sh

# Build the documentation, which needs a running XWindow server. We
# thus use the doc_minimal target.

	cd $(BUILD_DIR) && \
	 $(CMAKE) -DCMAKE_FIND_LIBRARY_SUFFIXES=".so" \
	 -DCONTRIB_LIB_DIR="/usr/lib;/usr/lib/$(DEB_BUILD_GNU_TYPE);/lib/$(DEB_BUILD_GNU_TYPE);/usr/lib/$(DEB_HOST_MULTIARCH);/lib/$(DEB_HOST_MULTIARCH)" \
	 -DCF_OPENMS_DATA_PATH=/usr/share/openms-common/OpenMS/ \
	 -DCF_OPENMS_DOC_PATH=/usr/share/doc/openms-doc/ \
	 -DCMAKE_BUILD_TYPE=release \
	 -DDEBIAN_BUILD=ON -DBOOST_USE_STATIC=OFF ../.. 

	VERBOSE=1 LD_LIBRARY_PATH=$(BUILD_DIR)/lib/:$(LD_LIBRARY_PATH)\
	 PATH=$(BUILD_DIR)/bin:$(CURDIR)/tools:$(PATH)\
	 OPENMS_DATA_PATH=$(CURDIR)/share/OpenMS/ \
	 $(MAKE) -C $(BUILD_DIR) doc_minimal doc_tutorials 

        # There are two files that we do not want to ship because
        # they exist in their own Debian packages.

	rm -f $(BUILD_DIR)/doc/html/jquery.js

	touch build-arch-stamp


#  Note that this requires a build of the binary arch stuff.
build-indep-stamp: build-arch-stamp

	touch build-indep-stamp


.PHONY: build
build: 	build-arch-stamp build-indep-stamp


.PHONY: build-indep
build-indep: build-indep-stamp


.PHONY: build-arch
build-arch: build-arch-stamp


# Build architecture-independent files here.
.PHONY: binary-indep
binary-indep: build-indep
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i CHANGELOG 
	dh_installdocs -i
	dh_install -i
	dh_installman -i
	dh_compress -XOpenMS_tutorial.pdf -XTOPP_tutorial.pdf -i
	dh_fixperms -i
	dh_installdeb -i
	dh_lintian -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i


# Build architecture-dependent files here.
.PHONY: binary-arch
binary-arch: build-arch
	dh_testdir -a
	dh_testroot -a
	dh_link
	dh_installchangelogs -a CHANGELOG
	dh_installdocs -a
	dh_install -a
# Now that install has been performed, remove all the Tutorial*
# binaries that will not ship in the topp package, but in the
# openms-doc package.
	rm -vf $(DEBIAN_DIR)/topp/usr/bin/Tutorial*
	dh_installmenu -a
	dh_installman -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a

# Generate the symbols for libs so that the call to dh_shlibdeps -a
# can work finding the dependencies upon
# $(OPENMS_LIB_PACKAGE). Specify the version (like x.yy.z) of the lib
# because otherwise, dpkg-gensymbols provides the full version with
# debian package version (x.yy.z-1), which we do not want.
	dpkg-gensymbols -P$(DEBIAN_DIR) -p$(OPENMS_LIB_PACKAGE) \
	-v$(OPENMS_LIB_VERSION) -c2 -d \
	-e$(BUILD_DIR)/lib/libOpenMS.so.$(OPENMS_LIB_VERSION) \
	-e$(BUILD_DIR)/lib/libOpenMS.so.$(OPENMS_LIB_VERSION) \
	-e$(BUILD_DIR)/lib/libOpenSwathAlgo.so.$(OPENMS_LIB_VERSION) \
	-O$(DEBIAN_DIR)/$(OPENMS_LIB_PACKAGE)/DEBIAN/symbols

# Send the proper shared libs details for the library:
	echo "libOpenMS 1.11.1 libopenms1.11 (>= 1.11.1-1)" > \
	$(DEBIAN_DIR)/$(OPENMS_LIB_PACKAGE)/DEBIAN/shlibs
	echo "libOpenMS_GUI 1.11.1 libopenms1.11 (>= 1.11.1-1)" >> \
	$(DEBIAN_DIR)/$(OPENMS_LIB_PACKAGE)/DEBIAN/shlibs
	echo "libOpenSwathAlgo 1.11.1 libopenms1.11 (>= 1.11.1-1)" >> \
	$(DEBIAN_DIR)/$(OPENMS_LIB_PACKAGE)/DEBIAN/shlibs

	dh_lintian -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


.PHONY: binary
binary: binary-arch binary-indep
