#!/usr/bin/make -f

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

include /usr/share/debhelper/dh-fortran/fortran-support.mk

# magic debhelper rule
%:
	dh  $@  --buildsystem=pybuild

PYTHON3:=$(shell py3versions -vr)
PY3DEFAULT:=$(shell py3versions -vd)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DESTDIR:=$(CURDIR)/debian/tmp
LIBDIR:=$(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)
LIBDIR_FC:=$(LIBDIR)/fortran/$(FC_DEFAULT)

LD_LIBRARY_PATH:=$(CURDIR)/debian/tmp:$(LD_LIBRARY_PATH)
export LD_LIBRARY_PATH

override_dh_auto_configure: $(PYTHON3:%=configure-python%)
	:

override_dh_auto_build: $(PYTHON3:%=build-python%)
	:
	# meson compile -C build-$(PY3DEFAULT) sphere:static_library sphere:shared_library

override_dh_auto_install: $(PYTHON3:%=install-python%)
	mkdir -p $(LIBDIR_FC) 
	mv $(DESTDIR)/usr/lib/python3/dist-packages/Src $(DESTDIR)/usr/lib/python3/dist-packages/sphere
	cp Lib/__init__.py Lib/sphere.py $(DESTDIR)/usr/lib/python3/dist-packages/sphere
	mv $(LIBDIR)/libsphere.a $(LIBDIR_FC)
	cp build-$(PY3DEFAULT)/Src/libsphere.so $(LIBDIR)/libsphere-$(FC_DEFAULT).so.0d
	ln -s libsphere-$(FC_DEFAULT).so.0d $(LIBDIR)/libsphere-$(FC_DEFAULT).so
	ln -s ../../libsphere-$(FC_DEFAULT).so.0d $(LIBDIR_FC)/libsphere.so
	dh_numpy3

override_dh_auto_test: $(PYTHON3:%=test-python%)
	:

override_dh_auto_clean:
	rm -rf build-* .pybuild

configure-python%:
	python$* /usr/bin/meson setup --prefix /usr --buildtype plain build-$* 

build-python%:
	python$* /usr/bin/meson compile -C build-$*

test-python%:
	python$* /usr/bin/meson test -C build-$*

install-python%:
	python$* /usr/bin/meson install -C build-$* --destdir $(DESTDIR)

