# -*- mode: Makefile -*-
# this makefile is run from a subdirectory, hence an extra ../ in the paths
VPATH = $(chapter_srcdir)
EXPECTED ?= test.out.expected
include ../../../../include/config.Makefile
all: check
# we depend on "diff" accepting some options that perhaps only GNU diff does
check: test.out.expected.trim test.out.trim
	@if ! diff -B -b -u test.out.expected.trim test.out.trim ;\
	then echo 'make[$(MAKELEVEL)]: *''** test failed: [Macaulay2/tests/ComputationsBook/$(shell basename $(chapter_srcdir))]' ;\
	     false ;\
	fi
diff:
	diff -B -b -u test.out.expected.trim test.out.trim

LIMIT :=
ifeq (@ULIMIT_T@,yes)
LIMIT += ulimit -t 1200 ;
endif
ifeq (@ULIMIT_V@,yes)
LIMIT += ulimit -v 1600000 ;
endif
ifeq (@ULIMIT_M@,yes)
LIMIT += ulimit -m 800000 ;
endif
ifeq (@ULIMIT_S@,yes)
LIMIT += ulimit -s 8192 ;
endif


PATHJOIN := 'path = join({"./","$(chapter_srcdir)/"},path)'
M2CMD := nice -18 @pre_exec_prefix@/bin/M2 --stop -q -e $(PATHJOIN) --print-width 0 --silent
EXIT := -e "exit 0"
INPUT = -e 'input "$<"'

book_patterns := $(chapter_srcdir)/../patterns
chapter_patterns := $(chapter_srcdir)/patterns

test.out.trim: test.out $(book_patterns) $(chapter_patterns)
	@<$< sed -f $(book_patterns) -f $(chapter_patterns) >$@

test.out.expected.trim: $(EXPECTED) $(book_patterns) $(chapter_patterns)
	@<$< sed -f $(book_patterns) -f $(chapter_patterns) >$@

test.out: test.m2 always
	@$(LIMIT) $(M2CMD) $(INPUT) $(EXIT) >test.out.tmp
	@mv test.out.tmp test.out

clean:; rm -f *.trim

distclean::clean
distclean::; rm -f Makefile

# capture output from the chapter's code with M2 version 0.9.2 as a standard of comparison:
# this requires an old binary, but we run it only once and save the output
OLDM2 = M2-0.9.2
OLDM2CMD := time nice -18 $(OLDM2) -s -q -e$(PATHJOIN)
capture: test.oldout test.oldvalues
test.oldvalues test.oldout : chapter.m2 ../capture.m2 ../Makefile.chapter
	$(LIMIT) $(OLDM2CMD) -e'load "$(chapter_srcdir)/../capture.m2"; input "$<"; exit 0'  >test.oldout.tmp </dev/null
	mv test.oldout.tmp test.oldout

# Local Variables:
# compile-command: "make -C $M2BUILDDIR/Macaulay2/tests/ComputationsBook "
# End:
