# 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.inc
include plugins_list
include install_utils

# MAIN target
EXTLIBS=liblapack libiotk


###########################################################
# D3Q
###########################################################

d3q:  uncompress-d3q
	if test -d ../D3Q; then \
	cd ../D3Q; $(MAKE) || exit 1; \
	fi
	touch make-d3q

uncompress-d3q:
	$(call download_and_unpack,$(D3Q),$(D3Q_URL),D3Q,d3q)
	touch uncompress-d3q

d3q_clean:
	if test -d ../D3Q; then (cd ../D3Q; $(MAKE) clean); fi
	rm -f ../bin/d3q.x
	rm -f ../bin/d3_*.x
	rm -f ../bin/diffd3.x
	rm -f ../bin/xml2giorgia.x

#d3q_veryclean: d3q_clean
d3q_veryclean: d3q_clean
	# delete unpacked directory and its links, keep the archive
	(cd ..; \
		i=D3Q; \
		while j=$$(readlink $$i);do rm -rf $$i; i=$$j;done;\
		rm -rf $$i\
	); \

d3q_distclean: d3q_veryclean
	# also delete the tgz archive
	rm -f ../archive/$(D3Q); \
	rm -f make-d3q uncompress-d3q

###########################################################
# 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:
	$(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:
	$(call download_and_unpack,$(GIPAW),$(GIPAW_URL),GIPAW,qe-gipaw)	
	touch uncompress-gipaw

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

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

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

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

configure-w90: 
	cd ../W90 ; \
	if (test -e ../install/make_wannier90.inc) || (test -d ../W90); then \
	(cp ../install/make_wannier90.inc ../W90/make.inc); 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: 
	$(call download_and_unpack,$(WANT),$(WANT_URL),WANT,want)
	touch uncompress-want

configure-want:
	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: 
	$(call download_and_unpack,$(YAMBO),$(YAMBO_URL),YAMBO,yambo)
	touch uncompress-yambo

configure-yambo: 
	@(if test -z "$(F90)" ; then \
	echo "*** F90 is not defined; please define F90 or edit make.inc" ; 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: 
	$(call download_and_unpack,$(YAMBO_DEVEL),$(YAMBO_DEVEL_URL),YAMBO_DEVEL,yambo-devel)
	touch uncompress-yambo-devel

configure-yambo-devel: 
	@(if test -z "$(F90)" ; then \
	echo "*** F90 is not defined; please define F90 or edit make.inc" ; 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

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

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

test-plumed:
	@(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:
	$(call download_and_unpack,$(PLUMED),$(PLUMED_URL),PLUMED,plumed)
	touch uncompress-plumed

patch-plumed:
	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: 
	$(call download_and_unpack,$(SAX),$(SAX_URL),SaX,$(SAX))
	touch uncompress-sax

configure-sax: 
	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: 
	$(call download_and_unpack,$(WEST),$(WEST_URL),West,WEST)
	touch uncompress-west

###########################################################
# SternheimerGW
###########################################################

SternheimerGW: extlibs uncompress-SternheimerGW
	if test -d ../SternheimerGW; then \
	  cd ../SternheimerGW; $(MAKE); \
	fi
	touch ./make-SternheimerGW

uncompress-SternheimerGW: 
	$(call download_and_unpack,$(SternheimerGW),$(SternheimerGW_URL),SternheimerGW,SternheimerGW)
	touch uncompress-SternheimerGW


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

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

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

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

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 ./configure.h

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

SternheimerGW_clean:
	if test -d ../SternheimerGW; then \
	  cd ../SternheimerGW ; $(MAKE) clean ; \
	fi
	- rm -rf ./make-SternheimerGW

SternheimerGW_veryclean: SternheimerGW_clean
	if test -d ../SternheimerGW; then (rm -R -f ../SternheimerGW); fi
	if test -d ../$(SternheimerGW); then (rm -R -f ../$(SternheimerGW)); fi
	- rm -rf ./uncompress-SternheimerGW

SternheimerGW_distclean:
	rm -f ../archive/$(SternheimerGW_archive)

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

#examples_clean:

#examples_veryclean: examples_clean

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

clean: w90_clean want_clean yambo_clean yambo-devel_clean plumed_clean \
	gipaw_clean west_clean SternheimerGW_clean test-suite_clean \
	sax_clean d3q_clean #examples_clean

veryclean: w90_veryclean want_veryclean yambo_veryclean yambo-devel_veryclean \
	plumed_veryclean gipaw_veryclean west_veryclean SternheimerGW_veryclean \
	test-suite_veryclean sax_veryclean d3q_veryclean #examples_veryclean

#distclean : veryclean
distclean: w90_distclean want_distclean yambo_distclean yambo-devel_distclean \
  test-suite_distclean gipaw_distclean west_distclean SternheimerGW_distclean \
	sax_distclean d3q_distclean

