# Copyright (C) 2001-2016 Quantum ESPRESSO group
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License. See the file `License' in the root directory
# of the present distribution.
#
# Makefile for plugins

sinclude ../make.sys
include plugins_list

# MAIN target
EXTLIBS=liblapack libblas libiotk


###########################################################
# Template function
# $(1) = package name
# $(2) = package URL
# $(3) = directory name
# $(4) = plugin/code name
###########################################################

define download_and_unpack
	@(if test ! -s ../archive/$(1).tar.gz && test ! -d ../$(3) ; then \
	wget -O ../archive/$(1).tar.gz $(2) > /dev/null 2>&1; \
	if test "`echo $$?`" -ne "0" ; then \
	curl -o ../archive/$(1).tar.gz $(2) > /dev/null 2>&1; \
	if test "`echo $$?`" -ne "0" ; then \
	echo "*** Unable to download $(4). Test whether curl or wget is installed and working," ; \
	echo "*** if you have direct access to internet. If not, copy into archive/ the file" ; \
	      echo "*** $(1).tar.gz from $(2)." ; \
	exit 1 ; fi ; fi ; fi)
	if test ! -d ../$(3); then \
	(gzip -dc ../archive/$(1).tar.gz | (cd ../ ; tar -xvf -)) ; fi
	if test ! -d ../$(3); then \
	(cd ../ ; ln -s $(1) $(3)) ; fi
endef


###########################################################
# LD1
###########################################################

ld1:  uncompress-ld1
	if test -d ../atomic; then \
	cd ../atomic; $(MAKE) all || exit 1; cd ../bin; ln -fs ../atomic/src/ld1.x . ; fi
	touch make-ld1

uncompress-ld1: touch-dummy
	$(call download_and_unpack,$(LD1),$(LD1_URL),atomic,ld1)
	touch uncompress-ld1

###########################################################
# XSpectra
###########################################################

xspectra:  uncompress-xspectra
	if test -d ../XSpectra; then \
	cd ../XSpectra; $(MAKE) all || exit 1; cd ../bin; ln -fs ../XSpectra/src/xspectra.x . ; fi
	touch make-xspectra

uncompress-xspectra: touch-dummy
	$(call download_and_unpack,$(XSPECTRA),$(XSPECTRA_URL),XSpectra,xspectra)
	touch uncompress-xspectra

###########################################################
# PWCOND
###########################################################

pwcond:  uncompress-pwcond
	if test -d ../PWCOND; then \
	cd ../PWCOND; $(MAKE) all || exit 1; cd ../bin; ln -fs ../PWCOND/src/pwcond.x . ; fi
	touch make-pwcond

uncompress-pwcond: touch-dummy
	$(call download_and_unpack,$(PWCOND),$(PWCOND_URL),PWCOND,pwcond)
	touch uncompress-pwcond

###########################################################
# NEB
###########################################################

neb:  uncompress-neb
	if test -d ../NEB; then \
	cd ../NEB; $(MAKE) all || exit 1; cd ../bin; ln -fs ../NEB/src/neb.x . ; fi
	touch make-neb

uncompress-neb: touch-dummy
	$(call download_and_unpack,$(NEB),$(NEB_URL),NEB,neb)
	touch uncompress-neb

###########################################################
# TDDFPT
###########################################################

tddfpt:  uncompress-tddfpt
	if test -d ../TDDFPT; then \
	cd ../TDDFPT; $(MAKE) all || exit 1; cd ../bin; ln -fs ../TDDFPT/src/turbo_lanczos.x . ; \
	ln -fs ../TDDFPT/tools/turbo_spectrum.x . ; fi
	touch make-tddfpt

uncompress-tddfpt: touch-dummy
	$(call download_and_unpack,$(TDDFPT),$(TDDFPT_URL),TDDFPT,tddfpt)
	touch uncompress-tddfpt

###########################################################
# PHonon
###########################################################

phonon:  uncompress-phonon
	if test -d ../PHonon; then \
	cd ../PHonon; $(MAKE) all || exit 1; cd ../bin; \
	ln -fs ../PHonon/PH/ph.x . ; \
	ln -fs ../PHonon/PH/dynmat.x . ; \
	ln -fs ../PHonon/PH/matdyn.x . ; \
	ln -fs ../PHonon/PH/q2r.x . ; \
	ln -fs ../PHonon/PH/q2qstar.x . ; \
	ln -fs ../PHonon/PH/lambda.x . ; \
	ln -fs ../PHonon/PH/fqha.x . ; \
	ln -fs ../PHonon/Gamma/phcg.x . ; \
	ln -fs ../PHonon/D3/d3.x . ; \
	fi
	touch make-phonon

uncompress-phonon: touch-dummy
	$(call download_and_unpack,$(PHONON),$(PHONON_URL),PHonon,phonon)
	touch uncompress-phonon

###########################################################
# test-suite
###########################################################

test-suite: uncompress-test-suite
	if test -d ../test-suite ; then \
	( cd ../test-suite ; $(MAKE) TLDEPS= run-tests || exit 1) ; fi

uncompress-test-suite: touch-dummy
	$(call download_and_unpack,$(TESTSUITE),$(TESTSUITE_URL),test-suite,test-suite)
	touch uncompress-test-suite

##########################################################
# GIPAW
###########################################################

gipaw:  uncompress-gipaw configure-gipaw
	if test -d ../GIPAW; then \
	cd ../GIPAW; $(MAKE) all || exit 1; cd ../bin; ln -fs ../GIPAW/src/gipaw.x . ; fi
	touch make-gipaw

uncompress-gipaw: touch-dummy
	$(call download_and_unpack,$(GIPAW),$(GIPAW_URL),GIPAW,qe-gipaw)	
	touch uncompress-gipaw

configure-gipaw: touch-dummy
	cd ../GIPAW ; \
	./configure --with-qe-source=${TOPDIR}
	touch ./configure-gipaw

###########################################################
# EPW
###########################################################

epw: extlibs uncompress-epw
	if test -d ../EPW; then \
	cd ../EPW ; $(MAKE) all ; $(MAKE) all || exit 1; cd ../bin; ln -fs ../EPW/bin/epw.x . ; fi
	-(cd ../bin; ln -fs ../EPW/src/epw.x .)
	touch ./make-epw

uncompress-epw: touch-dummy
	$(call download_and_unpack,$(EPW),$(EPW_URL),EPW,EPW)
	touch uncompress-epw

###########################################################
# wannier90
###########################################################

w90: uncompress-w90 configure-w90
	if test -d ../W90; then \
	cd ../W90; $(MAKE) all || exit 1; cd ../bin; ln -fs ../W90/wannier90.x . ; fi
	-(cd ../bin; ln -fs ../W90/wannier90.x .)
	touch make-w90

uncompress-w90: touch-dummy
	$(call download_and_unpack,$(W90),$(W90_URL),W90,wannier90)
	touch uncompress-w90

configure-w90: touch-dummy
	cd ../W90 ; \
	if (test -e ../install/make_wannier90.sys) || (test -d ../W90); then \
	(cp ../install/make_wannier90.sys ../W90/make.sys); fi 
	touch ./configure-w90

###########################################################
# WANT
###########################################################

want: extlibs uncompress-want configure-want
	if test -d ../WANT; then \
	cd ../WANT; $(MAKE) all || exit 1; fi
#	- ( cd ../bin ; ln -sf ../WANT/bin/* . ) 
	touch ./make-want

uncompress-want: touch-dummy
	$(call download_and_unpack,$(WANT),$(WANT_URL),WANT,want)
	touch uncompress-want

configure-want: touch-dummy
	cd ../WANT ; \
	./configure \
	--with-external-iotk \
	IOTK_LIBS="-L../../../S3DE/iotk/src -liotk" \
	IOTK_INCLUDE="$(MOD_FLAG)../../../S3DE/iotk/src" \
	CC="$(CC)" \
	F90="$(F90)" \
	MPIF90="$(MPIF90)" \
	F90FLAGS="$(F90FLAGS) $($PRE_FDFLAGS) $(FDFLAGS) $(IFLAGS) $(MODFLAGS)" \
	F77="$(F77)" \
	FFLAGS="$(FFLAGS)" \
	FFLAGS_NOOPT="$(FFLAGS_NOOPT)" \
	LD="$(LD)" \
	LDFLAGS="$(LDFLAGS)" \
	AR="$(AR)" \
	ARFLAGS="$(ARFLAGS)" \
	RANLIB="$(RANLIB)" \
	DFLAGS="$(DFLAGS)" \
	FDFLAGS="$(DFLAGS)" \
	LAPACK_LIBS="$(LAPACK_LIBS)" \
	BLAS_LIBS="$(BLAS_LIBS)" \
	FFT_LIBS="$(FFT_LIBS)" 
	touch ./configure-want

###########################################################
# YAMBO
###########################################################

yambo: extlibs uncompress-yambo configure-yambo
	if test -d ../YAMBO; then \
	cd ../YAMBO; $(MAKE) yambo interfaces ypp || exit 1; fi
	touch ./make-yambo

uncompress-yambo: touch-dummy
	$(call download_and_unpack,$(YAMBO),$(YAMBO_URL),YAMBO,yambo)
	touch uncompress-yambo

configure-yambo: touch-dummy
	@(if test -z "$(F90)" ; then \
	echo "*** F90 is not defined; please define F90 or edit make.sys" ; exit 1 ; fi)
	cd ../YAMBO ; \
	./configure \
	 --with-blas-libs="$(BLAS_LIBS)" \
	 --with-lapack-libs="$(LAPACK_LIBS) $(BLAS_LIBS)" \
	 --with-iotk-path="$(TOPDIR)/iotk" \
	 --with-fft-libs="$(FFT_LIBS)" \
	 --with-scalapack-libs="$(SCALAPACK_LIBS)" \
	 --with-blacs-libs="$(SCALAPACK_LIBS)" \
	 PFC="$(MPIF90)" \
	 FC="$(F90)" \
	 F77="$(F77)"
	touch ./configure-yambo

yambo-devel: extlibs uncompress-yambo-devel configure-yambo-devel
	if test -d ../YAMBO_DEVEL; then \
	cd ../YAMBO_DEVEL; $(MAKE) yambo interfaces ypp || exit 1; fi
	touch ./make-yambo-devel

uncompress-yambo-devel: touch-dummy
	$(call download_and_unpack,$(YAMBO_DEVEL),$(YAMBO_DEVEL_URL),YAMBO_DEVEL,yambo-devel)
	touch uncompress-yambo-devel

configure-yambo-devel: touch-dummy
	@(if test -z "$(F90)" ; then \
	echo "*** F90 is not defined; please define F90 or edit make.sys" ; exit 1 ; fi)
	cd ../YAMBO_DEVEL ; \
	./configure \
	 --with-blas-libs="$(BLAS_LIBS)" \
	 --with-lapack-libs="$(LAPACK_LIBS) $(BLAS_LIBS)" \
	 --with-iotk-path="$(TOPDIR)/iotk" \
	 --with-fft-libs="$(FFT_LIBS)" \
	 --with-scalapack-libs="$(SCALAPACK_LIBS)" \
	 --with-blacs-libs="$(SCALAPACK_LIBS)" \
	 PFC="$(MPIF90)" \
	 FC="$(F90)" \
	 F77="$(F77)"
	touch ./configure-yambo-devel

touch-dummy:
	$(dummy-variable)

###########################################################
# plumed
###########################################################

plumed: test-plumed uncompress-plumed patch-plumed
	if test -d ../PLUMED; then \
	cd ../; $(MAKE) pw; $(MAKE) cp; fi
	touch make-plumed

test-plumed: touch-dummy
	@(if test ! -s ../archive/$(PLUMED).tar.gz && test ! -d ../PLUMED; then \
	echo "The release contains no PLUMED package. Trying to download it from qe-forge."; fi) 
	touch ./test-plumed

uncompress-plumed: touch-dummy
	$(call download_and_unpack,$(PLUMED),$(PLUMED_URL),PLUMED,plumed)
	touch uncompress-plumed

patch-plumed: touch-dummy
	if test -d ../PLUMED; then \
	(cd ../PLUMED ; export plumedir="`pwd`" ; \
	cp  patches/plumedpatch_qespresso_5.2.sh ../ ; \
	cd ../ ; ./plumedpatch_qespresso_5.2.sh -patch) ; fi	
	touch ./patch-plumed

###########################################################
# SaX
###########################################################

sax: extlibs uncompress-sax configure-sax
	if test -d ../SaX; then \
	cd ../SaX; $(MAKE) sax_all; fi
#	- ( cd ../bin ; ln -sf ../SaX/sax/bin/* . ) 
	touch ./make-sax

uncompress-sax: touch-dummy
	$(call download_and_unpack,$(SAX),$(SAX_URL),SaX,$(SAX))
	touch uncompress-sax

configure-sax: touch-dummy
	cd ../SaX ; \
	./configure \
	--with-external-iotk \
	IOTK_LIBS="-L../../S3DE/iotk/src -liotk" \
	IOTK_INCLUDE="$(MOD_FLAG)../../S3DE/iotk/src" \
	CC="$(CC)" \
	F90="$(F90)" \
	MPIF90="$(MPIF90)" \
	F90FLAGS="$(F90FLAGS) $($PRE_FDFLAGS) $(FDFLAGS) $(IFLAGS) $(MODFLAGS)" \
	F77="$(F77)" \
	FFLAGS="$(FFLAGS)" \
	FFLAGS_NOOPT="$(FFLAGS_NOOPT)" \
	LD="$(LD)" \
	LDFLAGS="$(LDFLAGS)" \
	AR="$(AR)" \
	ARFLAGS="$(ARFLAGS)" \
	RANLIB="$(RANLIB)" \
	LIB_LAPACK="$(LAPACK_LIBS)" \
	LIB_BLAS="$(BLAS_LIBS)" 
	touch ./configure-sax

###########################################################
# WEST
###########################################################

west: extlibs uncompress-west 
	if test -d ../West; then \
	cd ../West; $(MAKE) -j1 all || exit 1; fi
	touch ./make-west

uncompress-west: touch-dummy
	$(call download_and_unpack,$(WEST),$(WEST_URL),West,WEST)
	touch uncompress-west

###########################################################
# extlibs
###########################################################

extlibs:
	test -n "$(EXTLIBS)" && ( cd .. ; $(MAKE) $(EXTLIBS) || exit 1) || :

###########################################################
# GUI
###########################################################

gui:  uncompress-gui
	touch make-gui

uncompress-gui: touch-dummy
	$(call download_and_unpack,$(GUI),$(GUI_URL),GUI,pwGUI)
	touch uncompress-gui

###########################################################
# GPU
###########################################################

gpu:  uncompress-gpu
	touch make-gpu

uncompress-gpu: touch-dummy
	$(call download_and_unpack,$(GPU),$(GPU_URL),GPU,QE-GPU)
	@ echo ""
	@ echo " *****************************************************************************"
	@ echo " * QE-GPU has been downloaded under ./archive Please follow the instructions *"
	@ echo " *   reported here: http://qe-forge.org/gf/project/q-e-gpu/                  *" 
	@ echo " *****************************************************************************"
	@ echo ""
	touch uncompress-gpu

###########################################################
# cleaning
###########################################################

# each lib independently
ld1_clean:
	if test -d ../atomic; then (cd ../atomic; \
	$(MAKE) clean); fi
	rm -f ../bin/ld1.x
	rm -rf ./make-ld1 

ld1_veryclean: ld1_clean
# dir is not deleted for compatibility with the repository

ld1_distclean: 
	rm -f ../archive/$(LD1).tar.gz

xspectra_clean:
	if test -d ../XSpectra; then (cd ../XSpectra; \
	$(MAKE) clean); fi
	rm -f ../bin/xspectra.x
	rm -rf ./make-xspectra

xspectra_veryclean: xspectra_clean
# dir is not deleted for compatibility with the repository

xspectra_distclean: 
	rm -f ../archive/$(XSPECTRA).tar.gz

pwcond_clean:
	if test -d ../PWCOND; then (cd ../PWCOND; \
	$(MAKE) clean); fi
	rm -f ../bin/pwcond.x
	rm -rf ./make-pwcond

pwcond_veryclean: pwcond_clean
# dir is not deleted for compatibility with the repository

pwcond_distclean:
	rm -f ../archive/$(PWCOND).tar.gz

neb_clean:
	if test -d ../NEB; then (cd ../NEB; \
	$(MAKE) clean); fi
	rm -f ../bin/neb.x
	rm -rf ./make-neb
 
neb_veryclean: neb_clean
# dir is not deleted for compatibility with the repository

neb_distclean: 
	rm -f ../archive/$(NEB).tar.gz

tddfpt_clean:
	if test -d ../TDDFPT; then (cd ../TDDFPT; \
	$(MAKE) clean); fi
	rm -f ../bin/turbo_lanczos.x
	rm -f ../bin/turbo_davidson.x
	rm -f ../bin/turbo_eels.x
	rm -f ../bin/turbo_spectrum.x
	rm -rf ./make-tddfpt

tddfpt_veryclean: tddfpt_clean 
# dir is not deleted for compatibility with the repository

tddfpt_distclean: 
	rm -f ../archive/$(TDDFPT).tar.gz

phonon_clean:
	if test -d ../PHonon; then (cd ../PHonon; \
	$(MAKE) clean); fi
	rm -rf ../bin/ph.x
	rm -rf ../bin/dynmat.x
	rm -rf ../bin/matdyn.x
	rm -rf ../bin/q2r.x
	rm -rf ../bin/q2qstar.x
	rm -rf ../bin/lambda.x
	rm -rf ../bin/fqha.x
	rm -rf ../bin/phcg.x
	rm -rf ../bin/d3.x
	rm -rf ./make-phonon 

phonon_veryclean: phonon_clean
# dir is not deleted for compatibility with the repository
# exe links to main bin dir are deleted by PHonon Makefile

phonon_distclean:
	rm -f ../archive/$(PHONON).tar.gz

gipaw_clean:
	if test -d ../GIPAW; then (cd ../GIPAW; $(MAKE) clean); fi
	rm -f ../bin/gipaw.x
	rm -rf ./make-gipaw 

gipaw_veryclean: gipaw_clean
	if test -d ../GIPAW; then (cd ../GIPAW; \
	rm -f config.log config.status ); fi
	rm -rf ./configure-gipaw

gipaw_distclean:
	if test -d ../GIPAW; then (rm -R -f ../GIPAW ); fi
	if test -d ../$(GIPAW); then (rm -R -f ../$(GIPAW) ); fi
	rm -f ../archive/$(GIPAW).tar.gz

epw_clean:
	if test -d ../EPW; then (cd ../EPW; \
	$(MAKE) clean); fi
	- rm -rf ./make-epw ./configure-epw

epw_veryclean: epw_clean
# dir is not deleted for compatibility with the repository

epw_distclean:
	rm -f ../archive/$(EPW).tar.gz

w90_clean:
	if test -d ../W90; then (cd ../W90; \
	$(MAKE) veryclean); fi
	rm -f ../bin/wannier90.x
	rm -rf ./make-w90 ./configure-w90

w90_veryclean: w90_clean
	if test -d ../W90; then (rm -R -f ../W90); fi
	if test -d ../$(W90); then (rm -R -f ../$(W90)); fi
	rm -f ../bin/wannier90.x
	rm -rf ./uncompress-w90

w90_distclean:
	rm -f ../archive/$(W90).tar.gz

want_clean:
	if test -d ../WANT; then (cd ../WANT; \
	$(MAKE) clean); fi
	- rm -rf ./make-want ./configure-want

want_veryclean: want_clean
	if test -d ../WANT; then (rm -R -f ../WANT); fi
	if test -d ../$(WANT); then (rm -R -f ../$(WANT)); fi
	if test -e ../archive/$(WANT).tar.gz ; then (rm -f ../archive/$(WANT).tar.gz); fi
	- rm -rf ./uncompress-want

want_distclean: 
	rm -f ../archive/$(WANT).tar.gz

yambo_clean:
	if test -d ../YAMBO; then (cd ../YAMBO; \
	$(MAKE) clean); fi
	- rm -rf ./make-yambo ./configure-yambo

yambo_veryclean: yambo_clean
	if test -d ../YAMBO; then (rm -R -f ../YAMBO); fi
	if test -d ../$(YAMBO); then (rm -R -f ../$(YAMBO)); fi
	- rm -rf ./uncompress-yambo

yambo_distclean: 
	if test -e ../archive/$(YAMBO).tar.gz ; then \
		rm -f ../archive/$(YAMBO).tar.gz; fi

yambo-devel_clean:
	if test -d ../YAMBO_DEVEL; then (cd ../YAMBO_DEVEL; \
	$(MAKE) clean); fi
	- rm -rf ./make-yambo-devel ./configure-yambo-devel

yambo-devel_veryclean: yambo-devel_clean
	if test -d ../YAMBO_DEVEL; then (rm -R -f ../YAMBO_DEVEL); fi
	if test -d ../$(YAMBO_DEVEL); then (rm -R -f ../$(YAMBO_DEVEL)); fi
	- rm -rf ./uncompress-yambo-devel

yambo-devel_distclean: 
	if test -e ../archive/$(YAMBO_DEVEL).tar.gz ; then \
		rm -f ../archive/$(YAMBO_DEVEL).tar.gz; fi

plumed_clean: 
	if test -d ../PLUMED; then (cd ../PLUMED; \
	export plumedir="`pwd`" ; \
	cd ../ ; ./plumedpatch_qespresso_5.2.sh -revert); fi
	cd ../PW ; $(MAKE) clean; cd ../CPV ; $(MAKE) clean
	rm -rf ./make-plumed ./patch-plumed

plumed_veryclean: plumed_clean
	if test -d ../PLUMED; then (rm -R -f ../PLUMED); fi
	if test -d ../$(PLUMED); then (rm -R -f ../$(PLUMED)); fi
	rm -rf ./uncompress-plumed ../plumedpatch_qespresso_5.2.sh

plumed_distclean: 
# Nothing to do here

sax_clean:
	if test -d ../SaX; then (cd ../SaX; \
	$(MAKE) clean); fi
	- rm -rf ./make-sax ./configure-sax

sax_veryclean: sax_clean
	if test -d ../SaX; then (rm -R -f ../SaX); fi
	if test -d ../$(SAX); then (rm -R -f ../$(SAX)); fi
	- rm -rf ./uncompress-sax

sax_distclean: 
	rm -f ../archive/$(SAX).tar.gz

west_clean:
	if test -d ../West; then (cd ../West; \
	$(MAKE) clean); fi
	- rm -rf ./make-west ./configure-west

west_veryclean: west_clean
# dir is not deleted for compatibility with the repository

west_distclean: 
	   rm -f ../archive/$(WEST).tar.gz

gui_clean:
	rm -rf ./make-gui 

gui_veryclean: gui_clean
# dir is not deleted for compatibility with the repository

gui_distclean: 

test-suite_clean:
	if test -d ../test-suite; then (cd ../test-suite; \
	$(MAKE) clean); fi

test-suite_veryclean: test-suite_clean

test-suite_distclean:
	rm -f ../archive/$(TESTSUITE).tar.gz

gpu_clean:
	rm -f ./make-gpu 

gpu_veryclean: gpu_clean
# dir is not deleted for compatibility with the repository

gpu_distclean: 
	rm -f ../archive/$(GPU).tar.gz

##########################################################
# general cleaning
##########################################################

clean: w90_clean want_clean yambo_clean yambo-devel_clean plumed_clean \
	epw_clean gui_clean phonon_clean tddfpt_clean xspectra_clean neb_clean \
	pwcond_clean ld1_clean gipaw_clean gpu_clean west_clean test-suite_clean \
	sax_clean

veryclean: w90_veryclean want_veryclean yambo_veryclean yambo-devel_veryclean \
	plumed_veryclean epw_veryclean gui_veryclean phonon_veryclean \
	tddfpt_veryclean xspectra_veryclean neb_veryclean pwcond_veryclean \
	ld1_veryclean gipaw_veryclean gpu_veryclean west_veryclean \
	test-suite_veryclean sax_veryclean

#distclean : veryclean
distclean: w90_distclean want_distclean yambo_distclean yambo-devel_distclean \
  epw_distclean gui_distclean phonon_distclean test-suite_distclean \
  tddfpt_distclean xspectra_distclean neb_distclean pwcond_distclean \
  ld1_distclean gipaw_distclean gpu_distclean west_distclean sax_distclean

