#!/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 dh_fortran get_fc_default)
# CPATH= $(shell dh_fortran get_fmoddir)
# for this build use /usr/include until netcdff-dev is multifortran
CPATH=/usr/include
# LIBRARY_PATH:= $(shell dh_fortran get_flibdir)
LIBRARY_PATH:= /usr/include/$(DEB_HOST_MULTIARCH)
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
export FC LIBRARY_PATH CPATH

# magic debhelper rule
%:
	dh $@


override_dh_auto_build:
	$(MAKE) -C src -f makefile_gfortran LDFLAGS="$(LDFLAGS) -L$(LIBRARY_PATH) -lnetcdff -leccodes -leccodes_f90 -lgomp" FCFLAGS="$(FCFLAGS)" ncf=yes eta=yes
	rm -f src/*.o src/*.mod
	$(MAKE) -C src -f makefile_gfortran LDFLAGS="$(LDFLAGS) -L$(LIBRARY_PATH) -lnetcdff -leccodes -leccodes_f90 -lgomp" FCFLAGS="$(FCFLAGS)" ncf=yes eta=no
	rm -f ./options/SPECIES/spec_overview
	find options -type f -exec chmod -x {} \;

execute_before_dh_auto_clean:
	- mv src/FLEXPART.f90.orig src/FLEXPART.f90

execute_before_dh_auto_configure:
	cp src/FLEXPART.f90 src/FLEXPART.f90.orig

override_dh_auto_clean:
	$(MAKE) -f src/makefile_gfortran clean
	rm -f src/*.mod erf.f90.185r.combine src/FLEXPART src/FLEXPART_ETA src/*.o src/gitversion.txt
