#!/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_INDEP_DIR = $(CURDIR)/debian/build-indep
BUILD_ARCH_DIR = $(CURDIR)/debian/build-arch

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_INDEP_DIR)
	rm -rf $(BUILD_ARCH_DIR)
	rm -rf $(INSTALL_INDEP_DIR) $(INSTALL_ARCH_DIR)

# No more used because we now use CMake with UseLATEX.cmake
# The following is for the user manual
#	cd $(CURDIR)/usermanual && $(MAKE) distclean


build-indep-stamp: 
	dh_testdir
	dh_prep -i

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

# The following is for building of the user manual
	cd $(BUILD_INDEP_DIR) && \
	$(CMAKE) \
	-D BUILD_PROGRAM=0 \
	-D BUILD_DATA=1 \
	-D BUILD_USERMANUAL=1 \
	-D CMAKE_INSTALL_PREFIX=/usr ../.. 

	# No more this way, as we now use CMake with UseLATEX.cmake
	# cd $(CURDIR)/usermanual && $(MAKE) -f Makefile all_no_reload
	$(MAKE) -C $(BUILD_INDEP_DIR)

	touch build-indep-stamp


build-arch-stamp: 
	dh_testdir
	dh_prep -a

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

# The following is for building of the binary massxpert software
	cd $(BUILD_ARCH_DIR) && \
	$(CMAKE) -D CMAKE_BUILD_TYPE=debug \
	-D BUILD_PROGRAM=1 \
	-D BUILD_DATA=0 \
	-D BUILD_USERMANUAL=0 \
	-D CMAKE_INSTALL_PREFIX=/usr ../.. 

	$(MAKE) -C $(BUILD_ARCH_DIR)

	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)
	$(MAKE) -C $(BUILD_INDEP_DIR) install DESTDIR=$(INSTALL_INDEP_DIR)

# Remove the license file
	rm $(INSTALL_INDEP_DIR)/usr/share/doc/massxpert/COPYING

	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_ARCH_DIR) install DESTDIR=$(INSTALL_ARCH_DIR)

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

# Remove the license file
	rm $(INSTALL_ARCH_DIR)/usr/share/doc/massxpert/COPYING

	touch install-arch-stamp


# Build architecture-independent files here.
.PHONY: binary-indep
binary-indep: install-indep-stamp
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_install -i
	dh_installman -i
	dh_compress -Xmassxpert.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_testdir -a
	dh_testroot -a
	dh_installchangelogs  -a
	dh_installdocs -a
	dh_install -a
	dh_installmenu -a
	dh_installman -a
	dh_strip --dbg-package=massxpert-dbg -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

