#!/usr/bin/make -f

export DH_VERBOSE=1


# Hard-coded to avoid a circular dependency on dh-fortran 
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
FMODDIR:=/usr/lib/${DEB_HOST_MULTIARCH}/fortran/gfortran-mod-16
FLIBDIR:=/usr/lib/${DEB_HOST_MULTIARCH}/fortran/gnu

# The fpm library stack (toml-f, jonquil, ...) is not built on i386 in
# Ubuntu, so only ship the bootstrap fpm there, as with the stage1 profile.
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(DEB_HOST_ARCH),i386)
DEB_BUILD_PROFILES += stage1
endif


# The magic debhelper  rule
%:
	dh $@ 


override_dh_auto_clean:
	rm -rf build bootstrap

override_dh_auto_build:
	mkdir bootstrap
	cp debian/bootstrap/fpm.F90 bootstrap
	(cd bootstrap ; gfortran -o fpm fpm.F90)

override_dh_auto_install:
ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
	./bootstrap/fpm install --flag "-I ${FMODDIR} -I ${FMODDIR}/toml-f -I ${FMODDIR}/jonquil -static -L ${FLIBDIR}" --prefix=debian/tmp/usr
else
	mkdir -p debian/tmp/usr/bin
	cp bootstrap/fpm debian/tmp/usr/bin
endif

override_dh_auto_test:
ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
	(fpm test --flag "-I${FMODDIR} -I${FMODDIR}/toml-f -I${FMODDIR}/jonquil" --link-flag -L${FLIBDIR} || true)
else
	@echo No tests in bootstrap builds
endif
