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

#export DH_VERBOSE=1

include /usr/share/mpi-default-dev/debian_defaults

DEB_BUILD_ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)

ifeq (,$(filter mpich,$(ARCH_DEFAULT_MPI_IMPL)))
export SCALAPACK_LIBS:=-lscalapack-$(ARCH_DEFAULT_MPI_IMPL) -lblacs-$(ARCH_DEFAULT_MPI_IMPL) -lblacsF77init-$(ARCH_DEFAULT_MPI_IMPL)
else
export SCALAPACK_LIBS:=-lscalapack-mpich2 -lblacs-mpich2 -lblacsF77init-mpich2
endif
export SCALAPACK_LIB:=/usr/lib
ifeq ($(DEB_BUILD_ARCH), i386)
    export FFTW_FORCE_ALIGN=-D_FFTW_FORCE_UNALIGNED
else
    export FFTW_FORCE_ALIGN=
endif
export OMPI_MCA_orte_rsh_agent=/bin/false

%:
	dh $@ --with autotools_dev --parallel

override_dh_auto_clean:
	dh_auto_clean
	(cd TDDFPT/Examples; $(MAKE) clean)
	find PW/examples -name "results" | xargs rm -rf
	find PW/examples/tests -name "*.out" | xargs rm -rf
	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
	# work-around for shipped binary in upstream tarball
	rm -f CPV/src/wfdd.x

override_dh_auto_build:
	$(MAKE) all

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# Copy in some required pseudo-potentials which are shipped in other 
	# parts of the source tree
	(cd pseudo; for i in \
		TDDFPT/Examples/pseudo/C.pz-rrkjus.UPF \
		TDDFPT/Examples/pseudo/H.pz-vbc.UPF \
		TDDFPT/Examples/pseudo/O.pbe-rrkjus.UPF \
		TDDFPT/Examples/pseudo/O.pz-rrkjus.UPF \
		TDDFPT/Examples/pseudo/Si.pbe-rrkj.UPF \
		atomic/examples/pseudo-LDA-0.5/Si.pz-vbc.UPF \
	; do ln -sf ../$$i .; done)
	-(cd PW/tests; sh ./check-pw.x.j)
	-(cd CPV/tests; sh ./check-cp.x.j)
	-(cd TDDFPT/Examples; $(MAKE) small_test)
	# Remove symliked pseudo-potentials again
	find pseudo -type l | xargs rm -f
endif

override_dh_installchangelogs:
	dh_installchangelogs Doc/release-notes

binary-indep:
	dh $@ --before dh_auto_build --with autotools_dev --parallel
	(cd pseudo; ../PW/src/generate_vdW_kernel_table.x)
	(cd Doc; $(MAKE))
	dh $@ --after dh_auto_test --with autotools_dev --parallel

binary: binary-arch binary-indep
