#!/usr/bin/make -f

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

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

# Hack until gfortran updated: /usr/bin/f95 only exists when the gfortran
# metapackage is installed; some buildds resolve 'gfortran | fortran-compiler'
# to gfortran-N only (no /usr/bin/f95), so fall back to any installed gfortran.
FC:=$(shell if [ -x /usr/bin/f95 ]; then basename "$$(readlink -f /usr/bin/f95)"; elif command -v gfortran >/dev/null 2>&1; then echo gfortran; else for c in gfortran-15 gfortran-14 gfortran-13 gfortran-12; do if command -v $$c >/dev/null 2>&1; then echo $$c; break; fi; done; fi)
# Fortran module include dirs shipped by libeccodes-dev (either
# fortran/<compiler> legacy layout or fortran/gfortran-mod-<abi>);
# exclude flang dirs (flang-mod-<n>) whose .mod files gfortran cannot read.
# The flang dirs are collected explicitly, because a pattern containing two
# wildcards such as %flang% does not match them in filter-out.
FMODDIRS := $(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/fortran/*)
FCFLANGDIRS := $(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/fortran/*flang*)
FCFLAGS := $(addprefix -I,$(filter-out $(FCFLANGDIRS),$(FMODDIRS)))

CFLAGS=$(shell dpkg-buildflags --get CFLAGS) -g
LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)
CFLAGS+=$(HARDENING_CFLAGS)
LDFLAGS+=$(HARDENING_LDFLAGS)

LC_ALL=C
export LC_ALL

# magic debhelper rule
%:
	dh $@

override_dh_auto_build:
	$(MAKE) -f makefile.ecmwf LDFLAGS="$(LDFLAGS)" FCFLAGS="$(FCFLAGS)" FC=$(FC)
	rm *.o
	$(MAKE) -f makefile.gfs  LDFLAGS="$(LDFLAGS)" FCFLAGS="$(FCFLAGS)" FC=$(FC)


override_dh_auto_clean:
	rm -f *.o flextra.ecmwf flextra.gfs

