#!/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

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)

BUILD_DIR = $(CURDIR)/debian/build

INSTALL_INDEP_DIR = $(CURDIR)/debian/tmp-indep
INSTALL_ARCH_DIR = $(CURDIR)/debian/tmp-arch

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 for debhelper compat level 9, see
# http://wiki.debian.org/Hardening

# To enable all, uncomment following line
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

.NOTPARALLEL:

.PHONY: clean
clean:
	dh_testdir
	dh_testroot

	dh_clean

# The following is for the binary software
	rm -rf $(BUILD_DIR)
	rm -rf $(INSTALL_INDEP_DIR) $(INSTALL_ARCH_DIR)

# Remove the generated files (even if by CMake).
	rm -f msxpertsuite-doc.7 \
	 massxpert/data/msxpertsuite-data.7 \
	 massxpert/massxpert.1 \
	 minexpert/minexpert.1

# The user manuals, after creation in the build tree are exported to the source
# tree. Remove them.
	rm -f massxpert/user-manual/massxpert-doc.pdf \
		minexpert/user-manual/minexpert-doc.pdf

# Remove all the doxygen-generated developer documentation.
	rm -rf devdoc/html

build-indep-stamp:
	dh_testdir
	dh_prep -i

	rm -rf $(INSTALL_INDEP_DIR)
	mkdir -p $(BUILD_DIR)

# The following is for building of the user manual
	cd $(BUILD_DIR) && \
	$(CMAKE) \
	-DOPENMP=1 \
	-DDEBUG=1 \
	-DBUILD_USER_MANUALS=1 \
	-DCMAKE_INSTALL_PREFIX=/usr ../..

	cd $(BUILD_DIR) && $(MAKE) -j1 massxpert-user-manual

	cd $(BUILD_DIR) && $(MAKE) -j1 minexpert-user-manual

	# The qthelp target depends on the devdoc target
	# that is responsible for compiling the Doxygen-based documentation
	cd $(BUILD_DIR) && $(MAKE) qthelp

	rm devdoc/html/jquery.js

	find devdoc/html -name "*.md5" | xargs rm

	touch build-indep-stamp


build-arch-stamp:
	dh_testdir
	dh_prep -a

	rm -rf $(INSTALL_ARCH_DIR)
	mkdir -p $(BUILD_DIR)

# The following is for building of the binary msxpertsuite software
	cd $(BUILD_DIR) && \
	$(CMAKE) \
	-DOPENMP=1 \
	-DDEBUG=1 \
	-DBUILD_USER_MANUALS=0 \
	-DCMAKE_INSTALL_PREFIX=/usr ../..

	cd $(BUILD_DIR) && $(MAKE) -j1 massxpert
	cd $(BUILD_DIR) && $(MAKE) -j1 minexpert

	touch build-arch-stamp


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

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

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


install-indep-stamp: build-indep-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -i

	mkdir -p $(INSTALL_INDEP_DIR)

# The general doc stuff
	$(MAKE) -C $(BUILD_DIR)/doc install DESTDIR=$(INSTALL_INDEP_DIR)

# But, now remove the license file
	rm $(INSTALL_INDEP_DIR)/usr/share/doc/msxpertsuite/COPYING

# Finally the indep data
	$(MAKE) -C $(BUILD_DIR)/massxpert/data install DESTDIR=$(INSTALL_INDEP_DIR)

# The user manuals

	$(MAKE) -C $(BUILD_DIR)/massxpert/user-manual install DESTDIR=$(INSTALL_INDEP_DIR)
#	DESTDIR=$(INSTALL_INDEP_DIR) $(CMAKE) -DCOMPONENT=usermanual -P $(BUILD_DIR)/massxpert/cmake_install.cmake

	$(MAKE) -C $(BUILD_DIR)/minexpert/user-manual install DESTDIR=$(INSTALL_INDEP_DIR)
#	DESTDIR=$(INSTALL_INDEP_DIR) $(CMAKE) -DCOMPONENT=usermanual -P $(BUILD_DIR)/minexpert/cmake_install.cmake

	touch install-indep-stamp


install-arch-stamp: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -a

	mkdir -p $(INSTALL_ARCH_DIR)

	# $(MAKE) -C $(BUILD_DIR)/massxpert install DESTDIR=$(INSTALL_ARCH_DIR)
	DESTDIR=$(INSTALL_ARCH_DIR) $(CMAKE) -DCOMPONENT=arch -P $(BUILD_DIR)/massxpert/cmake_install.cmake

	# $(MAKE) -C $(BUILD_DIR)/minexpert install DESTDIR=$(INSTALL_ARCH_DIR)
	DESTDIR=$(INSTALL_ARCH_DIR) $(CMAKE) -DCOMPONENT=arch -P $(BUILD_DIR)/minexpert/cmake_install.cmake

	# Install menu pixmap
	install -o root -g root -d $(INSTALL_ARCH_DIR)/usr/share/pixmaps
	install -o root -g root -m 644 \
		$(CURDIR)/images/msxpertsuite-icon-32.xpm \
		$(INSTALL_ARCH_DIR)/usr/share/pixmaps/msxpertsuite-icon-32.xpm

	touch install-arch-stamp


# Build architecture-independent files here.
.PHONY: binary-indep
binary-indep: install-indep-stamp
	dh_prep -i
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i

# symlink the jquery.js file
	dh_link -i
	dh_installdocs -i
	dh_install -i
	dh_installman -i
	dh_compress -Xmassxpert-doc.pdf -Xminexpert-doc.pdf -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i


# Build architecture-dependent files here.
.PHONY: binary-arch
binary-arch: install-arch-stamp
	dh_prep -a
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs  -a
	dh_installdocs -a
	dh_install -a
	dh_installman -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


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

