#!/usr/bin/make -f

B3DEST=$(CURDIR)/debian/python3-escript
B3MDEST=$(CURDIR)/debian/python3-escript-mpi
DDEST=$(CURDIR)/debian/python-escript-doc
BUILD3=$(CURDIR)/debian/build
BUILD3M=$(CURDIR)/debian/build-mpi
WORK3=$(CURDIR)/debian/tmp
WORK3M=$(CURDIR)/debian/tmp-mpi

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# The additional -Werror=implicit-function-declaration is useless in its functionality,
# but it is useful to satisfy a pedantic buildflag check in the source code.
# See https://bugs.debian.org/1068158 and debian/patches/fix-dpkg-buildflags-on-c-c++.patch
export DEB_CXXFLAGS_MAINT_APPEND = -Wno-uninitialized -Werror=implicit-function-declaration
export DEB_CFLAGS_MAINT_APPEND = -Wno-uninitialized

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk


#thanks to the debian manual
# export DEB_BUILD_OPTIONS="parallel=10"
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    PARBUILD:=$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    SFLAGS:=-j$(PARBUILD)
else
    SFLAGS:=-j1
endif

ifeq ($(DEB_HOST_ARCH_BITS), 64)
  OPT := -O3
else
  OPT := -Os -g1
endif

ifeq ($(DEB_HOST_ARCH), mips64el)
  OPT += -mxgot
endif

%:
	dh $@  -v

execute_after_dh_clean:
	rm -f itest.sh utest.sh
	rm -rf build condif.log esys include lib release .sconf_tmp .sconsign.dblite config.log .sconf_temp
	rm -f bin/run-escript escript/py_src/run_escript.py bin/escript-overlord
	rm -rf $(BUILD3) $(BUILD3M) $(WORK3M)

override_dh_installchangelogs:
	dh_installchangelogs debian/changelog.trivial

override_dh_auto_build:
	mkdir -p $(WORK3) $(WORK3M)
	scons -j4 options_file=scons/templates/debian_nompi_options.py build_dir=$(BUILD3) prefix=$(WORK3) cc_optim='$(OPT)' verbose=on
	scons -j4 options_file=scons/templates/debian_options.py build_dir=$(BUILD3M) prefix=$(WORK3M) cc_optim='$(OPT)' verbose=on
	# (scons $(SFLAGS) cc_optim='$(OPT)'  build_dir=$(BUILD3) verbose=on prefix=$(WORK5) options_file=debian/sid_options.py docs ) || true
	# extract the relevant .py files
	$(CURDIR)/debian/utils/cppy.py debian/build/posix debian/tmp 
	$(CURDIR)/debian/utils/cppy.py debian/build-mpi/posix debian/tmp-mpi
	mkdir -p $(BUILD3)/bin $(BUILD3M)/bin
	cp bin/run-escript	$(BUILD3)/bin/run-escript
	cp bin/run-escript	$(BUILD3M)/bin/run-escript-mpi
	# cp $(PROJROOT)/doc/manpage/man1/run-escript.1 $(WORK3)/run-escript3.man
	# cp $(PROJROOT)/doc/manpage/man1/run-escript.1 $(WORK3M)/run-escript3-mpi.man
	# fix paths in each package and fix some external links in the doc package
	debian/utils/tweak.sh  "$(BUILD3)" "$(BUILD3M)"
	find $(BUILD3) -name '*.so' -exec chrpath -d {} \;
	find $(BUILD3M) -name '*.so' -exec chrpath -d {} \;

execute_after_dh_fixperms:
	chrpath -d $(B3MDEST)/usr/bin/pyt* $(B3MDEST)/usr/bin/escript-overlord

override_dh_shlibdeps:
	# so libraries we just built are checked as well
	dh_shlibdeps -ppython3-escript  -Npython3-escript-mpi -Npython-escript-doc -P$(B3DEST) -l$(B3DEST)/usr/lib/python3-escript/lib
	dh_shlibdeps -ppython3-escript-mpi -Npython3-escript -Npython-escript-doc -P$(B3MDEST) -l$(B3MDEST)/usr/lib/python3-escript-mpi/lib

override_dh_python3:
	dh_python3 -ppython3-escript -Npython3-escript-mpi -Npython-escript-doc --no-ext-rename	usr/lib/python3-escript/esys
	dh_python3 -ppython3-escript-mpi -Npython3-escript -Npython-escript-doc --no-ext-rename	usr/lib/python3-escript-mpi/esys

