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

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

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

include $(top_builddir)/Makeconf

SORT = sort # to match Windows

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
SOURCES_TEXI = R-FAQ.texi R-admin.texi R-data.texi R-exts.texi \
  R-intro.texi R-ints.texi R-lang.texi
DISTFILES = \
	README \
	Makefile.in Makefile.win \
	R-defs.texi dir epsf.tex pdfcolor.tex \
	$(SOURCES_TEXI) rw-FAQ.texi \
	resources.texi \
	R-exts.R R-exts.c R-intro.R \
	refman.top refman.bot \
	ISBN Rman.css Rfaq.css quot.sed
SUBDIRS_WITH_NO_BUILD = images

OBJECTS_HTML = $(SOURCES_TEXI:.texi=.html)
OBJECTS_INFO = $(SOURCES_TEXI:.texi=.info)
OBJECTS_PDF = $(SOURCES_TEXI:.texi=.pdf)
OBJECTS_EPUB = $(SOURCES_TEXI:.texi=.epub)
OBJECTS_MOBI = $(SOURCES_TEXI:.texi=.mobi)

MAKEINDEX = @MAKEINDEX@
MAKEINFO = @MAKEINFO@
MAKEINFO_HTML_OPTS = --html --no-split --css-include=$(srcdir)/Rman.css
MAKEINFO_TEXT_OPTS = --number-sections --fill-column=76 --no-split --no-headers
TEXI2HTML = $(MAKEINFO) $(MAKEINFO_HTML_OPTS)
TEXI2TEXT = $(MAKEINFO) $(MAKEINFO_TEXT_OPTS)
## some broken versions of texi2dvi need C collation.
TEXI2DVI = LC_COLLATE=C @TEXI2DVI@ --texinfo="@set UseExternalXrefs "
TEXI2PDF = $(TEXI2DVI) --pdf
PDFLATEX = @PDFLATEX@
## These are used to check if there is a non-empty path to the
## texi2dvi script, and as it uses pdftex if that was detected.
TEXI2DVI0 = @TEXI2DVI@
PDFTEX = @PDFTEX@

BATCHPDFLATEX = $(PDFLATEX) -interaction=nonstopmode

## support for ebook conversion.
## ebook-convert is part of 'calibre'
## (http://calibre-ebook.com/download, or Debian/Fedora/... packages)
EBOOK = ebook-convert
## or on OS X
## EBOOK = /Applications/calibre.app/Contents/MacOS/ebook-convert
EBOOK_OPTS = --language en --publisher "R Core Team"

datarootdir = @datarootdir@
infodir = @infodir@
INSTALL_INFO = @INSTALL_INFO@

R_PAPERSIZE = @R_PAPERSIZE@
R_RD4PDF = @R_RD4PDF@

texinputs_BASE = $(R_PKGS_BASE:=-pkg.tex)
texinputs_RECOMMENDED = $(R_PKGS_RECOMMENDED:=-pkg.tex)
refman_DEPENDENCIES = version.tex $(top_srcdir)/share/texmf/tex/latex/Rd.sty
fullrefman_TEXINPUTS = $(texinputs_BASE) @USE_RECOMMENDED_PACKAGES_TRUE@ $(texinputs_RECOMMENDED)
SVN_REV = $(top_builddir)/SVN-REVISION

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

.SUFFIXES:
.SUFFIXES: .html .info .texi .pdf .epub .mobi

all: Makefile R docs

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

R: Makefile

docs: html

.texi.html:
	@if test -z "$(MAKEINFO)"; then \
	  $(ECHO) "'texi2any' v5.1 or later needed to make HTML docs but missing on your system." ; \
	  $(ECHO) "file $@ will be missing and linked from CRAN"; \
	else \
	  $(ECHO) "creating doc/manual/$@"; \
	  $(TEXI2HTML) -D UseExternalXrefs -I$(srcdir) $< -o $@.tmp || touch $@; \
	  $(SED) -f $(srcdir)/quot.sed $@.tmp > $@; \
	  rm -f $@.tmp; \
	fi

.texi.info:
	@if test -z "$(MAKEINFO)"; then \
	  $(ECHO) "ERROR: 'texi2any' v5.1 or later needed but missing on your system."; \
	  exit 1; \
	fi
	$(MAKEINFO) --enable-encoding -D UseExternalXrefs -I$(srcdir) $<

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

## It seems ebook-convert converts the directory, 
## so we populate a temporary dir.
.texi.epub:
	@mkdir -p tmp
	@$(TEXI2HTML) --no-headers -I$(srcdir) $< -o $@.tmp
	@$(SED) -f $(srcdir)/quot.sed $@.tmp > tmp/$*.html
	@rm -f $@.tmp
	$(EBOOK) tmp/$*.html $@ $(EBOOK_OPTS) --authors "R Core Team"
	@rm -R tmp

.texi.mobi:
	@mkdir -p tmp
	@$(TEXI2HTML) --no-headers -I$(srcdir) $< -o $@.tmp
	@$(SED) -f $(srcdir)/quot.sed $@.tmp > tmp/$*.html
	@rm -f $@.tmp
	$(EBOOK) tmp/$*.html $@ $(EBOOK_OPTS) --authors "R Core Team"
	@rm -R tmp

R-intro.epub: R-intro.texi
	@mkdir -p tmp
	@cp -R images tmp
	@$(TEXI2HTML) --no-headers -I$(srcdir) $< -o $@.tmp
	@$(SED) -f $(srcdir)/quot.sed $@.tmp > tmp/$*.html
	@rm -f $@.tmp
	$(EBOOK) tmp/$*.html $@ $(EBOOK_OPTS) --authors \
  "W.N. Venables, D.M. Smith, R Core Team"
	@rm -R tmp

R-intro.mobi: R-intro.texi
	@mkdir -p tmp
	@cp -r images tmp
	@$(TEXI2HTML) --no-headers -I$(srcdir) $< -o $@.tmp
	@$(SED) -f $(srcdir)/quot.sed $@.tmp > tmp/$*.html
	@rm -f $@.tmp
	$(EBOOK) tmp/$*.html $@ $(EBOOK_OPTS) --authors \
  "W.N. Venables, D.M. Smith, R Core Team"
	@rm -R tmp

R-admin.html: $(srcdir)/R-admin.texi
	@if test -z "$(MAKEINFO)"; then \
	  $(ECHO) "'texi2any' v5.1 or later needed to make HTML docs but missing on your system." ; \
	  $(ECHO) "file $@ will be missing and linked from CRAN"; \
	else \
	  $(ECHO) "creating doc/manual/$@"; \
	  $(TEXI2HTML) -I$(srcdir) $(srcdir)/R-admin.texi -o $@.tmp || touch $@;\
	  $(SED) -f $(srcdir)/quot.sed $@.tmp > $@; \
	  rm -f $@.tmp; \
	fi

R-FAQ.html: $(srcdir)/R-FAQ.texi
	@if test -z "$(MAKEINFO)"; then \
	  $(ECHO) "'texi2any' v5.1 or later needed to make HTML docs but missing on your system." ; \
	  $(ECHO) "file $@ will be missing and linked from CRAN"; \
	else \
	  $(ECHO) "creating doc/manual/$@"; \
	  $(MAKEINFO) --html --no-split --css-include=$(srcdir)/Rfaq.css -D UseExternalXrefs -I$(srcdir) $< -o $@.tmp || touch $@; \
	  $(SED) -f $(srcdir)/quot.sed $@.tmp > $@; \
	  rm -f $@.tmp; \
	fi


version.tex: Makefile $(top_srcdir)/VERSION $(SVN_REV)
	@$(ECHO) "creating $(subdir)/$@"
	@(v=`cat $(top_srcdir)/VERSION`; \
	  v="$${v} (`sed -e 1d -e 's/^Last Changed Date: //' $(SVN_REV)`)"; \
	  $(ECHO) "$${v}") > $@

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

$(texinputs_BASE): FORCE
	@(pkg=`basename $@ -pkg.tex`; \
	if $(R_EXE) -e \
	  "tools:::.Rdnewer(\"$(top_srcdir)/src/library/$${pkg}\", \"$@\")"; then \
	  $(ECHO) "collecting LaTeX docs for package '$${pkg}' ..."; \
	  $(ECHO) "tools:::.pkg2tex(\"$(top_builddir)/library/$${pkg}\")" \
	    | LC_ALL=C $(R_EXE) ; \
	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}' ..."; \
	  $(ECHO) "tools:::.pkg2tex(\"$(top_builddir)/library/$${pkg}\")" \
	    | LC_ALL=C $(R_EXE) ; \
	fi)

version.texi: Makefile $(top_srcdir)/VERSION $(SVN_REV)
	@$(ECHO) "creating $(subdir)/$@"
	@(vv=`sed 's/ *(.*//' $(top_srcdir)/VERSION`;v=`echo $${vv}| sed 's/\([^ ]*\).*/\1/'`; \
	  $(ECHO) "@set VERSIONno $${v}" > $@; \
	  v="$${vv} (`sed -e 1d -e 's/^Last Changed Date: //' $(SVN_REV)`)"; \
	  $(ECHO) "@set VERSION $${v}" >> $@; \
	  rwv=`$(R_EXE) -f $(top_srcdir)/src/gnuwin32/fixed/rwver.R`; \
	  $(ECHO) "@set RWVERSION $${rwv}" >> $@ )
	@if test "$(R_PAPERSIZE)" = "a4"; then \
	  $(ECHO) "@afourpaper" >> $@ ; \
	fi

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

R-admin.html: $(texiincludes) $(srcdir)/Rman.css
R-data.html: $(texiincludes) $(srcdir)/Rman.css
R-exts.html: $(texiincludes) $(srcdir)/Rman.css
R-intro.html: stamp-images-html $(texiincludes) $(srcdir)/Rman.css
R-ints.html: $(texiincludes) $(srcdir)/Rman.css
R-lang.html: $(texiincludes) $(srcdir)/Rman.css
R-FAQ.html: $(srcdir)/Rfaq.css

ebooks: epub mobi
epub: $(OBJECTS_EPUB)
mobi: $(OBJECTS_MOBI)

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

pdf: fullrefman.pdf $(OBJECTS_PDF)
refman.pdf: $(refman_DEPENDENCIES) $(texinputs_BASE)
	@if test -z "$(PDFLATEX)"; 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
## <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}"; \
	  $(ECHO) "\\usepackage[utf8]{inputenc}"; \
	  cat $(srcdir)/refman.top; \
	  texinputs=`(for f in $(texinputs_BASE); \
	    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/tex/latex:$$TEXINPUTS" \
	  $(BATCHPDFLATEX) refman.tex
	@-$(MAKEINDEX) refman
	@TEXINPUTS="$(top_srcdir)/share/texmf/tex/latex:$$TEXINPUTS" \
	  $(BATCHPDFLATEX) refman.tex
	@TEXINPUTS="$(top_srcdir)/share/texmf/tex/latex:$$TEXINPUTS" \
	  $(BATCHPDFLATEX) refman.tex
fullrefman.pdf: $(refman_DEPENDENCIES) $(fullrefman_TEXINPUTS)
	@if test -z "$(PDFLATEX)"; 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
	@(opt="$(R_PAPERSIZE)paper"; \
	  $(ECHO) "\\documentclass[$${opt}]{book}"; \
	  opt="$(R_RD4PDF)"; \
	  $(ECHO) "\\usepackage[$${opt}]{Rd}"; \
	  $(ECHO) "\\usepackage[utf8]{inputenc}"; \
	  cat $(srcdir)/refman.top; \
	  if test -z "@USE_RECOMMENDED_PACKAGES_TRUE@"; then \
	    $(ECHO) "\\part{}"; \
	    texinputs=`(for f in $(texinputs_BASE); \
	      do $(ECHO) $${f}; done) | LC_COLLATE=C $(SORT)`; \
	    for f in $${texinputs}; do $(ECHO) "\\input{$${f}}"; done; \
	    $(ECHO) "\\part{}"; \
	    texinputs=`(for f in $(texinputs_RECOMMENDED); \
	      do $(ECHO) $${f}; done) | LC_COLLATE=C $(SORT)`; \
	    for f in $${texinputs}; do $(ECHO) "\\input{$${f}}"; done; \
	  else \
	    texinputs=`(for f in $(texinputs_BASE); \
	      do $(ECHO) $${f}; done) | LC_COLLATE=C $(SORT)`; \
	    for f in $${texinputs}; do $(ECHO) "\\input{$${f}}"; done; \
	  fi; \
	  cat $(srcdir)/refman.bot) > fullrefman.tex
	@TEXINPUTS="$(top_srcdir)/share/texmf/tex/latex:$$TEXINPUTS" \
	  $(BATCHPDFLATEX) fullrefman.tex
	@-$(MAKEINDEX) fullrefman
	@TEXINPUTS="$(top_srcdir)/share/texmf/tex/latex:$$TEXINPUTS" \
	  $(BATCHPDFLATEX) fullrefman.tex
	@TEXINPUTS="$(top_srcdir)/share/texmf/tex/latex:$$TEXINPUTS" \
	  $(BATCHPDFLATEX) fullrefman.tex

R-admin.pdf: $(texiincludes)
R-data.pdf: $(texiincludes)
R-exts.pdf: $(texiincludes)
R-intro.pdf: stamp-images-pdf $(texiincludes)
R-ints.pdf: $(texiincludes)
stamp-images-pdf:
	@if test "$(BUILDDIR_IS_SRCDIR)" = no ; then \
	  $(MAKE) rdocdir=$(top_builddir)/doc install-images-pdf; \
	fi
	@touch $@
R-lang.pdf: $(texiincludes)

install: install-message installdirs install-html install-images-html
install-message:
	@$(ECHO) "installing $(subdir) ..."
installdirs:
	@$(MKINSTALLDIRS) "$(DESTDIR)$(rdocdir)/manual"
install-strip: install
install-images-html: installdirs
	@$(MKINSTALLDIRS) "$(DESTDIR)$(rdocdir)/manual/images"
	@-for f in $(srcdir)/images/*.png; do \
	  $(INSTALL_DATA) $${f} "$(DESTDIR)$(rdocdir)/manual/images"; \
	done
stamp-images-html:
	@if test "$(BUILDDIR_IS_SRCDIR)" = no ; then \
	  $(MAKE) rdocdir=$(top_builddir)/doc install-images-html; \
	fi
	@touch $@
install-images-pdf: installdirs
	@$(MKINSTALLDIRS) "$(DESTDIR)$(rdocdir)/manual/images"
	@-for f in $(srcdir)/images/*.pdf; do \
	  $(INSTALL_DATA) $${f} "$(DESTDIR)$(rdocdir)/manual/images"; \
	done
## if there is no makeinfo >= 5.1 then there are no html pages
install-html: installdirs
	@for f in $(OBJECTS_HTML); do \
	  if test -f $${f} ; then \
	    $(INSTALL_DATA) $${f} "$(DESTDIR)$(rdocdir)/manual"; \
	  fi \
	done
install-info: info $(DESTDIR)$(infodir)/dir
	@$(ECHO) "installing R info pages ..."
	@for f in $(OBJECTS_INFO:.info=.info*); do \
	  $(INSTALL_DATA) $${f} "$(DESTDIR)$(infodir)"; \
	  $(R_GZIPCMD) -f "$(DESTDIR)$(infodir)/$${f}"; \
	done
	@$(ECHO) "updating '$(infodir)/dir' ..."
	@for f in $(OBJECTS_INFO); do \
	  $(INSTALL_INFO) --info-dir=$(DESTDIR)$(infodir) $(DESTDIR)$(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} "$(DESTDIR)$(rdocdir)/manual"; \
	done

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

uninstall:
	@if test -f "$(DESTDIR)$(infodir)/R-data.info" \
	     || test -f "$(DESTDIR)$(infodir)/R-data.info.gz"; then \
	  $(MAKE) uninstall-info; \
	fi
	@$(ECHO) "uninstalling $(subdir) ..."
	@rm -Rf "$(DESTDIR)$(rdocdir)/manual"
uninstall-info:
	@$(ECHO) "uninstalling R info pages ..."
	@for f in $(OBJECTS_INFO); do \
	  $(INSTALL_INFO) --remove --info-dir=$(DESTDIR)$(infodir) $${f} \
	    || $(ECHO) "you need to adjust $(infodir)/dir manually"; \
	done
	@for f in $(OBJECTS_INFO:.info=.info*); do \
	  rm -f "$(DESTDIR)$(infodir)/$${f}" "$(DESTDIR)$(infodir)/$${f}.gz" ; \
	done
uninstall-pdf:
	@$(ECHO) "uninstalling R manuals in PDF format ..."
	@-rm -f "$(DESTDIR)$(rdocdir)/manual"/*.pdf

mostlyclean: clean
clean:
	@if test "$(BUILDDIR_IS_SRCDIR)" = no ; then \
	  rm -Rf $(INSTFILES) images; \
	fi
	-@rm -f *.aux *.cp *.cps *.dvi *.en *.ens *.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-*
	-@rm -f *.epub *.mobi rw-FAQ rw-FAQ.html
distclean: clean
	-@rm -f 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
	@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 -z "$(MAKEINFO)"; then \
	  $(ECHO) "ERROR: 'texi2any' v5.1 or later 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

front-matter: ${top_builddir}/doc/RESOURCES ${top_builddir}/doc/FAQ

${top_builddir}/doc/RESOURCES: $(srcdir)/resources.texi
	@if test -z "$(MAKEINFO)"; then \
	  $(ECHO) "'texi2any' v5.1 or later needed to make HTML docs but missing on your system." ; \
	  $(ECHO) "file doc/RESOURCES will be missing"; \
	else \
	  $(ECHO) "creating RESOURCES"; \
	  $(TEXI2TEXT) $(srcdir)/resources.texi -o $@; \
	fi

${top_builddir}/doc/FAQ: $(srcdir)/R-FAQ.texi
	@if test -z "$(MAKEINFO)"; then \
	  $(ECHO) "'texi2any' v5.1 or later needed to make HTML docs but missing on your system." ; \
	  $(ECHO) "file doc/FAQ will be missing"; \
	else \
	  $(ECHO) "creating FAQ"; \
	  $(TEXI2TEXT) $(srcdir)/R-FAQ.texi -o $@; \
	fi

html-non-svn: ../html/resources.html

../html/resources.html: $(srcdir)/resources.texi
	@if test -z "$(MAKEINFO)"; then \
	  $(ECHO) "'texi2any' v5.1 or later needed to make HTML docs but missing on your system." ; \
	  $(ECHO) "file doc/html/resources.html will be missing"; \
	else \
	  $(ECHO) "creating doc/html/resources.html"; \
	  $(MAKEINFO) --html --no-split --no-headers \
	    --css-include=$(top_srcdir)/doc/html/R.css $(srcdir)/resources.texi -o $@; \
	fi

## Added here to allow testing rw-FAQ changes on Unix.
Windows-FAQ: rw-FAQ rw-FAQ.html rw-FAQ.pdf

RVER = `cut -d' ' -f1 $(top_srcdir)/VERSION | $(SED) -n 1p`
RPREFIX = `../../bin$(R_ARCH)/Rscript $(top_srcdir)/src/gnuwin32/fixed/rwver.R`
SEDVER = -e s/@RVER@/$(RVER)/g -e s/@RWVER@/$(RPREFIX)/g

rw-FAQ.html: rw-FAQ.texi $(top_srcdir)/VERSION $(srcdir)/Rman.css
	@echo "making rw-FAQ.html"
	@$(SED) $(SEDVER) $< | \
	  $(MAKEINFO) --no-split --html --no-headers --number-sections --css-include=$(srcdir)/Rman.css -o $@ 

rw-FAQ: rw-FAQ.texi $(top_srcdir)/VERSION
	@echo "making rw-FAQ"
	@$(SED) $(SEDVER) $< | \
	  $(MAKEINFO) --no-headers --number-sections -o $@

rw-FAQ.pdf: rw-FAQ.texi $(top_srcdir)/VERSION
	@$(SED) $(SEDVER) $< > tmp.texi
	@$(TEXI2PDF) tmp.texi
	@mv tmp.pdf $@
	@rm tmp.*