#
# ${R_HOME}/doc/manual/Makefile

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

top_builddir = ../..
subdir = doc/manual

include $(top_builddir)/Makeconf

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
SOURCES_TEXI = R-FAQ.texi R-admin.texi R-data.texi R-exts.texi \
  R-intro.texi R-lang.texi
DISTFILES = \
	README \
	Makefile.in Makefile.win \
	R-defs.texi dir pdfcolor.tex texinfo.tex txi-en.tex \
	$(SOURCES_TEXI) \
	resources.texi \
	R-exts.R R-exts.c R-intro.R \
	refman.top refman.bot \
	ISBN
SUBDIRS_WITH_NO_BUILD = images

OBJECTS_DVI = $(SOURCES_TEXI:.texi=.dvi)
OBJECTS_HTML = $(SOURCES_TEXI:.texi=.html)
OBJECTS_INFO = $(SOURCES_TEXI:.texi=.info)
OBJECTS_PDF = $(SOURCES_TEXI:.texi=.pdf)

MAKEINFO = @MAKEINFO@
MAKEINFO_HTML_OPTS = --html --no-split
MAKEINFO_TEXT_OPTS = --number-sections --fill-column=76 --no-split --no-headers
TEXI2HTML = $(MAKEINFO) $(MAKEINFO_HTML_OPTS)
TEXI2TEXT = $(MAKEINFO) $(MAKEINFO_TEXT_OPTS)
TEXI2DVI = $(top_builddir)/bin/texi2dvi --texinfo="@set UseExternalXrefs "
TEXI2PDF = $(TEXI2DVI) --pdf
PDFLATEX = @PDFLATEX@
PDFTEX = @PDFTEX@
TEX = @TEX@

infodir = @infodir@
INSTALL_INFO = @INSTALL_INFO@

R_PAPERSIZE = @R_PAPERSIZE@
R_RD4DVI = @R_RD4DVI@
R_RD4PDF = @R_RD4PDF@

texinputs_BASE = $(R_PKGS_BASE:=-pkg.tex)
texinputs_RECOMMENDED = $(R_PKGS_RECOMMENDED:=-pkg.tex)
R_PKGS_refman = base datasets grDevices graphics grid methods stats tools utils
refman_TEXINPUTS = $(R_PKGS_refman:=-pkg.tex)
refman_DEPENDENCIES = version.tex $(top_srcdir)/share/texmf/Rd.sty
fullrefman_TEXINPUTS = $(texinputs_BASE) @USE_RECOMMENDED_PACKAGES_TRUE@ $(texinputs_RECOMMENDED)

texiincludes = version.texi $(srcdir)/R-defs.texi

.SUFFIXES:
.SUFFIXES: .dvi .html .info .texi .pdf

all: Makefile R docs

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

R: Makefile @MAINTAINER_MODE_TRUE@ maintainer-build

docs: html

.texi.dvi:
	@if test "$(TEX)" = false; then \
	  echo "ERROR: 'tex' needed but missing on your system."; \
	  exit 1; \
	fi
	TEXINPUTS="$(srcdir):$$TEXINPUTS" $(TEXI2DVI) $<

.texi.html:
	$(TEXI2HTML) -D UseExternalXrefs -I$(srcdir) $< -o $@ || touch $@

.texi.info:
	@if test "$(MAKEINFO)" = false; then \
	  echo "ERROR: 'makeinfo' v4.5 or later needed but missing on your system."; \
	  exit 1; \
	fi
	$(MAKEINFO) -D UseExternalXrefs -I$(srcdir) $<

.texi.pdf:
	@if test "$(PDFTEX)" = false; then \
	  echo "ERROR: 'pdftex' needed but missing on your system."; \
	  exit 1; \
	fi
	TEXINPUTS="$(srcdir):$$TEXINPUTS" $(TEXI2PDF) $<

## we have to be careful not to install a local R-admin.html made prior to
## texinfo 4.7, as doc/html/SearchEngine.html links to it.
## So use that in the sources if we can -- it is not in the svn archive.
## There are no cross-references, external or not.
R-admin.html:
	if test -f "$(top_srcdir)/doc/html/R-admin.html" ; then \
	  cp "$(top_srcdir)/doc/html/R-admin.html" R-admin.html ; \
	else \
	  $(TEXI2HTML) -I$(srcdir) $(srcdir)/R-admin.texi -o $@ || touch $@ ; \
	fi

dvi: refman.dvi $(OBJECTS_DVI)
refman.dvi: $(refman_DEPENDENCIES) stamp-refman-dvi
	@if test "$(LATEX)" = false; then \
	  echo "ERROR: 'latex' needed but missing on your system."; \
	  exit 1; \
	fi
	@echo "DVI/LaTeX documentation: reference index ..."
	@rm -f *.aux refman.toc refman.ind stamp-refman-dvi
## <NOTE>
## We do not make sure echo does not interpret backslash escapes.
## Could be trouble ...
	@(opt="$(R_PAPERSIZE)paper"; \
	  echo "\\documentclass[$${opt}]{book}"; \
	  opt="$(R_RD4DVI)"; \
	  echo "\\usepackage[$${opt}]{Rd}"; \
	  cat $(srcdir)/refman.top; \
	  texinputs=`(for f in $(refman_TEXINPUTS); \
	    do echo $${f}; done) | LC_COLLATE=C sort`; \
	  for f in $${texinputs}; do echo "\\input{$${f}}"; done; \
	  cat $(srcdir)/refman.bot) > refman.tex
## </NOTE>
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  $(LATEX) '\nonstopmode\input{refman.tex}'
	@-$(MAKEINDEX) refman
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  $(LATEX) '\nonstopmode\input{refman.tex}'
	@touch stamp-refman-dvi

version.tex: Makefile $(top_srcdir)/date-stamp $(top_srcdir)/VERSION
	@echo "creating $(subdir)/$@"
	@(v=`cat $(top_srcdir)/VERSION`; \
	  v="$${v} (`sed 's|/|-|g' $(top_srcdir)/date-stamp`)"; \
	  echo "$${v}") > $@

stamp-refman-dvi: $(refman_TEXINPUTS)
	@touch $@

## <NOTE> This assumes none of the latex dirs are zipped </NOTE>
$(texinputs_BASE): FORCE $(top_srcdir)/share/perl/build-help.pl
	@(pkg=`basename $@ -pkg.tex`; \
	if $(PERL) $(top_srcdir)/tools/Rdnewer.pl \
	    "$(top_srcdir)/src/library/$${pkg}" "$@"; then \
	  echo "collecting LaTeX docs for package '$${pkg}' ..."; \
	  (cd $(top_builddir)/src/library && \
	    OPTS="--latex" $(MAKE) R_PKGS_BASE="$${pkg}" Rdfiles); \
	  PERL5LIB=$(top_srcdir)/share/perl $(PERL) $(top_srcdir)/tools/pkg2tex.pl $${pkg}; \
	fi)
FORCE:

$(texinputs_RECOMMENDED): FORCE
	@(pkg=`basename $@ -pkg.tex`; \
	if find $@ -newer $(top_builddir)/library/$${pkg}/DESCRIPTION \
	    -print 2> /dev/null | grep $@ > /dev/null; then :; else \
	  echo "collecting LaTeX docs for package '$${pkg}' ..."; \
	  RLIB="$(top_builddir)/library" \
	    PERL5LIB=$(top_srcdir)/share/perl $(PERL) $(top_srcdir)/tools/pkg2tex.pl $${pkg}; \
	fi)

fullrefman.dvi: $(refman_DEPENDENCIES) stamp-fullrefman-dvi
	@if test "$(LATEX)" = false; then \
	  echo "ERROR: 'latex' needed but missing on your system."; \
	  exit 1; \
	fi
	@echo "DVI/LaTeX documentation: full reference index ..."
	@rm -f *.aux fullrefman.toc fullrefman.ind stamp-fullrefman-dvi
	@(opt="$(R_PAPERSIZE)paper"; \
	  echo "\\documentclass[$${opt}]{book}"; \
	  opt="$(R_RD4DVI)"; \
	  echo "\\usepackage[$${opt}]{Rd}"; \
	  cat $(srcdir)/refman.top; \
	  echo "\\part{}"; \
	  texinputs=`(for f in $(refman_TEXINPUTS); \
	    do echo $${f}; done) | LC_COLLATE=C sort`; \
	  for f in $${texinputs}; do echo "\\input{$${f}}"; done; \
	  echo "\\part{}"; \
	  texinputs=`(for f in $(fullrefman_TEXINPUTS); \
	    do echo $${f}; done) | LC_COLLATE=C sort`; \
	  for f in $(refman_TEXINPUTS); do \
	    texinputs=`echo "$${texinputs}" | sed "s/$${f}//"`; \
	  done; \
	  for f in $${texinputs}; do echo "\\input{$${f}}"; done; \
	  cat $(srcdir)/refman.bot) > fullrefman.tex
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  $(LATEX) '\nonstopmode\input{fullrefman.tex}'
	@-$(MAKEINDEX) fullrefman
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  $(LATEX) '\nonstopmode\input{fullrefman.tex}'
	@touch stamp-fullrefman-dvi
stamp-fullrefman-dvi: $(fullrefman_TEXINPUTS)

$(OBJECTS_DVI): $(srcdir)/texinfo.tex
R-admin.dvi: $(texiincludes)
R-data.dvi: $(texiincludes)
R-exts.dvi: $(texiincludes)
version.texi: Makefile $(top_srcdir)/VERSION $(top_srcdir)/date-stamp
	@echo "creating $(subdir)/$@"
	@(v=`sed 's/\([^ ]*\).*/\1/' $(top_srcdir)/VERSION`; \
	  echo "@set VERSIONno $${v}" > $@; \
	  v="$${v} (`sed 's|/|-|g' $(top_srcdir)/date-stamp`)"; \
	  echo "@set VERSION $${v}" >> $@; \
	  rwv=`$(PERL) $(top_srcdir)/src/gnuwin32/fixed/rwver.pl $(top_srcdir)/VERSION`; \
	  echo "@set RWVERSION $${rwv}" >> $@ )
	if test "$(R_PAPERSIZE)" = "a4"; then \
	  echo "@afourpaper" >> $@ ; \
	fi

R-intro.dvi: stamp-images-eps $(texiincludes)
stamp-images-eps:
	@if test "$(BUILDDIR_IS_SRCDIR)" = no ; then \
	  $(MAKE) rhome=$(top_builddir) install-images-eps; \
	fi
	@touch $@
R-lang.dvi: $(texiincludes)

html: $(OBJECTS_HTML)
	@(cd ../html; $(MAKE) index.html)

R-admin.html: $(texiincludes)
R-data.html: $(texiincludes)
R-exts.html: $(texiincludes)
R-intro.html: $(texiincludes)
R-lang.html: $(texiincludes)

info: $(OBJECTS_INFO)
R-admin.info: $(texiincludes)
R-data.info: $(texiincludes)
R-exts.info: $(texiincludes)
R-intro.info: $(texiincludes)
R-lang.info: $(texiincludes)

pdf: refman.pdf $(OBJECTS_PDF)
refman.pdf: $(refman_DEPENDENCIES) stamp-refman-pdf
	@if test "$(PDFLATEX)" = false; then \
	  echo "ERROR: 'pdflatex' needed but missing on your system."; \
	  exit 1; \
	fi
	@echo "PDF/LaTeX documentation: reference index ..."
	@rm -f *.aux refman.toc refman.ind stamp-refman-pdf
## <NOTE>
## We do not make sure echo does not interpret backslash escapes.
## Could be trouble ...
	@(opt="$(R_PAPERSIZE)paper"; \
	  echo "\\documentclass[$${opt}]{book}"; \
	  opt="$(R_RD4PDF)"; \
	  echo "\\usepackage[$${opt}]{Rd}"; \
	  cat $(srcdir)/refman.top; \
	  texinputs=`(for f in $(refman_TEXINPUTS); \
	    do echo $${f}; done) | LC_COLLATE=C sort`; \
	  for f in $${texinputs}; do echo "\\input{$${f}}"; done; \
	  cat $(srcdir)/refman.bot) > refman.tex
## </NOTE>
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  pool_size=500000 max_strings=50000 \
	  $(PDFLATEX) '\nonstopmode\input{refman.tex}'
	@-$(MAKEINDEX) refman
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  pool_size=500000 max_strings=50000 \
	  $(PDFLATEX) '\nonstopmode\input{refman.tex}'
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  pool_size=500000 max_strings=50000 \
	  $(PDFLATEX) '\nonstopmode\input{refman.tex}'
	@touch stamp-refman-pdf
stamp-refman-pdf: $(refman_TEXINPUTS)
	@touch $@
fullrefman.pdf: $(refman_DEPENDENCIES) stamp-fullrefman-pdf
	@if test "$(PDFLATEX)" = false; then \
	  echo "ERROR: 'pdflatex' needed but missing on your system."; \
	  exit 1; \
	fi
	@echo "PDF/LaTeX documentation: full reference index ..."
	@rm -f *.aux fullrefman.toc fullrefman.ind stamp-fullrefman-pdf
	@(opt="$(R_PAPERSIZE)paper"; \
	  echo "\\documentclass[$${opt}]{book}"; \
	  opt="$(R_RD4PDF)"; \
	  echo "\\usepackage[$${opt}]{Rd}"; \
	  cat $(srcdir)/refman.top; \
	  echo "\\part{}"; \
	  texinputs=`(for f in $(refman_TEXINPUTS); \
	    do echo $${f}; done) | LC_COLLATE=C sort`; \
	  for f in $${texinputs}; do echo "\\input{$${f}}"; done; \
	  echo "\\part{}"; \
	  texinputs=`(for f in $(fullrefman_TEXINPUTS); \
	    do echo $${f}; done) | LC_COLLATE=C sort`; \
	  for f in $(refman_TEXINPUTS); do \
	    texinputs=`echo "$${texinputs}" | sed "s/$${f}//"`; \
	  done; \
	  for f in $${texinputs}; do echo "\\input{$${f}}"; done; \
	  cat $(srcdir)/refman.bot) > fullrefman.tex
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  pool_size=500000 max_strings=50000 dest_names_size=50000 \
	  $(PDFLATEX) '\nonstopmode\input{fullrefman.tex}'
	@-$(MAKEINDEX) fullrefman
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  pool_size=500000 max_strings=50000 dest_names_size=50000 \
	  $(PDFLATEX) '\nonstopmode\input{fullrefman.tex}'
	@TEXINPUTS="$(top_srcdir)/share/texmf:$$TEXINPUTS" \
	  pool_size=500000 max_strings=50000 dest_names_size=50000 \
	  $(PDFLATEX) '\nonstopmode\input{fullrefman.tex}'
	@touch stamp-fullrefman-pdf
stamp-fullrefman-pdf: $(fullrefman_TEXINPUTS)

$(OBJECTS_PDF): $(srcdir)/texinfo.tex $(srcdir)/pdfcolor.tex
R-admin.pdf: $(texiincludes)
R-data.pdf: $(texiincludes)
R-exts.pdf: $(texiincludes)
R-intro.pdf: stamp-images-pdf $(texiincludes)
stamp-images-pdf:
	@if test "$(BUILDDIR_IS_SRCDIR)" = no ; then \
	  $(MAKE) rhome=$(top_builddir) install-images-pdf; \
	fi
	@touch $@
R-lang.pdf: $(texiincludes)

install: install-message installdirs install-html
install-message:
	@echo "installing $(subdir) ..."
installdirs:
	@$(MKINSTALLDIRS) "$(rhome)/$(subdir)"
install-strip: install
install-dvi: dvi installdirs install-images-eps
	@echo "installing R manuals in DVI format ..."
	@for f in *.dvi; do \
	  $(INSTALL_DATA) $${f} "$(rhome)/$(subdir)"; \
	done
install-images-eps:
	@$(MKINSTALLDIRS) "$(rhome)/$(subdir)"/images
	@-for f in $(srcdir)/images/*.eps; do \
	  $(INSTALL_DATA) $${f} "$(rhome)/$(subdir)"/images; \
	done
install-images-pdf:
	@$(MKINSTALLDIRS) "$(rhome)/$(subdir)"/images
	@-for f in $(srcdir)/images/*.pdf; do \
	  $(INSTALL_DATA) $${f} "$(rhome)/$(subdir)"/images; \
	done
install-html:
	@for f in $(OBJECTS_HTML); do \
	  $(INSTALL_DATA) $${f} "$(rhome)/$(subdir)"; \
	done
install-info: info $(infodir)/dir
	@echo "installing R info pages ..."
	@for f in $(OBJECTS_INFO:.info=.info*); do \
	  $(INSTALL_DATA) $${f} "$(infodir)"; \
	  $(R_GZIPCMD) -f "$(infodir)/$${f}"; \
	done
	@echo "updating '$(infodir)/dir' ..."
	@for f in $(OBJECTS_INFO); do \
	  $(INSTALL_INFO) --info-dir=$(infodir) $(infodir)/$${f} \
	    || echo "you need to adjust $(infodir)/dir manually"; \
	done
install-pdf: pdf installdirs
	@echo "installing R manuals in PDF format ..."
	@for f in *.pdf; do \
	  $(INSTALL_DATA) $${f} "$(rhome)/$(subdir)"; \
	done

$(infodir)/dir:
	@$(MKINSTALLDIRS) "$(infodir)"
	$(INSTALL_DATA) "$(srcdir)/dir" "$(infodir)"

uninstall:
	@if test -f "$(infodir)/R-data.info" \
	     || test -f "$(infodir)/R-data.info.gz"; then \
	  $(MAKE) uninstall-info; \
	fi
	@echo "uninstalling $(subdir) ..."
	@rm -rf "$(rhome)/$(subdir)"
uninstall-dvi:
	@echo "uninstalling R manuals in DVI format ..."
	@-rm -f "$(rhome)/$(subdir)"/images/*.eps
	@-rmdir "$(rhome)/$(subdir)"/images
	@-rm -f "$(rhome)/$(subdir)"/*.dvi
uninstall-info:
	@echo "uninstalling R info pages ..."
	@for f in $(OBJECTS_INFO); do \
	  $(INSTALL_INFO) --remove --info-dir=$(infodir) $${f} \
	    || echo "you need to adjust $(infodir)/dir manually"; \
	done
	@for f in $(OBJECTS_INFO:.info=.info*); do \
	  rm -f "$(infodir)/$${f}" "$(infodir)/$${f}.gz" ; \
	done
uninstall-pdf:
	@echo "uninstalling R manuals in PDF format ..."
	@-rm -f "$(rhome)/$(subdir)"/*.pdf

mostlyclean: clean
clean:
	@if test "$(BUILDDIR_IS_SRCDIR)" = no ; then \
	  rm -rf $(INSTFILES) images; \
	fi
	-@rm -f *.aux *.cp *.cps *.dvi *.fn *.fns *.html *.info* \
	  *.ky *.kys *.log *.out *.pdf *.pg *.pgs *.ps *.ps-2 *.tmp \
	  *.toc *.tp *.vr *.vrs \
	  *-pkg.tex refman.i?? refman.tex fullrefman.i?? fullrefman.tex \
	  version.tex version.texi
	-@rm -f stamp-*
distclean: clean
	-@rm -f Makefile
maintainer-clean: distclean

maintainer-build: \
  $(top_srcdir)/FAQ \
  $(top_srcdir)/RESOURCES \
  $(top_srcdir)/doc/html/faq.html \
  $(top_srcdir)/doc/html/resources.html
$(top_srcdir)/FAQ: @MAINTAINER_MODE_TRUE@ $(srcdir)/R-FAQ.texi
	@if test "$(MAKEINFO)" = false; then \
	  echo "ERROR: 'makeinfo' v4 needed but missing on your system."; \
	  exit 1; \
	fi
	$(TEXI2TEXT) $(srcdir)/R-FAQ.texi -o $@
$(top_srcdir)/doc/html/faq.html: @MAINTAINER_MODE_TRUE@ $(srcdir)/R-FAQ.texi
	@if test "$(MAKEINFO)" = false; then \
	  echo "ERROR: 'makeinfo' v4 needed but missing on your system."; \
	  exit 1; \
	fi
	$(TEXI2HTML) --number-sections $(srcdir)/R-FAQ.texi -o $@
$(top_srcdir)/RESOURCES: @MAINTAINER_MODE_TRUE@ $(srcdir)/resources.texi
	@if test "$(MAKEINFO)" = false; then \
	  echo "ERROR: 'makeinfo' v4 needed but missing on your system."; \
	  exit 1; \
	fi
	$(TEXI2TEXT) $(srcdir)/resources.texi -o $@
$(top_srcdir)/doc/html/resources.html: @MAINTAINER_MODE_TRUE@ $(srcdir)/resources.texi
	@if test "$(MAKEINFO)" = false; then \
	  echo "ERROR: 'makeinfo' v4 needed but missing on your system."; \
	  exit 1; \
	fi
	$(TEXI2HTML) --no-headers $(srcdir)/resources.texi -o $@

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_WITH_NO_BUILD); do \
	  ((cd $(srcdir); $(TAR) -c -f - $(DISTDIR_TAR_EXCLUDE) $${d}) \
	      | (cd $(distdir); $(TAR) -x -f -)) \
	    || exit 1; \
	done
	@if test "$(MAKEINFO)" = false; then \
	  echo "ERROR: 'makeinfo' v4 needed but missing on your system."; \
	  exit 1; \
	fi
#	$(distdir)/doc/html has already been created
	$(TEXI2HTML) --no-headers --number-sections \
	  $(srcdir)/R-admin.texi -I$(srcdir) \
	  -o $(distdir)/../html/R-admin.html
