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

# There are patches in the source package
# include /usr/share/quilt/quilt.make

# 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)

DEBIAN_DIR = $(CURDIR)/debian
BUILD_DIR = $(DEBIAN_DIR)/build
INSTALL_DIR = $(BUILD_DIR)

# 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)

PROTEOWIZ_LIB_PACKAGE = libpwiz3
PROTEOWIZ_LIB_VERSION = 3.0.0

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

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: 
# unpatch
	dh_testdir
	dh_testroot

	dh_autoreconf_clean
	dh_clean

	rm -f debian/msconvert.1
	rm -f debian/idconvert.1
	rm -f debian/mscat.1
	rm -f debian/txt2mzml.1

	rm -rf $(BUILD_DIR)

# Remove the generated includes directory:
	rm -rf debian/includes

# Remove the generated doc/dox/html directory:
	rm -rf doc/dox/html

build-arch-stamp: $(QUILT_STAMPFN)
	dh_testdir
	dh_prep -a
	dh_autoreconf

	mkdir -p $(BUILD_DIR)

	# Duplicate the source tree, since it is not easy to build the
	# software in a separate build directory.
	cp -rpf autotools libraries pwiz pwiz_tools $(BUILD_DIR)

	cd $(BUILD_DIR) && autotools/configure --prefix=/usr && \
	VERBOSE=1 $(MAKE) && \
	VERBOSE=1 DESTDIR=$(INSTALL_DIR) $(MAKE) install

	# Now that the build is fine and the built files installed,
	# remove all the RPATH stuff from these binaries.
	@echo "Removing the RPATH datum from the binaries." 
	sh debian/remove-rpath-from-binaries.sh

# Prepare a mirroring hierarchy for all the header files of the
# project. The mirror is in debian/includes/libpwiz.
	@echo $(shell pwd)
	sh debian/make-included-headers-hierarchy.sh

# Produce the man pages for both libpwiz-tools binaries
	docbook-to-man debian/msconvert.sgml > debian/msconvert.1
	docbook-to-man debian/idconvert.sgml > debian/idconvert.1
	docbook-to-man debian/mscat.sgml > debian/mscat.1
	docbook-to-man debian/txt2mzml.sgml > debian/txt2mzml.1

	touch build-arch-stamp

# Build the documentation with doxygen in doc/dox. That creates an
# html file. Note that we need the binary-arch build because the files
# that go in the documentation are produced by the arch targets.
build-indep-stamp: build-arch-stamp

	cd doc/dox && doxygen

	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
	dh_installdocs -i
	dh_install -i
	dh_installman -i
	dh_compress -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
	dh_installdocs -a
	dh_install -a
	dh_installmenu -a
	dh_installman -a
	dh_strip -a
# Do not compress the example source and binary files.
	dh_compress -av -Xexample-files
	dh_installdeb -a

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

# Send the proper shared libs details for the library:
	echo "libpwiz 3 libpwiz3 (>= 3.0.4043-1)" > \
	$(DEBIAN_DIR)/$(PROTEOWIZ_LIB_PACKAGE)/DEBIAN/shlibs

# Run the fixperms after generating the symbols and shlibs files so
# that the correct permissions are set for them also. Lintian was
# complaining.
	dh_fixperms -a
	dh_lintian -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


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