
#===============================
# espresso
#===============================
#
include ../make.sys

#
# MAIN target
#
all: 
	$(MAKE) libblas_$(BLAS_LIBS_SWITCH) 
	$(MAKE) liblapack_$(LAPACK_LIBS_SWITCH) 
	$(MAKE) libelpa_internal 
	$(MAKE) libiotk 

# BLAS
libblas : libblas_$(BLAS_LIBS_SWITCH)

libblas_external : fake_blas_external

fake_blas_external :
	touch fake_libblas.a
	-rm fake_libblas.a

libblas_internal:
	if test ! -d ../BLAS; then \
	( gzip -dc ../archive/blas-1.tar.gz |(cd ../; tar -xvf -)); fi
	if test -e make_blas.inc; then \
	(cp make_blas.inc ../BLAS/make.inc; \
	cd ../BLAS; $(MAKE) $(BLASLIB)); else \
        (echo "no configuration file found for blas"; \
	echo "run configure from main QE dir"); fi

# LAPACK
liblapack : liblapack_$(LAPACK_LIBS_SWITCH) 

liblapack_external : fake_lapack_external

fake_lapack_external :
	touch fake_liblapack.a
	-rm fake_liblapack.a

liblapack_internal:
	if test ! -d ../lapack-3.2; then \
	( gzip -dc ../archive/lapack-3.2.tar.gz | (cd ../; tar -xvf -)); fi
	if test -e make_lapack.inc; then \
	(cp make_lapack.inc ../lapack-3.2/make.inc; \
	cp Makefile_lapack ../lapack-3.2/SRC/Makefile; \
        cp Makefile_lapack_testing_lin ../lapack-3.2/TESTING/LIN/Makefile; \
	cd ../lapack-3.2; $(MAKE) lapacklib); else \
        (echo "no configuration file found for lapack"; \
        echo "run configure from main QE dir"); fi
libiotk:
	if test ! -d ../S3DE; then \
	(gzip -dc ../archive/iotk-1.2.beta.tar.gz | (cd ../; tar -xvf -)) ; \
	if test -e Makefile_iotk; then \
        (cp Makefile_iotk ../S3DE/iotk/src/Makefile); fi; \
	if test -e iotk_config.h; then \
	(cp iotk_config.h ../S3DE/iotk/include/iotk_config.h); fi; fi
	cd ../S3DE/iotk/src; $(MAKE) lib+util; 
	cd ../bin; ln -fs ../S3DE/iotk/tools/iotk .; \
	ln -fs ../S3DE/iotk/src/iotk.x .; \
	ln -fs ../S3DE/iotk/src/iotk_print_kinds.x .; \
	cd ../; ln -fs S3DE/iotk iotk
	
# ELPA
libelpa : libelpa_$(ELPA_LIBS_SWITCH)

libelpa_ : fake_elpa_external
libelpa_disabled : fake_elpa_external

fake_elpa_external :
	touch fake_libelpa.a
	-rm fake_libelpa.a

libelpa_enabled:
	if test ! -d ../ELPA; then \
	( gzip -dc ../archive/ELPA-0.3.tar.gz |(cd ../; tar -xvf -)); fi
	cd ../ELPA; $(MAKE)
	
###################################
# cleaning
###################################
# each lib independently
blas_clean:
	if test -d ../BLAS; then (cd ../BLAS; $(MAKE) clean); fi
blas_veryclean:
	if test -d ../BLAS; then (rm -R -f ../BLAS); fi
lapack_clean:
	if test -d ../lapack-3.2; then (cd ../lapack-3.2; $(MAKE) clean); fi
lapack_veryclean:
	if test -d ../lapack-3.2; then (rm -R -f ../lapack-3.2); fi
elpa_clean:
	if test -d ../ELPA; then (cd ../ELPA; $(MAKE) clean); fi
elpa_veryclean:
	if test -d ../ELPA; then (rm -R -f ../ELPA); fi
iotk_clean:
	if test -d ../S3DE; then (cd ../S3DE; $(MAKE) clean); fi
	if test -d ../iotk; then (rm -R -f ../iotk); fi
	if test -e ../S3DE/iotk/src/iotk.x; then \
	rm -f ../S3DE/iotk/src/iotk.x; fi
	(rm -f ../bin/iotk; \
	rm -f ../bin/iotk.x; \
	rm -f ../bin/iotk_print_kinds.x)
iotk_veryclean:
	if test -d ../S3DE; then (rm -R -f ../S3DE); fi
	if test -d ../iotk; then (rm -R -f ../iotk); fi
	if test -e ../S3DE/iotk/src/iotk.x; then (rm -f ../S3DE/iotk/src/iotk.x); fi
	rm -f ../bin/iotk; \
	rm -f ../bin/iotk.x; \
	rm -f ../bin/iotk_print_kinds.x; \
	rm -f ../iotk

# general cleaning
clean: blas_clean lapack_clean elpa_clean iotk_clean
veryclean: blas_veryclean lapack_veryclean elpa_veryclean iotk_veryclean
