#!/usr/bin/make -f

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

export FC_DEFAULT=gfortran
# export FC_DEFAULT=flang-22 lfortran

include /usr/share/dpkg/architecture.mk

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

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

FC:=$(shell dh_fortran get_fc_default)
F77:=$(FC)
export F77 FC


DESTDIR:=$(CURDIR)/debian/tmp
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)
ABI:=$(shell dh_fortran get_abi_vendor)
LIBDIR_FC:=$(LIBDIR)/fortran/$(ABI)


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 $(DESTDIR)/$(LIBDIR) $(DESTDIR)/$(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 $(DESTDIR)/$(LIBDIR)/libsphere.a $(DESTDIR)/$(LIBDIR_FC)
	cp build-$(PY3DEFAULT)/Src/libsphere.so	$(DESTDIR)/$(LIBDIR)/libsphere-$(ABI).so.0d
	dh_link -p libsphere-dev  $(LIBDIR)/libsphere-$(ABI).so.0d $(LIBDIR_FC)/libsphere.so
	dh_link -p libsphere-dev  $(LIBDIR)/libsphere-$(ABI).so.0d $(LIBDIR)/libsphere-$(ABI).so

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)

