#
# ${R_HOME}/src/library/Makefile

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

top_builddir = ../..
subdir = src/library

include $(top_builddir)/Makeconf

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

SUBDIRS = profile $(R_PKGS_BASE) Recommended

## <NOTE>
## Need to use R CMD to ensure that R_HOME is set (or do this ourselves).
## Need to go through Rcmd rather than just call $(PERL) to ensure that
## PERL5LIB/PERLLIB are set appropriately (or do this ourselves).
## See e.g. @file{src/gnuwin32/help/Makefile}.
R_CMD_BUILD_HELP = $(top_builddir)/bin/R CMD perl $(top_srcdir)/share/perl/build-help.pl
## </NOTE>
R_CMD_BUILD_HELP_OPTS =

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

PDFLATEX = @PDFLATEX@

all: Makefile R

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@

## FIXME: do some of this in parallel
## beware there is a bootstrapping problem here: tools uses tools to
## dump its namespace. So it works initially, but not to update.
R:
	@$(MKINSTALLDIRS) $(top_builddir)/library
	@for pkg in profile $(R_PKGS_BASE); do \
	  (cd $${pkg} && $(MAKE)) || exit 1; \
	done
	@$(ECHO) "tools:::.vinstall_package_descriptions_as_RDS(\"$(top_builddir)/library\", \"$(R_PKGS_BASE)\")" | \
	  R_DEFAULT_PACKAGES=tools LC_ALL=C $(R_EXE) > /dev/null
	@$(ECHO) "installing parsed NAMESPACE files"
	@rm -f $(top_builddir)/library/tools/Meta/nsInfo.rds
	@$(ECHO) "tools:::.vinstall_package_namespaces_as_RDS(\"$(top_builddir)/library\", \"$(R_PKGS_BASE)\")" | \
	  R_DEFAULT_PACKAGES=tools LC_ALL=C $(R_EXE) > /dev/null
	@$(INSTALL_DATA) $(top_srcdir)/doc/html/R.css \
	  $(top_builddir)/library

## do metadata before help indices
docs:
	@$(MAKE) metadata
	@$(ECHO) "building all R documentation indices"
	@$(MAKE) help-indices
	@$(ECHO) "building all R object docs (text, HTML, LaTeX, examples)"
	@$(MAKE) Rdfiles
	@touch stamp-docs

help-indices:
	@for pkg in $(R_PKGS_BASE); do \
	  $(ECHO) "tools:::.writePkgIndices(\"$(srcdir)/$${pkg}\", \"$(top_builddir)/library/$${pkg}\")" | LC_ALL=C $(R_EXE) || exit 1; \
	done

Rdfiles-new:
	@for pkg in $(R_PKGS_BASE); do \
	  $(ECHO) "tools:::.convertRdfiles(\"$(srcdir)/$${pkg}\", \"$(top_builddir)/library/$${pkg}\")" | LC_ALL=C $(R_EXE) || exit 1; \
	done

## NB this is called from tests/Examples/Makefile with the make variables set.
Rdfiles:
	@for pkg in $(R_PKGS_BASE); do \
	  $(R_CMD_BUILD_HELP) $(R_CMD_BUILD_HELP_OPTS) $(srcdir)/$${pkg}; \
	done

base-test:
	rm -f $(top_builddir)/library/base/html/*
	rm -f $(top_builddir)/library/base/R-ex/*
	$(R_CMD_BUILD_HELP) --html --example $(srcdir)/base
base-new:
	rm -f $(top_builddir)/library/base/html/*
	rm -f $(top_builddir)/library/base/R-ex/*
	@$(ECHO) "tools:::.convertRdfiles(\"$(srcdir)/base\", \"$(top_builddir)/library/base\")" | LC_ALL=C $(R_EXE)


vignettes:
	@if test "$(PDFLATEX)" = false; then \
          $(ECHO) "WARNING: cannot build/update package vignettes."; \
	  $(ECHO) "'pdflatex' needed but missing on your system."; \
	else \
	  for pkg in $(R_PKGS_BASE); do \
	    if test -d "$(srcdir)/$${pkg}/inst/doc"; then \
	      $(ECHO) "building/updating vignettes for package '$${pkg}' ..."; \
	      ($(ECHO) "tools:::.install_package_vignettes(\"$(srcdir)/$${pkg}\", \"$(top_builddir)/library/$${pkg}\")") | \
	          R_DEFAULT_PACKAGES="utils,tools" LC_ALL=C \
	            $(R_EXE) > /dev/null; \
	    fi; \
	  done; \
	fi

metadata:
	@$(ECHO) "building/updating package metadata ..."
	@$(ECHO) "tools:::.vinstall_package_indices(\"$(srcdir)\", \"$(top_builddir)/library\", \"$(R_PKGS_BASE)\")" | \
	  R_DEFAULT_PACKAGES=tools LC_ALL=C $(R_EXE) > /dev/null
	@cat $(top_builddir)/library/*/CONTENTS \
	  > $(top_builddir)/doc/html/search/index.txt

help:
	@$(MAKE) Rdfiles R_CMD_BUILD_HELP_OPTS="--txt"

html:
	@$(MAKE) Rdfiles R_CMD_BUILD_HELP_OPTS="--html"
	@$(ECHO) "tools:::unix.packages.html(.Library)" | \
	  R_DEFAULT_PACKAGES=NULL LC_ALL=C "${R_EXE}" >/dev/null

latex:
	@$(MAKE) Rdfiles R_CMD_BUILD_HELP_OPTS="--latex"

examples:
	@$(MAKE) Rdfiles R_CMD_BUILD_HELP_OPTS="--example"

install: installdirs
	@$(ECHO) "installing packages ..."
	@cp -r $(top_builddir)/library "$(DESTDIR)$(rhome)" || \
	  (cd $(top_builddir)/library; \
	    $(TAR) cf - . | (cd "$(DESTDIR)$(rhome)/library"; $(TAR) xmof -))
	@$(ECHO) "  building HTML index ..."
	@$(ECHO) "tools:::unix.packages.html(.Library, docdir=\"$(DESTDIR)${rdocdir}\")" | \
          R_DEFAULT_PACKAGES=NULL LC_ALL=C ${R_EXE} >/dev/null
installdirs:
	@$(MKINSTALLDIRS) "$(DESTDIR)$(rhome)/library"
install-strip:
	@$(MAKE) install
	@if test -n "$(STRIP_LIBS)"; then \
	  $(STRIP_LIBS) "$(DESTDIR)$(rhome)/library"/*/libs/*${SHLIB_EXT}; \
	fi
uninstall:
	@$(ECHO) "uninstalling packages ..."
	@(cd "$(DESTDIR)$(rhome)/library"; \
	  for p in $(R_PKGS); do rm -rf $${p}; done)
	@rm -f "$(DESTDIR)$(rhome)/library/R.css"
	@rmdir "$(DESTDIR)$(rhome)/library" 2>/dev/null \
          || $(ECHO) "  subdir $(DESTDIR)$(rhome)/library not removed"

install-tests:
	@$(ECHO) "installing package tests ..."
	@for pkg in $(R_PKGS_BASE); do \
	  if test -d "$(srcdir)/$${pkg}/tests"; then \
	    $(MKINSTALLDIRS) "$(DESTDIR)$(rhome)/library/$${pkg}/tests" ; \
	    for f in $(srcdir)/$${pkg}/tests/*; do \
	      $(INSTALL_DATA) $${f} "$(DESTDIR)$(rhome)/library/$${pkg}/tests"; \
	    done; \
	  fi; \
	done
	@(cd Recommended && $(MAKE) $@)

## all the uninstall is done in tests/Makefile for now.
uninstall-tests:

mostlyclean: clean
clean:
	@(for d in $(SUBDIRS); do rsd="$${d} $${rsd}"; done; \
	  for d in $${rsd}; do (cd $${d} && $(MAKE) $@); done)
	-@rm -rf .vignettes
	-@rm -f stamp-*
distclean:
	@(for d in $(SUBDIRS); do rsd="$${d} $${rsd}"; done; \
	  for d in $${rsd}; do (cd $${d} && $(MAKE) $@); done)
	-@rm -f Makefile
maintainer-clean: distclean

TAGS info dvi check:

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
