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

SOVERSION_STOPT=4
VERSION_STOPT=4.2

%:
	dh $@ --with python3

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

override_dh_installdocs:
	dh_installdocs
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	# Building the latex documentation and installing it.
	# Two sets of paths have to be changed because we used a MUT package.
	cd texdoc && \
	        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
	mkdir -p debian/stopt-doc/usr/share/doc/stopt-doc/
	cp texdoc/documentation.pdf debian/stopt-doc/usr/share/doc/stopt-doc/
endif

override_dh_installexamples:
	dh_installexamples
	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:
	dh_fixperms
	# Manually setting permissions to 644 for source code provided as example
	chmod 644 debian/stopt-examples/usr/share/doc/stopt-examples/examples/c++/python/AR1Wrap.h
	chmod 644 debian/stopt-examples/usr/share/doc/stopt-examples/examples/c++/python/Pybind11SDDPOptimizers.cpp
	chmod 644 debian/stopt-examples/usr/share/doc/stopt-examples/examples/c++/python/Pybind11SDDPSimulators.cpp
	#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
