#
# ${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 = $(R_PKGS:=-Ex.R)
EX_OUT = $(EX_IN:.R=.Rout)

.SUFFIXES:
.SUFFIXES: .R .Rout

all: Makefile Makedeps test-Examples

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	@cd $(top_builddir) && \
	  CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= \
	  $(SHELL) ./config.status
Makedeps: Makefile
	@(for p in $(R_PKGS); 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: Makefile
	@$(MAKE) Makedeps
	@$(MAKE) $(EX_OUT)

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

.R.Rout:
	@if [ -f $@.bak ]; then mv $@.bak $@.bakk ; fi
	@if [ -f $@ ];     then cp -p $@     $@.bak  ; fi
	@echo @ECHO_N@ "running code in \`$<' ...@ECHO_C@"
	@$(R) < $< > $@ || (mv $@ $@.fail && exit 1)
	@echo "@ECHO_T@ OK"

$(EX_IN): FORCE
	@(pkg=`basename $@ -Ex.R`; \
	if $(PERL) $(top_srcdir)/tools/Rdnewer.pl \
	    "$(top_srcdir)/src/library/$${pkg}" "$@"; then \
	  if test -f $@ ; then \
	    if test -n "`tail -6 $@ | grep '^Time elapsed'`" ; then \
	      if test -f $@.bak ; then mv $@.bak $@.bakk ; fi; \
	      cp -p $@ $@.bak; \
	    fi; \
	  fi; \
	  echo "collecting examples for package \`$${pkg}' ..."; \
	  (cd $(top_builddir)/src/library && \
	    OPTS="--txt --example" $(MAKE) R_PKGS="$${pkg}" DOCS); \
	  f=$${TMPDIR-/tmp}/R$$$$; \
	  $(PERL) $(top_srcdir)/share/perl/massage-Examples.pl $${pkg} \
	    $(top_builddir)/library/$${pkg}/R-ex/*.R > $${f}; \
	  $(top_srcdir)/tools/move-if-change $${f} $@; \
	fi)
FORCE:

$(EX_OUT): 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 .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:
