#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

BUILDDIR = $(CURDIR)/debian/build
tmpInstall = $(CURDIR)/debian/tmp
tmpDirMatplotLib = $(CURDIR)/debian/matplotlib

# do not build i386 parallely
ifneq (,$(filter $(DEB_HOST_ARCH), i386 s390x all))
	parallelBuild=--no-parallel
endif

# We disable parallel builds on all platforms by now
# Ubuntu has problems with it and it causes sync-delays
parallelBuild=--no-parallel

%:
	dh $@ --builddirectory=$(BUILDDIR) --with python3 $(parallelBuild)

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed,-no-keep-memory

# no debug info to avoid running
# out of address space when linking
ifneq (,$(filter $(DEB_HOST_ARCH), mips mipsel i386))
	export DEB_CXXFLAGS_MAINT_APPEND += -g0
endif

# Check, whether Build-Depends-Indep are installed
BUILD_DOC=$(shell dpkg -s python3-sphinx | grep -q "^Status:.* installed$$" && echo yes)

export MPLCONFIGDIR := $(tmpDirMatplotLib)
export HOME := $(tmpDirMatplotLib)
export LD_LIBRARY_PATH :=  $(tmpInstall)/usr/lib/$(DEB_HOST_MULTIARCH)/yade/py:$(tmpInstall)/usr/lib/$(DEB_HOST_MULTIARCH)/yade:$(tmpInstall)/usr/lib/$(DEB_HOST_MULTIARCH)/yade/py:$(tmpInstall)/usr/lib/$(DEB_HOST_MULTIARCH)/yade/py/gts:$(tmpInstall)/usr/lib/$(DEB_HOST_MULTIARCH)/yade/py/yade:$(tmpInstall)/usr/lib/$(DEB_HOST_MULTIARCH)/yade/py/yade/qt:$(LD_LIBRARY_PATH)
export DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export YADE_PREFIX := $(tmpInstall)/usr

extra_flags += \
-DruntimePREFIX="/usr" \
-DCMAKE_INSTALL_PREFIX="/usr" \
-DNOSUFFIX=ON \
-DLIBRARY_OUTPUT_PATH=lib/$(DEB_HOST_MULTIARCH) \
-DENABLE_SPH=ON \
-DPYTHON_VERSION=3

override_dh_auto_configure:
	dh_auto_configure -- $(extra_flags)
	mkdir -p $(tmpDirMatplotLib)
	mkdir -p $(tmpInstall)
	#Output an information about available memory and CPU
	-cat /proc/meminfo
	-cat /proc/cpuinfo

override_dh_auto_install:
	dh_auto_install
	# generate manpages
	help2man $(tmpInstall)/usr/bin/yade > yade.1
	help2man $(tmpInstall)/usr/bin/yade-batch > yade-batch.1
	$(tmpInstall)/usr/bin/yade --test
	$(tmpInstall)/usr/bin/yade --checkall
	dh_numpy3
ifeq ($(BUILD_DOC), yes)
	#Generate docs
	cd $(CURDIR)/doc/sphinx && PYTHONPATH=. $(tmpInstall)/usr/bin/yade yadeSphinx.py html && PYTHONPATH=. $(tmpInstall)/usr/bin/yade yadeSphinx.py latex && cd _build/latex && xelatex Yade.tex && xelatex Yade.tex && xelatex Yade.tex
	cd $(CURDIR)/doc/sphinx/_build/html && find . -type f -print0 | xargs -0 sed -i 's/https:\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/mathjax\/2\.7\.5\/latest.js/\/usr\/share\/javascript\/mathjax\/MathJax.js\//g'
	cd $(CURDIR)/doc/sphinx/_build/html && find . -type f -print0 | xargs -0 sed -i 's/https:\/\/www\.youtube\.com//g'
endif
	#Delete all pyc files
	find $(tmpInstall) -name '*.pyc' -print0 | xargs -0 rm -f
	find $(tmpInstall) -name '__pycache__' -print0 | xargs -0 rm -rf
	find $(tmpInstall) -name 'yade.py' -print0 | xargs -0 rm -f

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

override_dh_installman:
	dh_installman -p yade yade.1 yade-batch.1

override_dh_compress:
	dh_compress --all -X.py -X.stl -X.gts -X.geo -X.mesh -X.msh -X.pdf -X.png -X.svg -X.txt -X.js -X.inv

override_dh_auto_test:

override_dh_installdocs:
	dh_installdocs
ifeq ($(BUILD_DOC), yes)
	dh_sphinxdoc
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(BUILDDIR)

override_dh_dwz:
