#!/usr/bin/make -f

export DH_VERBOSE=1

export FC_DEFAULT=gfortran
export FC_OPTIONAL=flang lfortran

include /usr/share/dpkg/architecture.mk
FLIBDIR:=/usr/lib/${DEB_HOST_MULTIARCH}/fortran

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


# Need a bootstrap version for fpm full build
execute_before_dh_auto_clean:
	- mv fpm.toml.orig fpm.toml
	- mv meson.build.orig meson.build
	- mv config/meson.build.orig config/meson.build

FB=/usr/share/dh-fortran/dh_fortran_build

override_dh_auto_build:
	:
override_dh_auto_test:
	:

override_dh_auto_install:
	fpm install --compiler gfortran --flag "-fPIC -I$(FLIBDIR)/gfortran-mod-16/toml-f -I$(FLIBDIR)/gfortran-mod-16/testdrive" \
		--link-flag " -L$(FLIBDIR)/gnu/" --verbose --prefix=$(CURDIR)/debian/tmp-gfortran/usr
	-(fpm install --compiler flang-21 --flag "-fPIC -I$(FLIBDIR)/flang-mod-1/toml-f -I$(FLIBDIR)/flang-mod-1 -I$(FLIBDIR)/flang-mod-1/testdrive" \
		--link-flag " -L$(FLIBDIR)/flang/" --verbose --prefix=$(CURDIR)/debian/tmp-flang/usr ) || true
	-(fpm install --compiler lfortran --flag "--separate-compilation -fPIC -I$(FLIBDIR)/lfortran-mod-0/toml-f -I$(FLIBDIR)/lfortran-mod-0 -I$(FLIBDIR)/lfortran-mod-0/testdrive" \
		--link-flag " -L$(FLIBDIR)/lcc/" --verbose --prefix=$(CURDIR)/debian/tmp-lfortran/usr ) || true
	dh_fortran_lib --fc gfortran -P debian/tmp-gfortran
	dh_fortran_lib --fc flang  -P debian/tmp-flang
	dh_fortran_lib --fc lfortran -P debian/tmp-lfortran

execute_before_dh_auto_configure:
	cp meson.build meson.build.orig
	cp config/meson.build config/meson.build.orig
	cp debian/meson.build.bootstrap meson.build
	cp debian/config.meson.build.bootstrap config/meson.build
ifneq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
	cp fpm.toml fpm.toml.orig
	cp debian/fpm.toml.bootstrap fpm.toml
endif

execute_after_dh_clean:
	rm -rf build mbuild subprojects/test-drive subprojects/toml-f

execute_before_dh_install:
	# to create  pkgconfig file
	meson setup mbuild --prefix=$(CURDIR)/debian/tmp/usr
	meson install -C mbuild

execute_after_dh_fixperms:
	chrpath -d debian/libfortran-jonquil-0/usr/lib/*/*

