#!/usr/bin/make -f

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CMAKE_EXTRA_FLAGS = \
	-DCMAKE_BUILD_TYPE:STRING=None \
	-DBUILD_SHARED_LIBS:BOOL=ON \
	-DVTK_INSTALL_EXPORT_NAME:STRING=mrmpi \
	-DVTK_LIBRARY_PROPERTIES:STRING="VERSION;1;SOVERSION;1" \
	-DVTK_INSTALL_INCLUDE_DIR_CM24:STRING=include \
	-DVTK_INSTALL_BIN_DIR_CM24:STRING=bin \
	-DVTK_INSTALL_LIB_DIR_CM24:STRING=lib/$(DEB_HOST_MULTIARCH) \
	-DVTK_USE_MPI:BOOL=ON

pkg_run = libmrmpi1
pkg_dev = libmrmpi-dev
pkg_doc = mrmpi-doc

%:
	dh $@ --buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_EXTRA_FLAGS)

override_dh_compress:
	dh_compress -X.pdf

override_dh_auto_clean:
	rm -f mrmpi_txt2html
	rm -f doc/Manual.pdf
	rm -f doc/*.html
	rm -f oinkdoc/Manual.pdf
	rm -f oinkdoc/*.html
	dh_auto_clean

override_dh_install-arch:
	# Runtime Package
	dh_install -p$(pkg_run) usr/lib/$(DEB_HOST_MULTIARCH)/lib*.so.*
	# Development Package
	dh_install -p$(pkg_dev) usr/include
	dh_install -p$(pkg_dev) usr/lib/$(DEB_HOST_MULTIARCH)/lib*.so

# Let's be smart with building doc
override_dh_auto_build-indep:
	# Need a custom txt2html
	g++ ./txt2html/txt2html.cpp -o mrmpi_txt2html
	# 1. MapReduceMPI:
	# Generate html file for PDF:
	./mrmpi_txt2html -b doc/*.txt
	# Generate PDF from HTML
	(cd doc && htmldoc --title --toctitle "Table of Contents" --tocfooter ..i --toclevels 4 \
		--header ... --footer ..1 --size letter --linkstyle plain --linkcolor blue -f \
		Manual.pdf Manual.html Background.html Whatis.html Start.html Program.html \
		Interface_c++.html [a-z]*.html Interface_c.html Interface_python.html \
		Interface_oink.html Technical.html Examples.html)
	# Generate clean HTML files:
	./mrmpi_txt2html doc/*.txt
	# 2. OINK:
	(cd oinkdoc && python3 Manual.py)
	./mrmpi_txt2html -b oinkdoc/*.txt
	(cd oinkdoc && htmldoc --title --toctitle "Table of Contents" --tocfooter ..i --toclevels 4 \
		--header ... --footer ..1 --size letter --linkstyle plain --linkcolor blue -f \
		Manual.pdf Manual.html Section_build.html Section_script.html \
		Section_functions.html Section_commands.html Section_errors.html [a-z]*.html)
	./mrmpi_txt2html oinkdoc/*.txt

# No tests needed for docs
override_dh_auto_test-indep:

VER_FULL = $(shell dpkg-parsechangelog | grep '^Version' | cut -d' ' -f2 | cut -f1 -d-)

UPSTREAM_SRC = mrmpi
UPSTREAM_SRC_DIR = mrmpi-7Apr14
DEBIAN_SRC_DIR = mrmpi-$(VER_FULL)
DEBIAN_SRC_TAR = mrmpi_$(VER_FULL).orig.tar.gz

txt2html.tar.gz:
	wget http://www.cs.sandia.gov/~sjplimp/tars/$@

$(UPSTREAM_SRC).tar.gz: txt2html.tar.gz
	wget http://www.cs.sandia.gov/~sjplimp/tars/$@

get-orig-source: $(UPSTREAM_SRC).tar.gz
	tar xzf $(UPSTREAM_SRC).tar.gz
	mv $(UPSTREAM_SRC_DIR) $(DEBIAN_SRC_DIR)
	rm $(DEBIAN_SRC_DIR)/oinkdoc/Manual.pdf
	rm $(DEBIAN_SRC_DIR)/oinkdoc/*.html
	rm $(DEBIAN_SRC_DIR)/doc/Manual.pdf
	rm $(DEBIAN_SRC_DIR)/doc/*.html
	tar xfz txt2html.tar.gz -C $(DEBIAN_SRC_DIR)
	GZIP="--best --no-name" tar czf $(DEBIAN_SRC_TAR) $(DEBIAN_SRC_DIR)
	rm -rf $(DEBIAN_SRC_DIR)
	rm $(UPSTREAM_SRC).tar.gz
	rm txt2html.tar.gz
