#
# This file is part of Mascot.
# Copyright (C) 2010-2012 Xavier Clerc.
#
# Mascot is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Mascot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

EXECUTABLE=../../_build/src/driver/mascot.byte

default: FORCE
	@echo 'Available targets:'
	@echo '  all              runs all tests'
	@echo '  one NAME=dir     runs the test whose name is passed'
	@echo '  clean            deletes log and result files'
	@echo '  log              print log for last execution'
	@echo '  new NAME=dir     creates a new test directory'

all: clean
	@for file in *; do \
	  if [ -d $$file ]; then \
	    $(MAKE) one NAME=$$file; \
	  fi \
	done 2>&1 | tee _log
	@echo ''
	@echo "`grep '^\*\*\*' _log | wc -l` error(s) in `grep '^Running tests for' _log | wc -l` test(s)"

one: FORCE
	@if [ -z $(NAME) ]; then echo "*** value for 'NAME' is missing"; exit 1; fi
	@echo "Running tests for '$(NAME)'..."
	@cd $(NAME) \
	  && $(EXECUTABLE) -config config -ignore ignore `ls *.reference | sed 's|\(.*\)\.reference$$|-\1 \1.result|' | xargs` *.ml* > result \
	  && if [ -f html.result ]; then \
	    mv html.result html.tmp; \
	    sed 's|Mascot report / .*|Mascot report|g' html.tmp > html.result; \
	    rm -f html.tmp; \
	  fi \
	  && for file in *.reference; do \
	       (diff -q $$file `basename $$file reference`result > /dev/null 2> /dev/null) \
	       || echo "*** invalid $(NAME)/`basename $$file reference`result"; \
	     done \
	  && ((diff -q reference result > /dev/null 2> /dev/null) \
	     || echo "*** invalid $(NAME)/result") \
	  && cd ..

promote: FORCE
	@if [ -z $(NAME) ]; then echo "*** value for 'NAME' is missing"; exit 1; fi
	@cd $(NAME) \
	  && for file in *.reference; do \
	    cp `basename $$file reference`result $$file; \
	  done \
	&& cd ..

clean: FORCE
	@rm -f _log
	@rm -f */*.result */result

log: FORCE
	@cat _log
	@echo ''
	@echo "`grep '^\*\*\*' _log | wc -l` error(s) in `grep '^Running tests for' _log | wc -l` test(s)"

new: FORCE
	@if [ -z $(NAME) ]; then echo "*** value for 'NAME' is missing"; exit 1; fi
	@mkdir -p $(NAME)
	@cd $(NAME) \
	  && touch config ignore reference \
	  && for file in checkstyle csv html summary text xml; do \
	    touch $$file.reference; \
	  done \
	  && cd ..
FORCE:
