NO_COLOR=\033[0m
OK_COLOR=\033[32;01m
ERROR_COLOR=\033[31;01m

CGNSDIR = ..
include $(CGNSDIR)/make.defs

CGNSLIB = $(CGNSDIR)/$(LIBCGNS)

COPTS  = $(CFLAGS) -I$(CGNSDIR) -I$(CGNSDIR)/tests $(MPIINC) @BUILDOPTS@
FOPTS  = $(FFLAGS) -I$(CGNSDIR) $(MPIINC)
LDLIBS = $(CGNSLIB) $(BUILDLIBS)

#----------

CALL =	pcgns_ctest$(EXE) \
	cexample$(EXE) \
	benchmark$(EXE) \
	benchmark_hdf5$(EXE) \
	open_close$(EXE) \
        comm_test$(EXE) \
	test_base$(EXE) \
	test_unstructured$(EXE) \
	test_unstruc_quad$(EXE) \
	test_zone$(EXE) \
	test_general_readwrite$(EXE) \
        Metadata_Stressing$(EXE) \
	test_poly_unstructured$(EXE) \
	thesis_benchmark$(EXE)

FALL =	pcgns_ftest$(EXE) \
	ftest_zone$(EXE) \
	fexample$(EXE) \
	benchmark_hdf5_f90$(EXE) \
	test_mixed_par_ser$(EXE) \
	test_unstruc_quad_f90$(EXE) \
	test_poly_unstructured_f90$(EXE)

ALL   = $(CALL) @FALL@
TESTS = $(ALL)

TIMING_AVAIL:=$(shell /usr/bin/time -a -o CGNS_timing.txt -f "%e" pwd > /dev/null; echo $$?)
ifeq ("$(TIMING_AVAIL)" , "0")
  TIMING = /usr/bin/time -a -o CGNS_timing.txt -f "$$x %e" $(MPIEXEC) ./$$x >/dev/null 2>&1
  TIMING_ARGS = /usr/bin/time -a -o CGNS_timing.txt -f "$$x %e" $(MPIEXEC) ./$$x $$args >/dev/null 2>&1
else
  TIMING = $(MPIEXEC) ./$$x >/dev/null 2>&1
  TIMING_ARGS = $(MPIEXEC) ./$$x $$args >/dev/null 2>&1
endif

all : $(ALL)

#----------

test : $(TESTS)
	@echo " "; \
	status=0; \
	echo "=== running parallel tests ==="; \
	if test -f CGNS_timing.txt; then \
	  $(RM) CGNS_timing.txt; \
	fi; \
	padlimit=40; \
	pad=$$(printf '%*s' "$$padlimit"); \
	pad=$${pad// /.}; \
        padlength=32; \
	time=""; \
	for x in $(TESTS) ; do \
           args=""; \
           if test "$$x" = "Metadata_Stressing$(EXE)" ; then \
		for n in 1 2 ; do \
	           printf "$$x"; \
	           printf ' %*.*s' 0 $$((padlength - $${#x} )) "$$pad"; \
	           if `$(TIMING_ARGS)` ; then \
	             printf " [$(OK_COLOR)PASSED$(NO_COLOR)]"; \
                     if [ -f "CGNS_timing.txt" ]; then \
                       itime=$$(tail -n1 CGNS_timing.txt |  awk  '{print $$2}' | sed -e 's/$$/ sec/'); \
	               printf " %+12s" "$$itime";\
                     fi; \
                     printf "\n";\
	           else \
	              printf " [$(ERROR_COLOR)FAILED$(NO_COLOR)]\n"; \
                      status=`expr $$status + 1`; \
	            fi; \
	            args="-sec2"; \
		done;\
           else \
	     printf "$$x"; \
	     printf ' %*.*s' 0 $$((padlength - $${#x} )) "$$pad"; \
	     if `$(TIMING)` ; then \
	       printf " [$(OK_COLOR)PASSED$(NO_COLOR)]"; \
               if [ -f "CGNS_timing.txt" ]; then \
                 itime=$$(tail -n1 CGNS_timing.txt |  awk  '{print $$2}' | sed -e 's/$$/ sec/'); \
	         printf " %+12s" "$$itime";\
               fi; \
               printf "\n";\
	     else \
	       printf " [$(ERROR_COLOR)FAILED$(NO_COLOR)]\n"; \
               status=`expr $$status + 1`; \
	     fi; \
           fi;\
	 done; \
	 echo "=== finished ==="; \
         if [ $$status -ne 0 ]; then \
           printf "$(ERROR_COLOR)"; \
	   stat=1; \
         else \
           printf "$(OK_COLOR)"; \
	   stat=0; \
         fi; \
         printf "$$status tests failed $(NO_COLOR) \n"; \
	 exit $$stat;

#----------

pcgns_ctest$(EXE) : pcgns_ctest.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ pcgns_ctest.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

cexample$(EXE) : cexample.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ cexample.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

benchmark$(EXE) : benchmark.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ benchmark.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

benchmark_hdf5$(EXE) : benchmark_hdf5.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ benchmark_hdf5.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

open_close$(EXE) : open_close.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ open_close.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

comm_test$(EXE) : comm_test.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ comm_test.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_base$(EXE) : test_base.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_base.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_unstructured$(EXE) : test_unstructured.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_unstructured.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_unstruc_quad$(EXE) : test_unstruc_quad.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_unstruc_quad.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

test_zone$(EXE) : test_zone.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_zone.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@


#----------

test_general_readwrite$(EXE) : test_general_readwrite.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_general_readwrite.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@


#----------

test_poly_unstructured$(EXE) : test_poly_unstructured.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ test_poly_unstructured.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@


#----------

thesis_benchmark$(EXE) : thesis_benchmark.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ thesis_benchmark.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

pcgns_ftest$(EXE) : pcgns_ftest.F90 utilsf.$(O) $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ pcgns_ftest.F90 utilsf.$(O) $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

test_mixed_par_ser$(EXE) : test_mixed_par_ser.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test_mixed_par_ser.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

ftest_zone$(EXE) : ftest_zone.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ ftest_zone.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

fexample$(EXE) : fexample.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ fexample.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

benchmark_hdf5_f90$(EXE) : benchmark_hdf5_f90.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ benchmark_hdf5_f90.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

test_unstruc_quad_f90$(EXE) : test_unstruc_quad_f90.F90 $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test_unstruc_quad_f90.F90 $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

test_poly_unstructured_f90$(EXE) : test_poly_unstructured_f90.F90 utilsf.$(O) $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test_poly_unstructured_f90.F90 utilsf.$(O) $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

Metadata_Stressing$(EXE) : Metadata_Stressing.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ Metadata_Stressing.c $(LDLIBS) $(CLIBS)
	$(STRIP) $@

#----------

utilsf.$(O) : $(CGNSDIR)/tests/utilsf.F90
	$(F77) $(FOPTS) -c $(CGNSDIR)/tests/utilsf.F90

clean :
	-$(RM) *.$(O) *.mod *.MOD

allclean : clean
	-$(RM) $(ALL)
	-$(RM) *~ *.bak
	-$(RM) *.cgns *.cg?
	-$(RM) CGNS_timing.txt

