#
# ${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  Makefile.win Makefile.install

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

## principle is that we can run all the examples in a package
## starting with just that package and 'datasets'
## R = R_DEFAULT_PACKAGES=datasets $(top_builddir)/bin/R --vanilla
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:=-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.R.  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>
## FIXME: Why does this make txt as well as example?
$(EX_IN_BASE): FORCE
	@(pkg=`basename $@ -Ex.R`; \
	if find $@ -newer $(top_srcdir)/share/R/massage-examples.R \
	    -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 $(R_EXE) -e \
	  "tools:::.Rdnewer(\"$(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 && \
	    $(MAKE) R_CMD_BUILD_HELP_OPTS="--txt --example" \
	    R_PKGS_BASE="$${pkg}" Rdfiles); \
	  f=$${TMPDIR-/tmp}/R$$$$; \
	  LC_ALL=C $(R_EXE) < $(top_srcdir)/share/R/massage-examples.R \
	    --args $${pkg} $(top_builddir)/library/$${pkg}/R-ex > $${f}; \
	  $(SHELL) $(top_srcdir)/tools/move-if-change $${f} $@; \
	  touch $@; \
	fi)

$(EX_IN_RECOMMENDED): FORCE
	@(pkg=`basename $@ -Ex.R`; \
	if find $@ -newer $(top_srcdir)/share/R/massage-examples.R \
	    -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$$$$; \
	  LC_ALL=C $(R_EXE) < $(top_srcdir)/share/R/massage-examples.R \
	    --args $${pkg} $(top_builddir)/library/$${pkg}/R-ex > $${f}; \
	  $(SHELL) $(top_srcdir)/tools/move-if-change $${f} $@; \
	  touch $@; \
	fi)
FORCE:

$(EX_OUT_BASE) $(EX_OUT_RECOMMENDED): Makefile \
  $(top_builddir)/bin/exec@R_ARCH@/R \
  $(top_builddir)/src/library/base/all.R \
  $(top_builddir)/src/library/graphics/all.R \
  $(top_builddir)/src/library/methods/all.R \
  $(top_builddir)/src/library/stats/all.R \
  $(top_builddir)/src/library/utils/all.R \
  $(top_builddir)/modules@R_ARCH@/lapack$(SHLIB_EXT) \
  @WANT_R_SHLIB_TRUE@ $(top_builddir)/lib@R_ARCH@/libR$(DYLIB_EXT)
graphics-Ex.Rout: $(top_builddir)/modules@R_ARCH@/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:
