# Makefile to create a main program that tests ReLaTIve on a Laplace Transforms Database, with a locally installed GSL
# Version 1.0

#***********************************************************************
#   Authors:                                                            
#       Luisa D'Amore - University of Naples, Federico II	           
#		Rosanna Campagna - University of Naples, Federico II	        
#		Valeria Mele - University of Naples, Federico II	            
#		Almerico Murli - SPACI - NAPLES                   	            
# 		Mariarosaria Rizzardi - University of Naples, Parthenope        
#************************************************************************

ifneq ($(CC),gcc)
CC     = gcc
endif

ifndef rootdir 
rootdir  = ..
endif

ifndef RELdir
RELdir  =  $(rootdir)/SRC
endif

ifndef dbdir
dbdir  =  ./database
endif

ifndef utildir
utildir  =  ./utility
endif

#######################################################################
### FILES to install of GSL library: eventually to change with yours ## 

gslloc  = $(PWD)/GSL

gsltar = ./gsl-1.15.tar.gz

gsldir = $(PWD)/gsl-1.15/

#######################################################################

ifndef gsldirinc
gsldirinc   = $(gslloc)/include
endif

ifndef gsldirlib
gsldirlib = $(gslloc)/lib
endif

ifndef CFLAGS
CFLAGS 	= -Wall -ansi -pedantic 
endif

ifndef RM
RM     = rm -f -r
endif


LIBS	= -Wl,-rpath,$(gsldirlib) -L. -lrelative -L$(dbdir) -ldatabase -L$(utildir) -lutil  -L$(gsldirlib) -lgsl -lgslcblas -lm

OBJ		= Driver-ArticleTests.o 
BIN		= Driver

all: $(BIN) 

gsl: $(gsltar)
	@echo "############################################################################"
	@echo "ReLaTIve ARTICLE TEST DRIVER: installing local GSL in $(gslloc) from $(gsltar)"
	@echo "############################################################################"
	@if [ ! -d "$(gslloc)" ] ; then\
	  echo "Creating directory $(gslloc)";\
	  mkdir -p "$(gslloc)"; \
	  echo "Extracting $(gsltar)"; \
	  tar xzf $(gsltar); \
	  echo "Configuring GSL istallation in $(gslloc)"; \
	  cd "$(gsldir)" && ./configure --prefix="$(gslloc)"; \
	  echo ; \
	  echo ; \
	  echo "Compiling GSL from $(gsldir)"; \
	  $(MAKE) -C "$(gsldir)"; \
	  echo ; \
	  echo ; \
	  echo "Installing GSL in $(gslloc)"; \
	  $(MAKE) install -C "$(gsldir)"; \
	  fi

util: gsl
	@echo
	@echo
	@echo "############################################################################"
	@echo "ReLaTIve ARTICLE TEST DRIVER: creating utility library"
	@echo "############################################################################"
	@$(MAKE) -C $(utildir) -f Makefile_gslloc
	
db: util 
	@echo
	@echo
	@echo "############################################################################"
	@echo "ReLaTIve ARTICLE TEST DRIVER: creating Transform/Inverse Databases library"
	@echo "############################################################################"
	@$(MAKE) -C $(dbdir) -f Makefile_gslloc
	
rel: $(RELdir)/ck_calc.c $(RELdir)/ReLaTIve.h $(RELdir)/ReLaTIve.c
	@echo
	@echo
	@echo "############################################################################"
	@echo "ReLaTIve ARTICLE TEST DRIVER: creating ReLaTIve library"
	@echo "############################################################################"
	@$(MAKE) -f Makefile_REL 

$(OBJ): Driver-ArticleTests.c rel db
	@echo
	@echo
	@echo "############################################################################"
	@echo "ReLaTIve ARTICLE TEST DRIVER: compiling main program to test ReLaTIve on database..."
	@echo "############################################################################"
	@$(CC) $(CFLAGS) -I$(dbdir) -I$(gsldirinc) -I$(RELdir) -I$(utildir) -c Driver-ArticleTests.c 
	
$(BIN): $(OBJ) 
	@echo
	@echo
	@echo "############################################################################"
	@echo "ReLaTIve ARTICLE TEST DRIVER: linking main program to test ReLaTIve on database..."
	@echo "############################################################################"
	@$(CC) $(CFLAGS) -o $(BIN) $(OBJ) $(LIBS)
	@${RM} $(OBJ) 
	@echo
	@echo
	@echo "############################################################################"
	@echo "############################################################################"
	@echo "############################################################################"
	@echo "############################################################################"
	@echo "Test driver installed!!!"
	@echo "Now you can run the program $(BIN) or a script in this directory."
	@echo "(Consult the DRIVER_userguide.pdf file to know about the arguments)"
	@echo "############################################################################"
	@echo "############################################################################"
	@echo "############################################################################"
	@echo "############################################################################"

clean: 
	@echo "ReLaTIve ARTICLE TEST DRIVER: deleting existing executable and output files..."
	@${RM} $(BIN) *.txt 
	@$(MAKE) clean -C $(dbdir) -f Makefile_gslloc
	@$(MAKE) clean -C $(utildir) -f Makefile_gslloc
	@$(MAKE) clean -f Makefile_REL 

cleangsl: 
	@echo "############################################################################"
	@echo "ReLaTIve ARTICLE TEST DRIVER: removing the GSL library installed in $(gslloc)!!!"
	@echo "############################################################################"
	@${RM} $(gsldir) -r
	@${RM} $(gslloc) -r
	






	
	
