#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CXXFLAGS_MAINT_APPEND=-O3
export DEB_CXXFLAGS_MAINT_STRIP=-O2
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)
DPKG_EXPORT_BUILDFLAGS = 1
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

include /usr/share/dpkg/pkg-info.mk

SOVERSION_STOPT=5
VERSION_STOPT=$(shell echo $(DEB_VERSION_UPSTREAM) | sed -E 's/~rc[0-9]*// ; s/\+dfsg[0-9]*//')

%:
	dh $@ --with python3

override_dh_auto_clean:
	dh_auto_clean
	# Removing the build directory of the LaTeX documentation.
	-rm -rf texdocBuild/

override_dh_auto_configure-arch:
	dh_auto_configure -- \
	        -DCMAKE_LIBRARY_ARCHITECTURE="$(DEB_HOST_MULTIARCH)" \
	        -DCMAKE_INSTALL_PREFIX="/usr" \
	        -DCMAKE_BUILD_RPATH_USE_ORIGIN=ON \
	        -DSOVERSION_STOPT="$(SOVERSION_STOPT)" \
	        -DVERSION_STOPT="$(VERSION_STOPT)"

override_dh_auto_configure-indep:

override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	# Building the LaTeX documentation.
	# Two sets of paths have to be changed because we used a MUT package.
	cp -a texdoc/ texdocBuild/
	cd texdocBuild && \
	        sed -i 's|../StOpt/StOpt/|../StOpt/|g' documentation.tex && \
	        sed -i 's|../StOpt/test/|../test/|g' documentation.tex && \
	        pdflatex documentation.tex && \
	        bibtex documentation.aux && \
	        pdflatex documentation.tex && \
	        pdflatex documentation.tex
endif

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test -a --no-parallel
endif

# No test to perform for the architecture-independent packages
override_dh_auto_test-indep:

override_dh_installdocs-indep:
	dh_installdocs -i
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	mkdir -p debian/stopt-doc/usr/share/doc/stopt-doc/
	cp texdocBuild/documentation.pdf debian/stopt-doc/usr/share/doc/stopt-doc/
endif

override_dh_installexamples-indep:
	dh_installexamples -i
	cp -a test/python/ debian/stopt-examples/usr/share/doc/stopt-examples/examples/
	# Some Python tests cannot be run because we don't package all built .so...
	# some of them being designed for the tests only.
	rm -rf debian/stopt-examples/usr/share/doc/stopt-examples/examples/python/functional/
	rm -rf debian/stopt-examples/usr/share/doc/stopt-examples/examples/python/unit/geners/testSomeBinaryArchiveStorage.py
	rm -rf debian/stopt-examples/usr/share/doc/stopt-examples/examples/python/unit/global
	rm -rf debian/stopt-examples/usr/share/doc/stopt-examples/examples/python/unit/sddp
	rm -rf debian/stopt-examples/usr/share/doc/stopt-examples/examples/python/unit/tree

override_dh_fixperms-indep:
	dh_fixperms -i
	#Setting all Python scripts (that are shipped as examples) as being executable
	for F in $$(find debian/stopt-examples/usr/share/doc/stopt-examples/examples/python/ -name "test*.py"); do \
	        chmod a+x $$F; \
	done
