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

# Set path to this directory so that our tweaked fop script is used.
PATH := $(CURDIR)/doc/user-manuals/scripts:$(PATH)
$(info PATH: $(PATH))

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

# 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

	rm -f fop

	dh_clean


build-indep-stamp:
	dh_testdir -i
	dh_prep -i

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

# The following is for building of the doc files (man pages, not the user
# manuals)
	cd $(BUILD_DIR) && \
	$(CMAKE) \
	-DOPENMP=1 \
	-DDEBUG=1 \
	-DBUILD_USER_MANUALS=0 \
	-DCMAKE_INSTALL_PREFIX=/usr ../..

# Ensure that the fop.xconf file is found in the DC* files
# that are located in $(CURDIR)/doc/user-manuals
	cd doc/user-manuals/massxpert && \
		sed -i 's|/home/rusconi/.fop/fop.xconf|$(CURDIR)/doc/user-manuals/fop.xconf|' DC-massxpert-user-manual

	cd doc/user-manuals/minexpert && \
		sed -i 's|/home/rusconi/.fop/fop.xconf|$(CURDIR)/doc/user-manuals/fop.xconf|' DC-minexpert-user-manual

	# The following is for building of the user manuals
	cd doc/user-manuals/massxpert && make all_f
	# Copythe default-named pdf file to what we want
	cp -fv doc/user-manuals/massxpert/build/massxpert-user-manual/massxpert-user-manual_color_en.pdf doc/user-manuals/massxpert/build/massxpert-user-manual/massxpert-doc.pdf

	cd doc/user-manuals/minexpert && make all_f
	# Copythe default-named pdf file to what we want
	cp -fv doc/user-manuals/minexpert/build/minexpert-user-manual/minexpert-user-manual_color_en.pdf doc/user-manuals/minexpert/build/minexpert-user-manual/minexpert-doc.pdf

	# Remove libjs-jquery from both massxpert and minexpert html user manuals
	# build directory and later replace them with symbolic links to the same file
	# of the libjs-jquery package (see the debian/*links files for the doc
	# packages).
	
	rm -f doc/user-manuals/minexpert/build/minexpert-user-manual/html/minexpert-user-manual/static/js/jquery-1.10.2.min.js
	rm -f doc/user-manuals/massxpert/build/massxpert-user-manual/html/massxpert-user-manual/static/js/jquery-1.10.2.min.js
	rm -f doc/user-manuals/minexpert/build/minexpert-user-manual/html/minexpert-user-manual/static/js/highlight.min.js
	rm -f doc/user-manuals/massxpert/build/massxpert-user-manual/html/massxpert-user-manual/static/js/highlight.min.js

	touch build-indep-stamp



build-arch-stamp:
	dh_testdir -a
	dh_prep -a
	dh_installdirs -i

	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 -i
	dh_testroot -i
	dh_installdirs -i

	mkdir -p $(INSTALL_INDEP_DIR)

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

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

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

	touch install-indep-stamp


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

	mkdir -p $(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)/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

