#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk
include /usr/share/mpi-default-dev/debian_defaults

export SCALAPACK_LIBS:=-lscalapack-$(ARCH_DEFAULT_MPI_IMPL)
export SCALAPACK_LIB:=/usr/lib
ifeq ($(DEB_BUILD_ARCH_CPU), i386)
    export FFTW_FORCE_ALIGN=-D_FFTW_FORCE_UNALIGNED
else
    export FFTW_FORCE_ALIGN=
endif
export OMPI_MCA_orte_rsh_agent=/bin/false

export F90FLAGS = $(shell dpkg-buildflags --get FFLAGS) -cpp -fallow-argument-mismatch

export DEB_CFLAGS_MAINT_APPEND = -std=gnu17

# workaround for dpkg exporting conflicting DFLAGS since #975896
export DFLAGS

%:
	dh $@

override_dh_auto_clean:
	-dh_auto_clean
	rm -rf CPV/tmp
	rm -f install/make_wannier90.sys
	rm -f install/make_lapack.inc
	rm -f install/make_blas.inc
	rm -f include/fft_defs.h
	rm -f Modules/version.f90.tmp
	rm -f include/configure.h

override_dh_auto_build-arch:
	$(MAKE) all epw

override_dh_auto_build-indep:
	$(MAKE) pw doc
	cp debian/pseudo/*.UPF pseudo/

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	cp debian/pseudo/*.UPF pseudo/
	-(cd test-suite; $(MAKE) -k run-tests)
endif

override_dh_installexamples-indep:
	for DIR in $$(find . -name debian -prune -o -name .pc -prune -o -name examples -print); \
	do dh_install -p quantum-espresso-data $$DIR/* usr/share/doc/quantum-espresso/examples/$$(dirname $$DIR); \
	done

override_dh_installchangelogs:
	dh_installchangelogs Doc/release-notes

override_dh_fixperms-indep:
	dh_fixperms
	find debian/quantum-espresso-data/usr/share/doc/quantum-espresso/examples -name \*.in | xargs chmod 644
