include $(ISISROOT)/make/isismake.utils

DIRONLY = $(subst /,,$(shell ls -d */))
CVS := CVS
TESTS = $(filter-out $(CVS), $(DIRONLY))

# This makefile does parallel builds with serialized output. Let's go ahead and
# set this up.

# We want targets for each possible parallelized build. The parallel builds work
# by parallelizing non-dependant targets. To accomplish our task, let's set up
# the arrays of dependencies for some of our build types.
CASESTEST := $(TESTS:%=%-test)

# For serializing output, we have log and lock files. Let's define some helpful
# targets for managing these.

# This deletes the lock file. This should not be called when anything could be
# using the file.
cleanuplockfile:
	BUILDNAME=`dirname $$ISISROOT`;                                        \
	BUILDNAME=`basename $$BUILDNAME`;                                      \
	if [ -f ".isisbuild.$$USER.$$BUILDNAME" ]; then                        \
	  $(RM) ".isisbuild.$$USER.$$BUILDNAME";                               \
	fi;

# When we make casename-print it should print the cases' log. This acquires the
# appropriate lock before printing. There is a 30s timeout on the lock before 
# breaking it.
%-print:
	CASENAME=`$(ECHO) $@ | $(SED) 's/-print//'`;                           \
	                                                                       \
	BUILDNAME=`dirname $$ISISROOT`;                                        \
	BUILDNAME=`basename $$BUILDNAME`;                                      \
	trap "rm -f .isisbuild.$$USER.$$BUILDNAME; exit $$?" INT TERM EXIT;    \
	                                                                       \
	LOCKED=0;                                                              \
	COUNTER=0;                                                             \
	while [ "$$LOCKED" -eq "0" ]; do                                       \
	  if [ "$$COUNTER" -gt 300 ]; then                                     \
	    $(RM) .isisbuild.$$USER.$$BUILDNAME;                               \
	    COUNTER=0;                                                         \
	    echo $(CURTIMESTAMP) "Warning: Acquiring lock to print to screen " \
	         "timed out";                                                  \
	  fi;                                                                  \
                                                                               \
	  ( set -o noclobber &&                                                \
	    echo "$$$$" > .isisbuild.$$USER.$$BUILDNAME ) &>/dev/null;         \
	  if [ "$$?" -eq "0" ]; then                                           \
	    LOCKED=1;                                                          \
	  else                                                                 \
	    COUNTER=`expr $$COUNTER + 1`;                                      \
	    sleep 0.1;                                                         \
	  fi;                                                                  \
	done;                                                                  \
	                                                                       \
	if [ -f .$$CASENAME.stdall.log ]; then                                 \
	  $(CAT) .$$CASENAME.stdall.log;                                       \
	fi;                                                                    \
	                                                                       \
	$(RM) .isisbuild.$$USER.$$BUILDNAME;                                   \
	                                                                       \
	$(MAKE) $$CASENAME-cleanupprint;


# Cleanup log files if they exist. This should be called BEFORE the files are
# redirected to and AFTER they are printed.
%-cleanupprint:
	CASENAME=`$(ECHO) $@ | $(SED) 's/-cleanupprint//'`; \
	                                                    \
	if [ -f ".$$CASENAME.stdall.log" ]; then            \
	  $(RM) ".$$CASENAME.stdall.log";                   \
	fi;

# Our overhead targets are now handled.


#----------------------------------------------------------------------------
# Target = help
# Dependencies = 
#
# Displays a list of targets and their descriptions.
#----------------------------------------------------------------------------
help:
	echo "Isis Make Tests Commands"
	echo "------------------------ "
	echo "make test                   : Runs and displays results for all of the tests"
	echo "make newtest TEST=testname  : Creates a new test named testname"

#----------------------------------------------------------------------------
# Target = tests
# Dependencies = 
#
# Iteretates over all of the tests and runs them.  If the test
# has no makefile or there is no tests a message is
# printed saying so.
#----------------------------------------------------------------------------
test: $(CASESTEST)
	$(MAKE) cleanuplockfile; \
	if [ "$CASESTEST" = "" ]; then \
	  echo $(CURTIMESTAMP) "no tests"; \
	fi;

%-test:
	CASENAME=`$(ECHO) $@ | $(SED) 's/-test//'`;                            \
	$(MAKE) $$CASENAME-cleanupprint;                                       \
	                                                                       \
	if [ -f "Makefile" ]; then                                             \
	  echo "" > .$$CASENAME.stdall.log;                                    \
	  $(MAKE) --directory=$$CASENAME test CASE=$$CASENAME BLANKS=$(BLANKS) \
	    1>>.$$CASENAME.stdall.log 2>.$$CASENAME.stdall.log;                \
	else                                                                   \
	  printf "$(BLANKS)No makefile in case [$$CASENAME]"                   \
	    1>>.$$CASENAME.stdall.log;                                         \
	fi;                                                                    \
	                                                                       \
	$(MAKE) $$CASENAME-print;

force: ;

#----------------------------------------------------------------------------
# Target = newtest
# Dependencies = variable TEST 
#
# Creates a new test in the current directory.  Relies on 
# the TEST variable to create a new folder that will contain 
# the new test.  If the folder does not already exist then it is
# created and the right Makefile is copied into that directory
# and the test is told to create what it needs. 
#----------------------------------------------------------------------------
newtest:
	if [ "$(TEST)" = "" ]; \
	then \
	  echo $(CURTIMESTAMP) "TEST needs to be set to create a new test"; \
	elif [ -d "$(TEST)" ]; \
	then \
	    echo $(CURTIMESTAMP) "The test already exists"; \
	else \
	  $(MKDIR) $(TEST); \
	  cd $(TEST); \
	  $(CP) $(ISISROOT)/make/Makefile.tsts Makefile; \
	  $(MAKE) dirs; \
	fi

#----------------------------------------------------------------------------
# Target = clean
#
# Removes the output from any previous tests
#----------------------------------------------------------------------------
clean:
	if [ "$(TESTS)" != "" ]; \
	then \
	  for i in $(TESTS); do \
	    cd $$i; \
	    if [ -f "Makefile" ]; \
	    then \
	      $(MAKE) clean; \
	    fi ; \
	    cd ..; \
	  done \
	fi
