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

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

CGNSLIB = $(CGNSDIR)/$(LIBCGNS)

COPTS  = $(CFLAGS) -I$(CGNSDIR)
FOPTS  = $(FFLAGS) -I$(CGNSDIR)
LDLIBS = $(CGNSLIB) $(BUILDLIBS)

#----------

CALL = \
	cgioc$(EXE) \
	elemtest$(EXE) \
	test_exts$(EXE) \
	test_goto$(EXE) \
	test_multifam$(EXE) \
	test_partial$(EXE) \
	test_ver31$(EXE) \
	write_array$(EXE) \
	write_links$(EXE) \
	write_bcdata$(EXE) \
	write_test$(EXE) \
	write_zones$(EXE) \
	write_rind$(EXE)

F_TESTS = \
	cgiof_F03$(EXE) \
	cgwrite_F03$(EXE) \
	cgread_F03$(EXE) \
	cgzconn_F03$(EXE) \
	cgsubreg_F03$(EXE)

FALL = $(F_TESTS)

CALL64 = test64c$(EXE)
FALL64 = test64f$(EXE) test64f_F03$(EXE)

ALL = dbtest$(EXE) open_cgns$(EXE) $(CALL) @FALL@
ALL64 = $(CALL64) @FALL64@

TESTS = $(CALL) @FALL@

#----------

all : $(ALL)
fortran : $(FALL)
test64 : $(ALL64)

#----------

test : $(TESTS)
	@echo " "; \
	status=0; \
	echo "=== running tests ==="; \
	 for x in $(TESTS) ; do \
	   if `./$$x >/dev/null 2>&1` ; then \
	     printf "$$x ... $(OK_COLOR) PASSED $(NO_COLOR) \n"; \
	   else \
	     printf "$$x ... $(ERROR_COLOR) *** FAILED *** $(NO_COLOR) \n"; \
		status=1; \
	   fi; \
	 done; \
	 echo "=== finished ==="; \
	 exit $$status;

#----------

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

#----------

dbtest$(EXE) : dbtest.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ dbtest.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
dbtest.$(O) : dbtest.c utils.h
	$(CC) $(COPTS) -c dbtest.c

#----------

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

#----------

open_cgns$(EXE) : open_cgns.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ open_cgns.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
open_cgns.$(O) : open_cgns.c utils.h
	$(CC) $(COPTS) -c open_cgns.c

#----------

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

#----------

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

#----------

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

#----------

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

#----------

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

#----------

write_array$(EXE) : write_array.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_array.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_array.$(O) : write_array.c utils.h
	$(CC) $(COPTS) -c write_array.c

#----------

write_links$(EXE) : write_links.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_links.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_links.$(O) : write_links.c utils.h
	$(CC) $(COPTS) -c write_links.c

#----------

write_bcdata$(EXE) : write_bcdata.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_bcdata.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_bcdata.$(O) : write_bcdata.c utils.h
	$(CC) $(COPTS) -c write_bcdata.c

#----------

write_test$(EXE) : write_test.c $(CGNSLIB)
	$(CC) $(COPTS) $(CEOUT)$@ write_test.c $(LDLIBS) $(CLIBS) -lm
	$(STRIP) $@

#----------

write_zones$(EXE) : write_zones.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ write_zones.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
write_zones.$(O) : write_zones.c utils.h
	$(CC) $(COPTS) -c write_zones.c

#----------

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

#----------

cgiof$(EXE) : cgiof.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgiof.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgiof_F03$(EXE) : cgiof_f03.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgiof_f03.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgwrite$(EXE) : cgwrite.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgwrite.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgwrite_F03$(EXE) : cgwrite_f03.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgwrite_f03.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgread$(EXE) : cgread.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgread.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgread_F03$(EXE) : cgread_f03.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgread_f03.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgzconn$(EXE) : cgzconn.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgzconn.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgzconn_F03$(EXE) : cgzconn_f03.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgzconn_f03.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgsubreg$(EXE) : cgsubreg.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgsubreg.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

cgsubreg_F03$(EXE) : cgsubreg_f03.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ cgsubreg_f03.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@
#----------

test64c$(EXE) : test64c.$(O) utils.$(O) $(CGNSLIB)
	$(CC) $(CFLAGS) $(CEOUT)$@ test64c.$(O) utils.$(O) $(LDLIBS) $(CLIBS)
	$(STRIP) $@
test64c.$(O) : test64c.c utils.h
	$(CC) $(COPTS) -c test64c.c

#----------

test64f$(EXE) : test64f.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test64f.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

test64f_F03$(EXE) : test64f_f03.F $(CGNSLIB)
	$(F77) $(FOPTS) $(FEOUT)$@ test64f_f03.F $(LDLIBS) $(FLIBS)
	$(STRIP) $@

#----------

utils.$(O) : utils.c utils.h
	$(CC) $(COPTS) -c utils.c

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

allclean : clean
	-$(RM) $(ALL) $(FALL) $(ALL64)
	-$(RM) *.cgns *.cgio

