#-*-Makefile-*-
#
# ${R_HOME}/doc/manual/Makefile.win

ECHO = echo

include ../../src/gnuwin32/MkRules

ifeq "$(MIKTEX)" "TRUE"
R_TEXOPTS=--include-directory=../../share/texmf/tex/latex
TEXI2DVI=texi2dvi
else
R_TEXOPTS=
TEXI2DVI=
endif

R_EXE=../../$(BINDIR)/Rterm

include ../../share/make/vars.mk

srcdir = .
top_srcdir = ../..
subdir = doc/manual

SOURCES_TEXI = R-FAQ.texi R-admin.texi R-data.texi R-exts.texi \
  R-intro.texi R-ints.texi R-lang.texi

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

MAKEINFO = LC_ALL=C makeinfo
PDFLATEX = pdflatex $(R_TEXOPTS)
PDFTEX = pdftex $(R_TEXOPTS)
BATCHPDFLATEX = $(PDFLATEX) -interaction=nonstopmode

MAKEINDEX = LC_ALL=C makeindex
TEXINDEX = LC_ALL=C texindex

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)

R_PAPERSIZE = a4
# omit 'hyper' to omit the hyperlinks
R_RD4PDF = times,inconsolata,hyper
#R_RD4PDF = lm,hyper
#R_RD4PDF = cm-super,hyper
#R_RD4PDF = ae,hyper

texinputs_BASE = $(R_PKGS_BASE:=-pkg.tex)
texinputs_RECOMMENDED = $(R_PKGS_RECOMMENDED:=-pkg.tex)
R_PKGS_refman = base datasets grDevices graphics grid methods splines stats stats4 tcltk tools utils
refman_TEXINPUTS = $(R_PKGS_refman:=-pkg.tex)
refman_DEPENDENCIES = version.tex $(top_srcdir)/share/texmf/tex/latex/Rd.sty
fullrefman_TEXINPUTS = $(texinputs_BASE) $(texinputs_RECOMMENDED)
SVN_REV = $(top_srcdir)/SVN-REVISION

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

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

all: pdf

.texi.html:
	@$(ECHO) "creating doc/manual/$@"
	@$(TEXI2HTML) -D UseExternalXrefs -I$(srcdir) $< -o $@ || touch $@

.texi.info:
	$(MAKEINFO) --enable-encoding -D UseExternalXrefs -I$(srcdir) $<

ifeq ($(strip $(TEXI2DVI)),)
.texi.pdf:
	$(PDFTEX) $<
	$(TEXINDEX) $*.cp $*.fn $*.vr
	$(PDFTEX) $<
	$(PDFTEX) $<
else
.texi.pdf:
	$(TEXI2DVI) --pdf --texinfo="@set UseExternalXrefs " $<
endif

R-admin.html: $(srcdir)/R-admin.texi
	@$(ECHO) "creating doc/manual/$@"
	@$(TEXI2HTML) -I$(srcdir) $(srcdir)/R-admin.texi -o $@ || touch $@

R-FAQ.html: R-FAQ.texi
	@$(ECHO) "creating doc/manual/$@"
	@$(MAKEINFO) --html --no-split --css-include=Rfaq.css -D UseExternalXrefs -I$(srcdir) $< -o $@ || touch $@


html: $(OBJECTS_HTML)
$(OBJECTS_HTML): $(texiincludes) Rman.css

info: $(OBJECTS_INFO)
$(OBJECTS_INFO): $(texiincludes)

pdf: refman.pdf $(OBJECTS_PDF)
$(OBJECTS_PDF): $(texiincludes)

refman.pdf: $(refman_TEXINPUTS) $(refman_DEPENDENCIES) \
   refman.top refman.bot
	@$(ECHO) " PDF/LaTeX documentation: reference index ..."
	@(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 $(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
	@$(RM) -f *.aux refman.toc refman.ind
	@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: $(fullrefman_TEXINPUTS) $(refman_DEPENDENCIES) \
   refman.top refman.bot
	@$(ECHO) " PDF/LaTeX documentation: reference index ..."
	@(opt="$(R_PAPERSIZE)paper"; \
	  $(ECHO) "\\documentclass[$${opt}]{book}"; \
	  opt="$(R_RD4PDF)"; \
	  $(ECHO) "\\usepackage[$${opt}]{Rd}"; \
	  $(ECHO) "\\usepackage[utf8]{inputenc}"; \
	  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 $(texinputs_RECOMMENDED); \
	    do $(ECHO) $${f}; done) | LC_COLLATE=C $(SORT)`; \
	  for f in $${texinputs}; do $(ECHO) "\\input{$${f}}"; done; \
	  cat $(srcdir)/refman.bot) > fullrefman.tex
	@$(RM) -f *.aux fullrefman.toc fullrefman.ind
	@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

version.tex: $(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}") > $@

## Unlike Unix, we don't check dates
%-pkg.tex: FORCE
	@$(ECHO) "collecting LaTeX docs for package \`$*' ..."
	@$(ECHO) "tools:::.pkg2tex(\"$(top_srcdir)/library/$*\")" \
	    | LC_ALL=C $(R_EXE) --slave --vanilla
FORCE:

version.texi: Makefile.win $(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=$(shell ../../bin$(R_ARCH)/Rscript ../../src/gnuwin32/fixed/rwver.R); \
	  $(ECHO) "@set RWVERSION $${rwv}" >> $@ )
	@if test "$(R_PAPERSIZE)" = "a4"; then \
	  $(ECHO) "@afourpaper" >> $@ ; \
	fi


mostlyclean: clean
clean:
	@-rm -f *.aux *.toc *refman.i?? *.out *.log
	@-rm -f *.cp *.cps *.en *.ens *.fn *.fns *.ky *.kys \
	  *.out *.pg *.pgs *.tmp *.tp *.vr *.vrs \
	  version.tex version.texi refman.tex fullrefman.tex *-pkg.tex
distclean: clean
	@-rm -f *.pdf *.info* *.html
maintainer-clean: distclean

../FAQ: R-FAQ.texi
	@$(TEXI2TEXT) -o $@ $<
../RESOURCES: resources.texi
	@$(TEXI2TEXT) -o $@ $<
../html/resources.html: resources.texi
	@$(TEXI2HTML) --no-headers -o $@ $<

## Run from src/gnuwin32/fixed/Makefile
svnonly: ../FAQ ../RESOURCES ../html/resources.html
