#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

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

# parallel support
#NPROC ?= $(shell dh_assistant which-build-system| jq .parallel)
NPROC ?= $(shell nproc)  # for backport

# Multiarch
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# so version number managed by Debian
SONAME=0
SOVERSION=0.0.1

MODULES         = boost_adaptbx cctbx scitbx crys3d libtbx iotbx wxtbx smtbx

embed:
	# a bunche of modules are missing in the original git repository, so embed them for now
	# annlib_adaptbx
	git clone https://github.com/cctbx/annlib_adaptbx annlib_adaptbx
	rm -rf annlib_adaptbx/.git*
%:
	dh $@ --with python3 --buildsystem=pybuild

annlib_adaptbx:
	cp -r debian/annlib_adaptbx annlib_adaptbx

setup.py : debian/setup.py
	cp -f $< $@

override_dh_auto_clean: setup.py
	dh_auto_clean
	find . -name __pycache__ -type d -exec rm -rf {} \;

override_dh_auto_configure: setup.py annlib_adaptbx
	dh_auto_configure
	dh_auto_configure -- -s custom --configure-args="mkdir -p {build_dir}_ && cd {build_dir}_ && {interpreter} $(CURDIR)/libtbx/configure.py --build=debug --use_environment_flags $(MODULES)"

override_dh_auto_build: setup.py annlib_adaptbx
	# build the python modules
	dh_auto_build

	# build the extensions and the libraries
	dh_auto_build -- -s custom --build-args="cd {build_dir}_ && ./bin/libtbx.scons -j $(NPROC)"
	iconv -f ISO-8859-1 -t UTF-8 cctbx/eltbx/covalent_radii.h > covalent_radii.h
	mv covalent_radii.h cctbx/eltbx/covalent_radii.h

	# move all extensions into the python build directory
	dh_auto_build -- -s custom --build-args="mv {build_dir}_/lib/*_ext* {build_dir}"

override_dh_auto_install: setup.py annlib_adaptbx
	dh_auto_install

	# install all the scripts
	dh_auto_install -- -s custom --install-args="mkdir -p {destdir}/usr/bin \
		; cp -f {build_dir}_/bin/* {destdir}/usr/bin"

	# install the libraries with soversion numbers
	dh_auto_install -- -s custom --install-args="mkdir -p {destdir}/usr/lib/$(DEB_HOST_MULTIARCH) \
		; cp -f {build_dir}_/lib/*.so {destdir}/usr/lib/${DEB_HOST_MULTIARCH}"

	cd $(CURDIR)/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH} && \
	for f in *.so; do \
		mv $$f $$f.$(SOVERSION);\
		ln -s $$f.$(SOVERSION) $$f.$(SONAME); \
		ln -s $$f.$(SONAME) $$f; \
	done

	# install all the headers
	mkdir -p $(CURDIR)/debian/tmp/usr/include/cctbx/
	rsync -ar --prune-empty-dirs --exclude=debian --include=*/ --include="*.h" --include="*.hpp" --exclude="*" $(CURDIR)/ $(CURDIR)/debian/tmp/usr/include/cctbx/

	# install the generated headers
	dh_auto_install -- -s custom --install-args="rsync -ar --prune-empty-dirs --include=*/ --include='*.h' --include='*.hpp' --exclude='*' {build_dir}_/include/ {destdir}/usr/include/cctbx/"

	# dh_auto_install -- -s custom --install-args="mkdir -p {destdir}/usr/include/cctbx/scitbx/array_family \
        #         ; cp -f {build_dir}_/include/scitbx/array_family/small_reductions.h {destdir}/usr/include/cctbx/scitbx/array_family \
        #         ; cp -f {build_dir}_/include/scitbx/array_family/small_algebra.h  {destdir}/usr/include/cctbx/scitbx/array_family \
        #         ; cp -f {build_dir}_/include/scitbx/array_family/operator_traits_builtin.h {destdir}/usr/include/cctbx/scitbx/array_family \
        #         ; cp -f {build_dir}_/include/scitbx/array_family/tiny_reductions.h {destdir}/usr/include/cctbx/scitbx/array_family \
	# 	; mkdir -p {destdir}/usr/include/cctbx/scitbx/array_family/detail \
        #         ; cp -f {build_dir}_/include/scitbx/array_family/detail/operator_functors.h {destdir}/usr/include/cctbx/scitbx/array_family/detail \
        #         ; cp -f {build_dir}_/include/scitbx/array_family/detail/std_imports.h {destdir}/usr/include/cctbx/scitbx/array_family/detail"

	# install /usr/share/cctbx files
	dh_auto_install -- -s custom --install-args="mkdir -p {destdir}/usr/share/cctbx \
                ; cp -f {build_dir}_/libtbx_env {destdir}/usr/share/cctbx"


override_dh_auto_test: setup.py annlib_adaptbx
	# DOES NOT WORK, source code must be modified in order to find the expected
	# /usr/share/cctbx libtbx_env file during the build
	#dh_auto_test -- -s custom --test-args="LD_LIBRARY_PATH={build_dir}_/lib dh_auto_test"

.PHONY: embed
