# Additional files to distribute
EXTRA_DIST += \
	KNOWN_PROBLEMS \
	PACKAGING \
	INSTALL_CentOS.md \
	INSTALL_EasyBuild.md \
	INSTALL_MacOS.md \
    INSTALL_Ubuntu.md \
	INSTALL_gpu.md \
	RELNOTES \
	abimkdocs \
	abimkdocs_plugin \
	abimkdocs_tests \
	abinit_theme \
	abisrc.py \
	abisrc_requirements.txt \
	autogen.sh \
	doc/tests \
	doc/theory/a-TDEP/a-TDEP_Guide.pdf \
	doc/theory/a-TDEP/a-TDEP_Paper.pdf \
	doc/topics/documents/elphon_manual.pdf \
	doc/topics/documents/geometry.pdf \
	doc/topics/documents/gwa.pdf \
	doc/topics/documents/howto_chebfi.pdf \
	doc/topics/documents/hybrids-2017.pdf \
	doc/topics/documents/noncol.pdf \
	mkdocs.yml \
	mksite.py \
	requirements.txt \
	scripts \
	setup.cfg \
	tasks.py \
	set_abienv.sh \
	wipeout.sh \
	upgrade-build-config-file.sh

# Additional files to clean
CLEANFILES += \
	config.fc_info.tmp

# Files to install
install-data-local: config.pc
	$(INSTALL) -d -m 755 $(DESTDIR)$(libdir)/pkgconfig
	$(INSTALL_DATA) -m 644 config.pc \
	 $(DESTDIR)$(libdir)/pkgconfig/abinit.pc

# Files to uninstall
uninstall-local:
	/bin/rm -f $(DESTDIR)$(libdir)/pkgconfig/abinit.pc

# Tests
basic_tests:
	cd $(top_builddir)/tests && $(MAKE) tests_in @SET_MAKE@

# Let Automake know how to perform basic tests
#check-local:
#$(MAKE) basic_tests @SET_MAKE@
check-local:
	cat $(top_srcdir)/tests/built-in/README

# Set of internal tests
test_fast:
	cp $(top_builddir)/src/98_main/abinit $(top_srcdir)/tests/built-in/Input/abinit && cd $(top_srcdir)/tests/built-in/Input && export ABI_PSPDIR="../../Psps_for_tests" && ./abinit testin_fast.abi >& testin_fast.stdout && cat testin_fastt_STATUS && rm -f abinit *DDB *EIG *out* *nc *WFK *abo* *o_* *t_STATUS*

test_v1:
	cp $(top_builddir)/src/98_main/abinit $(top_srcdir)/tests/built-in/Input/abinit && cd $(top_srcdir)/tests/built-in/Input && export ABI_PSPDIR="../../Psps_for_tests" && ./abinit testin_v1.abi >& testin_v1.stdout && cat testin_v1t_STATUS && rm -f abinit *DDB *EIG *out* *nc *WFK *abo* *o_* *t_STATUS*

test_v5:
	cp $(top_builddir)/src/98_main/abinit $(top_srcdir)/tests/built-in/Input/abinit && cd $(top_srcdir)/tests/built-in/Input && export ABI_PSPDIR="../../Psps_for_tests" && ./abinit testin_v5.abi >& testin_v5.stdout && cat testin_v5t_STATUS && rm -f abinit *DDB *EIG *out* *nc *WFK *abo* *o_* *t_STATUS*

test_bigdft:
	cp $(top_builddir)/src/98_main/abinit $(top_srcdir)/tests/built-in/Input/abinit && cd $(top_srcdir)/tests/built-in/Input && export ABI_PSPDIR="../../Psps_for_tests" && ./abinit testin_bigdft.abi >& testin_bigdft.stdout && cat testin_bigdftt_STATUS && rm -f abinit *DDB *EIG *out* *nc *WFK *abo* *o_* *t_STATUS*

test_libxc:
	cp $(top_builddir)/src/98_main/abinit $(top_srcdir)/tests/built-in/Input/abinit && cd $(top_srcdir)/tests/built-in/Input && export ABI_PSPDIR="../../Psps_for_tests" && ./abinit testin_libxc.abi >& testin_libxc.stdout && cat testin_libxct_STATUS && rm -f abinit *DDB *EIG *out* *nc *WFK *abo* *o_* *t_STATUS*

test_wannier90:
	cp $(top_builddir)/src/98_main/abinit $(top_srcdir)/tests/built-in/Input/abinit && cd $(top_srcdir)/tests/built-in/Input && export ABI_PSPDIR="../../Psps_for_tests" && ./abinit testin_wannier90.abi >& testin_wannier90.stdout && cat testin_wannier90t_STATUS  && rm -f abinit *DDB *EIG *out* *nc *WFK *DEN *chk *eig *mmn *amn *abo* *o_* *t_STATUS*

tests_in:
	$(MAKE) test_fast test_v1 test_v5 test_libxc test_wannier90 

# ---------------------------------------------------------------------------- #

# Help
help:
	@more $(top_srcdir)/doc/help_make/help_make_top.txt

help_dev dev_help dev:
	@more $(top_srcdir)/doc/help_make/help_make_top_dev.txt

# ---------------------------------------------------------------------------- #

# robodoc-html-x.x.x.tar contains all html files produced by ROBODOC
robodoc:
	rm -rf tmp-robodoc robodoc-html && mkdir tmp-robodoc
	cp -rfL $(top_srcdir)/shared/common/src/[0-3]* tmp-robodoc
	cp -rf $(top_srcdir)/src/[4-9]* tmp-robodoc
	cp $(top_srcdir)/config/robodoc/robodoc-html.rc tmp-robodoc/robodoc.rc
	cd tmp-robodoc && rm -f */*.in && rm -f */interfaces* && robodoc > ../robodoc.log 2> ../robodoc.err
	mv -f tmp-robodoc/www/robodoc robodoc-html
	tardir=robodoc-html && $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >robodoc-html-$(VERSION).tar.gz
	rm -rf robodoc-html tmp-robodoc
	cat $(top_srcdir)/doc/developers/robodoc.doc.txt >> robodoc.err

# ---------------------------------------------------------------------------- #

# Default number of processors for parallel builds
multi_nprocs = 4

# Special targets for parallel builds, in order to work around random
# failures (e.g. LibXC, NetCDF) and make sure dependencies are met
multi:
	$(MAKE) -j$(multi_nprocs) @SET_MAKE@

multi_dual mj2:
	$(MAKE) multi multi_nprocs=2 @SET_MAKE@

multi_quad mj4:
	$(MAKE) multi multi_nprocs=4 @SET_MAKE@

multi_octo mj8:
	$(MAKE) multi multi_nprocs=8 @SET_MAKE@

# ---------------------------------------------------------------------------- #

check_list:
	@cat $(top_srcdir)/doc/maintainers/check_list.txt

# ---------------------------------------------------------------------------- #

# Generate binary tarball
abinit_binpkgdir = $(ABINIT_BINARY_PACKAGE)
binary_package:
	-rm -rf $(abinit_binpkgdir) $(abinit_binpkgdir).tgz
	$(MKDIR_P) $(abinit_binpkgdir)
	$(MAKE) $(MAKEFLAGS) distdir @SET_MAKE@ && \
	 chmod -R u+w $(distdir) && \
	 mv $(distdir) $(abinit_binpkgdir)
	$(MAKE) $(MAKEFLAGS) install @SET_MAKE@ DESTDIR=$(PWD)/$(abinit_binpkgdir)
	$(MKDIR_P) $(abinit_binpkgdir)/$(abinit_datdir)
	cd $(abinit_binpkgdir); \
	 mv $(distdir)/tests ./$(abinit_datdir); \
	 mv $(distdir)/scripts ./$(abinit_datdir); \
	 mv $(distdir)/extras/matlab_data_analysis ./$(abinit_datdir); \
	 mv $(distdir)/extras/post_processing ./$(abinit_datdir); \
	 rm -rf $(distdir); \
	 tardir=. && \
	 $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >../$(abinit_binpkgdir).tgz

binary_package_lite:
	-rm -rf $(abinit_binpkgdir) $(abinit_binpkgdir).tgz
	$(MKDIR_P) $(abinit_binpkgdir)
	$(MAKE) $(MAKEFLAGS) distdir  && \
	 chmod -R u+w $(distdir) && \
	 mv $(distdir) $(abinit_binpkgdir)
	$(MAKE) $(MAKEFLAGS) install  DESTDIR=$(PWD)/$(abinit_binpkgdir)
	$(MKDIR_P) $(abinit_binpkgdir)/$(abinit_datdir)
	chmod o+rX $(abinit_binpkgdir); \
	cd $(abinit_binpkgdir); \
	 mv $(distdir)/scripts ./$(abinit_datdir); \
	 rm -rf $(distdir); \
	 mv usr/local/* .; \
	 rm -rf usr share/doc; \
	 mkdir -p etc; \
	 chmod -R o+rX *

dist-lite: distdir
	tardir=$(distdir) && \
	  cd $(distdir)/doc; rm -rf build* inp* t* guide  && \
	  cd ../tests; rm -rf v* tuto* paral mpiio libxc gpu fast bigdft* && \
          cd ../tests/Psps*; rm -rf $$(ls -I "*hpc*") && \
	  cd ../../abichecks; rm -rf abirules buildsys && \
	  cd ../..; \
	  rm -rf $(distdir)/packages && \
	  rm -rf $(distdir)/developers && \
	  rm -rf $(distdir)/scripts/post_processing && \
	  $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).tar.gz
	$(am__remove_distdir)

bin_prep:
	rm -fr abinitbin-$(VERSION) abinitbin.tar abinitbin_seq-$(VERSION)* abinitbin_seqpar-$(VERSION)*
	mkdir abinitbin-$(VERSION)
	tar -cvf abinitbin.tar AUTHORS COPYING INSTALL* KNOWN_PROBLEMS NEWS README* RELNOTES THANKS src \
            doc scripts developers tests/Psps* tests/v*/Input tests/tutorial tests/paral tests/buil* tests/Scripts tests/cpu \
            tests/tutorespfn tests/bigdft tests/netcdf tests/libxc tests/wannier90 tests/atompaw tests/mpiio tests/psml
	mv abinitbin.tar abinitbin-$(VERSION)
	cd abinitbin-$(VERSION) ; tar -xvf abinitbin.tar ; \
            rm -fr abinitbin.tar ; \
            cd src ; rm -fr */*.a ; \
            rm -fr 00*/*.F90 ; rm -fr 00*/*.o ; \
            rm -fr 01*/*.F90 ; rm -fr 01*/*.o ; \
            rm -fr 11*/*.F90 ; rm -fr 11*/*.o ; \
            rm -fr 12*/*.F90 ; rm -fr 12*/*.o ; \
            rm -fr 13*/*.F90 ; rm -fr 13*/*.o ; \
            rm -fr 14*/*.F90 ; rm -fr 14*/*.o ; \
            rm -fr 15*/*.F90 ; rm -fr 15*/*.o ; \
            rm -fr 16*/*.F90 ; rm -fr 16*/*.o ; \
            rm -fr 17*/*.F90 ; rm -fr 17*/*.o ; \
            rm -fr 18*/*.F90 ; rm -fr 18*/*.o ; \
            rm -fr 19*/*.F90 ; rm -fr 19*/*.o ; \
            rm -fr */*.F90 ; rm -fr */*.o ; \
            cd ../tests ; rm -fr */,* ; cd .. ; \
            mkdir opt ; mv src/98_main/* opt ; rm -fr src ; \
            cp ../developers/maintainers/Makefile_topdir_binarypack Makefile ; \
            cp ../developers/maintainers/Makefile_testdir_binarypack tests/Makefile

# FIXME: there is no point anymore in having 2 binary distributions
bin_seq: bin_prep
	cd abinitbin-$(VERSION) ; rm -fr tests/paral
	tar -cvf abinitbin_seq-$(VERSION).tar abinitbin-$(VERSION)
	chmod 644 abinitbin_seq-$(VERSION).tar
	gzip abinitbin_seq-$(VERSION).tar
	rm -fr abinitbin-$(VERSION)

bin_seqpar: bin_prep
	tar -cvf abinitbin_seqpar-$(VERSION).tar abinitbin-$(VERSION)
	chmod 644 abinitbin_seqpar-$(VERSION).tar
	gzip abinitbin_seqpar-$(VERSION).tar
	cd abinitbin-$(VERSION) ; rm -fr tests/paral
	tar -cvf abinitbin_seq-$(VERSION).tar abinitbin-$(VERSION)
	chmod 644 abinitbin_seq-$(VERSION).tar
	gzip abinitbin_seq-$(VERSION).tar
	rm -fr abinitbin-$(VERSION)

# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #

# Thorough clean-up
maintainer-clean-local:
	rm -f robodoc-html*
	find . -name '*.~[0-9]~' -exec rm -f {} \;
