#
# ${R_HOME}/tests/Examples/Makefile

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

top_builddir = ../..
subdir = tests/Examples

include $(top_builddir)/Makeconf

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
DISTFILES = Makefile.in

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

EX_IN_BASE = $(R_PKGS_BASE:=-Ex.R)
EX_OUT_BASE = $(EX_IN_BASE:.R=.Rout)

EX_IN_RECOMMENDED = $(R_PKGS_RECOMMENDED_EXPANDED:=-Ex.R)
EX_OUT_RECOMMENDED = $(EX_IN_RECOMMENDED:.R=.Rout)

RDIFF = $(top_builddir)/bin/R CMD Rdiff
RVAL_IF_DIFF = 0

.SUFFIXES:
.SUFFIXES: .R .Rout

all: Makefile Makedeps test-Examples 

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
Makedeps: Makefile
	@(for p in $(R_PKGS_BASE); do \
	    dep="$${p}-Ex.Rout: $${p}-Ex.R"; \
	    dep="$${dep} \$$(top_builddir)/library/$${p}/R/$${p}"; \
	    tmp="library/$${p}/libs/$${p}" ; \
	    if test -f $(top_builddir)/$${tmp}$(SHLIB_EXT); then \
	      dep="$${dep} \$$(top_builddir)/$${tmp}\$$(SHLIB_EXT)"; \
	    fi; \
	    dep="$${dep} \$$(FORCE)"; \
	    echo $${dep}; \
	  done) >> Makefile
	@touch $@
test-Examples: test-Examples-Base
test-Examples-Base: Makefile
	@$(MAKE) Makedeps
	@$(MAKE) $(EX_OUT_BASE)

## <NOTE>
## We do *not* want this to be added to test-Examples conditional on
## @USE_RECOMMENDED_PACKAGES_TRUE@.
## 'make check-all' is used for running test-Examples-Recommended in
## addition to 'make check'.
test-Examples-Recommended: test-Examples-Base
	@$(MAKE) $(EX_OUT_RECOMMENDED)
## </NOTE>

R:
	@cd $(top_builddir) && $(MAKE) R

.R.Rout:
	@if test -f $@; then mv $@ $@.prev; fi
	@echo $(ECHO_N) "running code in '$<' ...$(ECHO_C)"
	@$(R) < $< > $@ 2>&1 || (mv $@ $@.fail && exit 1)
	@echo "$(ECHO_T) OK"
	@if test -f $@.prev; then \
	  mv $@ $@.fail; \
	  echo $(ECHO_N) "comparing '$@' to '$@.prev' ...$(ECHO_C)"; \
	  $(RDIFF) $@.fail $@.prev $(RVAL_IF_DIFF) || exit 1; \
	  mv $@.fail $@; \
	  echo "$(ECHO_T) OK"; \
	fi

## <NOTE>
## We need to update an ex_in file if it is older than any Rd file it
## contains examples from, or older than massage-Examples.pl.  As we do
## not have the Rd files as explicit dependencies (which would currently
## be impossible for recommended packages), we cannot use Make to figure
## out the need to update ex_in files ... hence we do things ourselves,
## and force these computations.
## The ugly 'find | grep' construct is necessary because 'test -nt' is
## not portable.  The pipeline will succeed if the target exists and is
## newer than the corresponding DESCRIPTION file in the package.  If the
## target does not exist, then find will fail; if it exists and is
## older, then find will succeed but output nothing so that the grep
## will fail.
## </NOTE>
$(EX_IN_BASE): FORCE
	@(pkg=`basename $@ -Ex.R`; \
	if find $@ -newer $(top_srcdir)/share/perl/massage-Examples.pl \
	    -print 2> /dev/null | grep $@ > /dev/null; then \
	  target_is_newer_than_massage_Ex=yes; \
	else \
	  target_is_newer_than_massage_Ex=no; \
	fi; \
	if $(PERL) $(top_srcdir)/tools/Rdnewer.pl \
	    "$(top_srcdir)/src/library/$${pkg}" "$@"; then \
	  target_is_newer_than_Rdfiles=no; \
	else \
	  target_is_newer_than_Rdfiles=yes; \
	fi; \
	if test "$${target_is_newer_than_massage_Ex}" = no \
	    || test "$${target_is_newer_than_Rdfiles}" = no; then \
	  echo "collecting examples for package '$${pkg}' ..."; \
	  (cd $(top_builddir)/src/library && \
	    OPTS="--txt --example" $(MAKE) R_PKGS_BASE="$${pkg}" DOCS); \
	  f=$${TMPDIR-/tmp}/R$$$$; \
	  $(PERL) $(top_srcdir)/share/perl/massage-Examples.pl $${pkg} \
	    $(top_builddir)/library/$${pkg}/R-ex > $${f}; \
	  $(top_srcdir)/tools/move-if-change $${f} $@; \
	  touch $@; \
	fi)

$(EX_IN_RECOMMENDED): FORCE
	@(pkg=`basename $@ -Ex.R`; \
	if find $@ -newer $(top_srcdir)/share/perl/massage-Examples.pl \
	    -print 2> /dev/null | grep $@ > /dev/null; then \
	  target_is_newer_than_massage_Ex=yes; \
	else \
	  target_is_newer_than_massage_Ex=no; \
	fi; \
	if find $@ -newer $(top_builddir)/library/$${pkg}/DESCRIPTION \
	    -print 2> /dev/null | grep $@ > /dev/null; then \
	  target_is_newer_than_Rdfiles=yes; \
	else \
	  target_is_newer_than_Rdfiles=no; \
	fi; \
	if test "$${target_is_newer_than_massage_Ex}" = no \
	    || test "$${target_is_newer_than_Rdfiles}" = no; then \
	  echo "collecting examples for package '$${pkg}' ..."; \
	  f=$${TMPDIR-/tmp}/R$$$$; \
	  $(PERL) $(top_srcdir)/share/perl/massage-Examples.pl $${pkg} \
	    $(top_builddir)/library/$${pkg}/R-ex > $${f}; \
	  $(top_srcdir)/tools/move-if-change $${f} $@; \
	  touch $@; \
	fi)
FORCE:

$(EX_OUT_BASE) $(EX_OUT_RECOMMENDED): Makefile \
  $(top_builddir)/bin/R.bin \
  $(top_builddir)/library/base/R/base \
  $(top_builddir)/modules/lapack$(SHLIB_EXT)
base-Ex.Rout: $(top_builddir)/modules/vfonts$(SHLIB_EXT)

mostlyclean: clean
clean:
	-@rm -f *.R *.Rout *.Rd* *.ps *.tex *.dat* data foo* Makedeps
distclean: clean
	-@rm -f *.R*bak *.R*bakk *.R*prev .RData sink-examp.txt Makefile
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

## Automagically generated dependencies: