#
# Makefile for octave's doc/interpreter directory
#
# John W. Eaton
# jwe@bevo.che.wisc.edu
# University of Wisconsin-Madison
# Department of Chemical Engineering

TOPDIR = ../..

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

include $(TOPDIR)/Makeconf

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

SCRIPT_SOURCES = sparseimages.m interpimages.m geometryimages.m plotimages.m

EXAMPLE_FILES_NODIR = \
  addtwomatrices.cc \
  celldemo.cc \
  firstmexdemo.c \
  fortdemo.cc \
  fortsub.f \
  funcdemo.cc \
  globaldemo.cc \
  helloworld.cc \
  mycell.c \
  myfeval.c \
  myfunc.c \
  mypow2.c \
  mysparse.c \
  mystring.c \
  mystruct.c \
  paramdemo.cc \
  stringdemo.cc \
  structdemo.cc \
  unwinddemo.cc

EXAMPLE_FILES = $(addprefix $(top_srcdir)/examples/, $(EXAMPLE_FILES_NODIR))

GEOMETRYIMAGES = voronoi triplot griddata convhull delaunay inpolygon
GEOMETRYIMAGES_EPS = $(addsuffix .eps, $(GEOMETRYIMAGES))
GEOMETRYIMAGES_PDF = $(addsuffix .pdf, $(GEOMETRYIMAGES))
GEOMETRYIMAGES_PNG = $(addsuffix .png, $(GEOMETRYIMAGES))

PLOTIMAGES = plot hist errorbar polar mesh plot3
PLOTIMAGES_EPS = $(addsuffix .eps, $(PLOTIMAGES))
PLOTIMAGES_PDF = $(addsuffix .pdf, $(PLOTIMAGES))
PLOTIMAGES_PNG = $(addsuffix .png, $(PLOTIMAGES))

INTERPIMAGES = interpft interpn interpderiv1 interpderiv2
INTERPIMAGES_EPS = $(addsuffix .eps, $(INTERPIMAGES))
INTERPIMAGES_PDF = $(addsuffix .pdf, $(INTERPIMAGES))
INTERPIMAGES_PNG = $(addsuffix .png, $(INTERPIMAGES))

SPARSEIMAGES_1 = gplot grid spmatrix spchol spcholperm
SPARSEIMAGES_EPS = $(addsuffix .eps, $(SPARSEIMAGES_1))
SPARSEIMAGES_PDF = $(addsuffix .pdf, $(SPARSEIMAGES_1))
SPARSEIMAGES_PNG = $(addsuffix .png, $(SPARSEIMAGES_1))
SPARSEIMAGES_TXT = $(addsuffix .txt, $(SPARSEIMAGES_1))

IMAGES_EPS = $(SPARSEIMAGES_EPS) $(INTERPIMAGES_EPS) \
	$(GEOMETRYIMAGES_EPS) $(PLOTIMAGES_EPS)
IMAGES_PDF = $(SPARSEIMAGES_PDF) $(INTERPIMAGES_PDF) \
	$(GEOMETRYIMAGES_PDF) $(PLOTIMAGES_PDF)
IMAGES_PNG = $(SPARSEIMAGES_PNG) $(INTERPIMAGES_PNG) \
	$(GEOMETRYIMAGES_PNG) $(PLOTIMAGES_PNG)
IMAGES_TXT = $(SPARSEIMAGES_TXT)

HTML_IMAGES_PNG = $(addprefix HTML/, $(IMAGES_PNG))

IMAGES = $(IMAGES_EPS) $(IMAGES_PDF) $(IMAGES_PNG) $(IMAGES_TXT)

SUB_SOURCE := arith.txi audio.txi basics.txi bugs.txi \
	container.txi control.txi cp-idx.txi data.txi \
	debug.txi diffeq.txi dynamic.txi emacs.txi errors.txi eval.txi \
	expr.txi finance.txi fn-idx.txi func.txi geometry.txi gpl.txi \
	grammar.txi image.txi install.txi interp.txi \
	intro.txi io.txi linalg.txi matrix.txi nonlin.txi numbers.txi \
	op-idx.txi optim.txi package.txi plot.txi poly.txi preface.txi \
	quad.txi quaternion.txi set.txi signal.txi sparse.txi stats.txi \
	stmt.txi strings.txi system.txi testfun.txi tips.txi var.txi \
	vr-idx.txi

SOURCES := $(SUB_SOURCE) $(SCRIPT_SOURCES)

MAIN_TEXINFO := $(srcdir)/octave.texi

SUB_TEXINFO := $(SUB_SOURCE:.txi=.texi)

# Don't list ../conf.texi here.
TEXINFO_SOURCE := $(MAIN_TEXINFO) $(SUB_TEXINFO) contributors.texi

TEXINFO := $(TEXINFO_SOURCE) ../conf.texi

TEXI2DVICOMMAND = TEXINPUTS="..:$(srcdir):$(srcdir)/..:$(TEXINPUTS):" \
	  $(UNSETCOMSPEC) $(TEXI2DVI) $(MAIN_TEXINFO) --output $@

TEXI2PDFCOMMAND = TEXINPUTS="..:$(srcdir):$(srcdir)/..:$(TEXINPUTS):" \
	  $(UNSETCOMSPEC) $(TEXI2PDF) $(MAIN_TEXINFO) --output $@

FORMATTED = octave.info octave.pdf octave-a4.pdf octave.info-[0-9]*

MAN_BASE := mkoctfile octave octave-bug octave-config
MAN_SRC := $(addsuffix .1, $(MAN_BASE))

# FIXME -- need to include generated figures here...
DISTFILES = $(addprefix $(srcdir)/, Makefile.in contributors.in \
  mkcontrib.awk dir  munge-texi.cc $(MAN_SRC) $(SOURCES) $(MAIN_TEXINFO)) \
  $(SUB_TEXINFO) contributors.texi $(FORMATTED) $(IMAGES)

DISTDIRS = HTML

DISTSUBDIRS :=

SUBDIRS :=

# Look for version.h to get version information.
xfiles := $(TOPDIR)/src/version.h $(srcdir)/$(TOPDIR)/src/version.h

version_file := $(firstword $(foreach file, $(xfiles), $(wildcard $(file))))

SPELL = $(patsubst %.texi, %.spell, $(TEXINFO))

%.spell : %.texi
	rm -f $@
	$(SED) -e 's/@@/ at /g' -e 's/@[a-zA-Z]*//g' $< | spell > $@.tmp
	mv $@.tmp $@

all: octave.info octave.pdf octave-a4.pdf HTML/index.html
.PHONY: all

$(TEXINFO): src-DOCSTRINGS scripts-DOCSTRINGS munge-texi$(BUILD_EXEEXT)

munge-texi$(BUILD_EXEEXT): munge-texi.cc
	$(BUILD_CXX) $(BUILD_CXXFLAGS) -o $@ $^ $(BUILD_LDFLAGS)

src-DOCSTRINGS:
	$(MAKE) -C ../../src DOCSTRINGS
.PHONY: src-DOCSTRINGS

scripts-DOCSTRINGS:
	$(MAKE) -C ../../scripts DOCSTRINGS
.PHONY: scripts-DOCSTRINGS

contributors.texi: contributors.in
	$(AWK) -f $(srcdir)/mkcontrib.awk $(srcdir)/contributors.in > $@-t
	@$(simple-move-if-change-rule)

$(SUB_TEXINFO) : %.texi : %.txi
	@echo making $@ from $<
	@./munge-texi \
	  -d $(TOPDIR)/src/DOCSTRINGS \
	  -d $(TOPDIR)/scripts/DOCSTRINGS < $< > $@-t
	@$(simple-move-if-change-rule)

octave.info: $(IMAGES_TXT) $(TEXINFO) $(EXAMPLE_FILES)
	-$(MAKEINFO) -I.. -I$(srcdir) -I$(srcdir)/.. $(MAIN_TEXINFO)

octave.dvi: $(IMAGES_EPS) $(TEXINFO) $(EXAMPLE_FILES)
	-TEXINPUTS="..:$(srcdir):$(srcdir)/..:$(TEXINPUTS):" \
	  $(UNSETCOMSPEC) $(TEXI2DVI) $(MAIN_TEXINFO)

octave-a4.dvi: $(IMAGES_EPS) $(TEXINFO) $(EXAMPLE_FILES)
	-$(TEXI2DVICOMMAND) -t @afourpaper

octave-smallbook.dvi: $(IMAGES_EPS) $(TEXINFO) $(EXAMPLE_FILES)
	-$(TEXI2DVICOMMAND) -t @smallbook

octave.ps: octave.dvi
	-dvips -o $@ $<

octave-a4.ps: octave-a4.dvi
	-dvips -o $@ $<

octave-smallbook.ps: octave-smallbook.dvi
	-dvips -o $@ $<

octave.pdf: $(IMAGES_PDF) $(TEXINFO) $(EXAMPLE_FILES)
	-$(TEXI2PDFCOMMAND)

octave-a4.pdf: $(IMAGES_PDF) $(TEXINFO) $(EXAMPLE_FILES)
	-$(TEXI2PDFCOMMAND) -t @afourpaper

octave-smallbook.pdf: $(IMAGES_PDF) $(TEXINFO) $(EXAMPLE_FILES)
	-$(TEXI2PDFCOMMAND) -t @smallbook

../../INSTALL.OCTAVE: install.texi
	rm -f INSTALL
	-$(MAKEINFO) -D INSTALLONLY \
	  --no-validate --no-headers --no-split --output INSTALL \
	  -I.. -I$(srcdir) -I$(srcdir)/.. $<
	mv INSTALL ../../INSTALL.OCTAVE

../../BUGS: bugs.texi
	rm -f BUGS
	-$(MAKEINFO) -D BUGSONLY \
	  --no-validate --no-headers --no-split --output BUGS \
	  -I.. -I$(srcdir) -I$(srcdir)/.. $<
	mv BUGS ../../BUGS

HTML/index.html: $(HTML_IMAGES_PNG) $(TEXINFO) $(EXAMPLE_FILES)
	-$(MAKEINFO) --html --ifinfo --output=HTML -I.. -I$(srcdir) -I$(srcdir)/.. $(MAIN_TEXINFO)

$(HTML_IMAGES_PNG): HTML/%.png : %.png
	@if [ -d HTML ]; then \
	  true; \
	else \
	  echo "creating HTML directory"; \
	  mkdir HTML; \
	fi
	cp $(filter-out html-dir, $<) HTML

## Sleep for a second after plotting to allow gnuplot to finish
## writing its output before Octave exits.
define run-octave
  $(TOPDIR)/run-octave -f -q -H -p $(srcdir) \
    --eval "$(notdir $(basename $<)) ('$(notdir $(basename $@))', '$(patsubst .%,%, $(suffix $@))'); sleep (1);"
endef

$(GEOMETRYIMAGES_EPS) $(GEOMETRYIMAGES_PNG) $(GEOMETRYIMAGES_TXT): geometryimages.m
	$(run-octave)

$(PLOTIMAGES_EPS) $(PLOTIMAGES_PNG) $(PLOTIMAGES_TXT): plotimages.m
	$(run-octave)

$(INTERPIMAGES_EPS) $(INTERPIMAGES_PNG) $(INTERPIMAGES_TXT): interpimages.m
	$(run-octave)

$(SPARSEIMAGES_EPS) $(SPARSEIMAGES_PNG) $(SPARSEIMAGES_TXT): sparseimages.m
	$(run-octave)

$(IMAGES_PDF) : %.pdf : %.eps
	if [ -f $< ] ; then $(GHOSTSCRIPT) -dBATCH -dEPSCrop -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$@ $< ; fi

check: all
.PHONY: check

install install-strip: all
	@$(subdir-for-command)
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(man1dir) $(DESTDIR)$(infodir)
	@if test -d $(DESTDIR)$(man1dir); then \
	  for f in $(MAN_BASE); do \
	    rm -f $(DESTDIR)$(man1dir)/$$f$(man1ext); \
	    echo "installing $(srcdir)/$$f.1 in $(DESTDIR)$(man1dir)"; \
	    $(INSTALL_DATA) $(srcdir)/$$f.1 $(DESTDIR)$(man1dir)/$$f$(man1ext); \
	  done ; \
	fi
	@if test -d $(DESTDIR)$(infodir); then \
	  rm -f $(DESTDIR)$(infodir)/octave.info*; \
	  echo "installing info files in $(DESTDIR)$(infodir)"; \
	  if test -f octave.info; then \
	    for f in octave.info*; do \
	      $(INSTALL_DATA) $$f $(DESTDIR)$(infodir)/$$f; \
	    done; \
	  else \
	    for f in $(srcdir)/octave.info*; do \
	      $(INSTALL_DATA) $$f $(DESTDIR)$(infodir)/`basename $$f`; \
	    done; \
	  fi; \
	  if test -f $(DESTDIR)$(infodir)/dir; then \
	    if grep "^\* Octave: (octave)." $(DESTDIR)$(infodir)/dir > /dev/null; then \
	      true; \
	    else \
	      echo ""; \
	      echo "You should add the following entry"; \
	      echo ""; \
	      echo "* Octave: (octave)."; \
	      echo "	Interactive language for numerical computations."; \
	      echo ""; \
	      echo "to $(DESTDIR)$(infodir)/dir."; \
	      echo ""; \
	    fi; \
	  else \
	    echo "installing $(srcdir)/dir in $(DESTDIR)$(infodir)"; \
	    $(INSTALL_DATA) $(srcdir)/dir $(DESTDIR)$(infodir)/dir; \
	  fi; \
	fi
.PHONY: install install-strip

uninstall:
	@$(subdir-for-command)
	rm -f $(infodir)/octave.info*
	for f in $(MAN_BASE); do rm -f $(DESTDIR)$(man1dir)/$$f$(man1ext); done
.PHONY: uninstall

tags: $(SOURCES)
	ctags $(SOURCES)

TAGS: $(SOURCES)
	etags $(SOURCES)

spell: $(SPELL)
.PHONY: spell

mostlyclean clean:
	@$(subdir-for-command)
	rm -f octave.cp octave.fn octave.in \
	octave.ky octave.op octave.pg octave.rd octave.tp octave.vr \
	octave.cps octave.fns octave.ins octave.kys octave.ops \
	octave.pgs octave.rds octave.tps octave.vrs octave.aux \
	octave.log octave.toc \
	munge-texi$(BUILD_EXEEXT) munge-texi.o
.PHONY: mostlyclean clean 

distclean: clean
	@$(subdir-for-command)
	rm -f Makefile 
.PHONY: distclean

maintainer-clean: clean clean-texi
	rm -f Makefile tags TAGS $(FORMATTED) $(IMAGES)
	rm -rf HTML
.PHONY: maintainer-clean

clean-texi:
	rm -f $(SUB_TEXINFO) contributors.texi
.PHONY: clean-texi

dist: clean-texi all
	ln $(DISTFILES) ../../`cat ../../.fname`/doc/interpreter
	for dir in $(DISTDIRS); do \
	  mkdir ../../`cat ../../.fname`/doc/interpreter/$$dir; \
	  ln ../../doc/interpreter/$$dir/* ../../`cat ../../.fname`/doc/interpreter/$$dir; \
	done
	for dir in $(DISTSUBDIRS); do mkdir ../../`cat ../../.fname`/doc/interpreter/$$dir; $(MAKE) -C $$dir $@; done
.PHONY: dist
