#!/usr/bin/make -f

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

CPU:=$(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

FC:=$(shell basename $(shell readlink -f /usr/bin/f95))
FMODDIR:= $(shell dh_fortran get_fmoddir)
FLIBDIR:= $(shell dh_fortran get_flibdir)
FCFLAGS:= -I$(FMODDIR) -L$(FLIBDIR)

ifeq ($(CPU), amd64)
  export FCFLAGS:= $(FCFLAGS) -mcmodel=medium
endif
ifeq ($(CPU), arm64)
  export FCFLAGS:= $(FCFLAGS) -mcmodel=large -fno-pie
endif
ifeq ($(CPU), ppc64el)
  export FCFLAGS:= $(FCFLAGS) -mcmodel=large
endif

LC_ALL=C
export LC_ALL

# magic debhelper rule
%:
	dh $@

override_dh_auto_build:
	$(MAKE) -f makefile.gfs_gfortran LDFLAGS="$(LDFLAGS)" FCFLAGS="$(FCFLAGS)"
	rm -f *.o *.mod
	$(MAKE) -f makefile.ecmwf_gfortran LDFLAGS="$(LDFLAGS)" FCFLAGS="$(FCFLAGS)"
	rm -f ./options/SPECIES/spec_overview
	find options -type f -exec chmod -x {} \;

override_dh_auto_clean:
	$(MAKE) -f makefile.gfs_gfortran clean
	$(MAKE) -f makefile.ecmwf_gfortran clean
	rm -f *.mod erf.f90.185r.combine flexpart.gfs flexpart.ecmwf
