ifeq ($(MAKELEVEL),0)
$(error Sorry, this makefile is not intended to be \
used directly. Please use the makefile in the toplevel \
directory of the ApectC++ source tree instead)
endif

ACTESTS = Advice AdviceVirtualFct That StaticTrace Context NameMangling \
          Forward StandAloneCalls UnknownTypeCheck AccessControl Result \
          ArrayMangling MatchExpr ConsDesAdvice NestedClassFuncAdvice \
          MacroCall CallAdvice CallOnRef TemplateExtCall Bug190 Bug191 Bug192 \
	  Order NonInlineIntros JPType PragmaOnce Bug207 Bug209 \
	  AbstractDerivedAspect PragmaOnceObserver ThatConst PrivateResult \
          VoidArg ArrayWrapper Bug216 Bug228 ExecAdviceRecursive ExternC \
	  ThatContext IntroIncludes Bug234 Bug234-2 Bug235 Bug239 \
	  ExecAdviceNewDelete CallPostfixOp STLString CFlowCycle \
	  IntroInBase NonInlineIntroInclude Bug267 Bug268 Bug271 MatchVirtual \
	  PctInClass IntroInAll Instantiation Slice NonInlineSliceInclude \
	  Empty \
	  RepoInvalid RepoVersion

ACMSGTESTS := RepoInvalid RepoVersion

ACNORUN := $(filter $(ACMSGTESTS), $(ACTESTS))

ACRUN   := $(filter-out $(ACNORUN), $(ACTESTS))

STDOUT_FILE := stdout.txt
STDERR_FILE := stderr.txt
ERROR_FILE :=  error.txt

all: $(addsuffix .make, $(ACRUN)) $(addsuffix .weave, $(ACNORUN))
	@if [ -f $(ERROR_FILE) ];then \
		echo -e "\n\n"; \
		echo "+---------+"; \
		echo "|ERRORS:  |"; \
		echo "+---------+"; \
		cat $(ERROR_FILE); \
	 else \
		echo -e "\n\n"; \
	 	echo "All tests OK"; \
	 fi;\
	 rm -f $(STDOUT_FILE) $(STDERR_FILE) $(ERROR_FILE)

ref:  $(addsuffix .ref, $(ACTESTS))

refclean:
	rm */feature.ref

run: $(addsuffix .run, $(ACRUN))

clean: $(addsuffix .clean, $(ACTESTS))


%.weave: %
	@unset LANG; \
	exec 6>&1; \
	exec 7>&2; \
	exec > $</feature.out; \
	exec 2> $</feature.err; \
	$(MAKE) -s EXT="$(EXT)" -C $< ; \
	exec 1>&6 6>&-; \
	exec 2>&7 7>&-; \
	cat $</feature.err >> $</feature.out; \
	rm $</feature.err; \
	exec 6>&1; \
	exec 7>&2; \
	exec > $(STDOUT_FILE); \
	exec 2> $(STDERR_FILE); \
	diff $</feature.out $</feature.ref; \
	RET=$$?; \
	exec 1>&6 6>&-; \
	exec 2>&7 7>&-; \
	if [ $$RET == 0 ];then \
		echo -n "."; \
	else \
		ERR_FLAG=D; \
	 	echo -n "[$$ERR_FLAG:$<]"; \
		echo -ne "\n-----------------------------------------------------------------------------\n" >> $(ERROR_FILE); \
		echo  -n "TEST: $<" >> $(ERROR_FILE); \
		echo -ne "\n-----------------------------------------------------------------------------\n" >> $(ERROR_FILE); \
		echo -e "STDOUT:\n--------" >> $(ERROR_FILE); \
		cat $(STDOUT_FILE) >> $(ERROR_FILE); \
		echo   >> $(ERROR_FILE); \
		echo -e "STDERR:\n--------" >> $(ERROR_FILE); \
		cat $(STDERR_FILE) >> $(ERROR_FILE); \
		echo   >> $(ERROR_FILE); \
	fi;


%.make: %
	@exec 6>&1; \
	exec 7>&2; \
	exec > $(STDOUT_FILE); \
	exec 2> $(STDERR_FILE); \
	$(MAKE) -s EXT="$(EXT)" -C $< ; \
	RET=$$?; \
	ERR_FLAG=C; \
	if [ $$RET == 0 ];then \
	 	$(MAKE) -s EXT="$(EXT)" -C $< diff; \
	 	RET=$$?; \
		ERR_FLAG=D; \
	fi; \
	exec 1>&6 6>&-; \
	exec 2>&7 7>&-; \
	if [ $$RET == 0 ];then \
		echo -n "."; \
	else \
	 	echo -n "[$$ERR_FLAG:$<]"; \
		echo -ne "\n-----------------------------------------------------------------------------\n" >> $(ERROR_FILE); \
		echo  -n "TEST: $<" >> $(ERROR_FILE); \
		echo -ne "\n-----------------------------------------------------------------------------\n" >> $(ERROR_FILE); \
		echo -e "STDOUT:\n--------" >> $(ERROR_FILE); \
		cat $(STDOUT_FILE) >> $(ERROR_FILE); \
		echo   >> $(ERROR_FILE); \
		echo -e "STDERR:\n--------" >> $(ERROR_FILE); \
		cat $(STDERR_FILE) >> $(ERROR_FILE); \
		echo   >> $(ERROR_FILE); \
	fi;




%.ref: %
	$(MAKE) -s EXT="$(EXT)" -C $< ref; 

%.run:	%
	cd $< && $(MAKE) run

%.clean: %
	cd $< && $(MAKE) clean

.PHONY: all clean run ref  


