#
# ${R_HOME}/tests/Makefile

VPATH = @srcdir@
srcdir = @srcdir@
top_srcdir = @top_srcdir@

top_builddir = ..
subdir = tests

include $(top_builddir)/Makeconf

SUBDIRS = Examples

R = LC_ALL=C $(top_builddir)/bin/R --vanilla

TESTsrc-strict-1 = \
	arith-true.R \
	arith.R \
	d-p-q-r-tests.R \
	eval-etc.R \
	is-things.R \
	primitive-funs.R \
	mode-methods.R \
	simple-true.R
TESTsrc-strict-auto = \
	isas-tests.R
TESTsrc-sloppy-1 = \
	print-tests.R
TESTsrc-sloppy-auto = \
	no-segfault.R

TESTsrc-strict = $(TESTsrc-strict-1) $(TESTsrc-strict-auto)
TESTsrc-sloppy = $(TESTsrc-sloppy-1) $(TESTsrc-sloppy-auto)
# These two "hand made":
TESTsrc-Rdiff = $(TESTsrc-strict) $(TESTsrc-sloppy-1)
TESTsrc-NO-diff = no-segfault.R

TESTsrc = $(TESTsrc-strict) $(TESTsrc-sloppy)
TESTsrc-auto = $(TESTsrc-strict-auto) $(TESTsrc-sloppy-auto)

TESTout-strict = $(TESTsrc-strict:.R=.Rout)
TESTout-sloppy = $(TESTsrc-sloppy:.R=.Rout)
TESTout = $(TESTsrc:.R=.Rout)
TESTout-Rdiff = $(TESTsrc-Rdiff:.R=.Rout)
TESTout-NO-diff = $(TESTsrc-NO-diff:.R=.Rout)

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
DISTFILES = Makefile.in README Rdiff \
	mode-methods.R make*.R \
	$(TESTsrc) \
	$(TESTsrc:.R=.Rout.save)

.SUFFIXES:
.SUFFIXES: .R .Rout

all: Makefile test-All

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	@cd $(top_builddir) && \
	  CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= \
	  $(SHELL) ./config.status
Makedeps: Makefile
	@(for f in $(TESTsrc-Rdiff); do \
	    echo "$${f}out: $${f} \$$(srcdir)/$${f}out.save \$$(FORCE)"; \
	  done) >> Makefile
	@touch $@
FORCE:

$(TESTsrc-auto): %.R : $(srcdir)/make-%.R
	$(R) < $< > /dev/null

$(TESTout-Rdiff): %.Rout : %.R
	@rm -f $@.fail
	@(SRCDIR=$(srcdir); export SRCDIR; \
	  echo "$(R) < $< > $@"; \
	  $(R) < $< > $@)
	@echo @ECHO_N@ \
	  "Comparing \`$@' to \`$(srcdir)/$@.save' ...@ECHO_C@"
	@$(srcdir)/Rdiff $@ $(srcdir)/$@.save $(RVAL_IF_DIFF) || \
	  (mv $@ $@.fail && exit 1)
	@echo "@ECHO_T@ OK"

$(TESTout-NO-diff): %.Rout : %.R
	$(R) < $< > $@

test-All: Makefile
	@$(MAKE) test-Examples
	@$(MAKE) test-Specific
test-Examples:
	@(cd Examples && $(MAKE) $@)
test-Specific-strict: $(TESTout-strict)
test-Specific-sloppy: $(TESTout-sloppy)
test-Specific:
	@echo "updating test dependencies"
	@$(MAKE) Makedeps
	@echo "running strict specific tests"
	@$(MAKE) test-Specific-strict RVAL_IF_DIFF=1
	@echo "running sloppy specific tests"
	@$(MAKE) test-Specific-sloppy RVAL_IF_DIFF=0

# Note: we need to increase nsize (in fact, twice the default) in what
# follows to make sure we can load the base package twice.
test-undoc:
	@for p in $(R_PKGS); do \
	  echo "Undocumented objects in package \`$${p}':"; \
	  echo "undoc(\"$${p}\")" | $(R) --nsize 500000 | grep "^ *\\["; \
	  echo; \
	done

$(TESTout): Makefile stamp-R

stamp-R: $(top_builddir)/bin/R.X11 $(top_builddir)/library/base/R/base
	@cd $(top_builddir) && $(MAKE) R
	@touch $@

mostlyclean: clean
clean:
	-(cd Examples; $(MAKE) $@)
	rm -f stamp-R Makedeps Rplots.* *.Rd
distclean: clean
	@rm -f $(TESTout)
	-(cd Examples; $(MAKE) $@)
	@rm -f Makefile $(TESTsrc-auto)
maintainer-clean: distclean

distdir: $(DISTFILES)
	@for f in $(DISTFILES); do \
	  test -f $(distdir)/$${f} \
	    || ln $(srcdir)/$${f} $(distdir)/$${f} 2>/dev/null \
	    || cp -p $(srcdir)/$${f} $(distdir)/$${f}; \
        done
	@for d in $(SUBDIRS); do \
	  test -d $(distdir)/$${d} \
	    || mkdir $(distdir)/$${d} \
	    || exit 1; \
	  chmod 755 $(distdir)/$${d}; \
	  (cd $${d} && $(MAKE) distdir) \
	    || exit 1; \
        done

## Automagically generated dependencies:

