#
# Manual Page
#

install:
    mkdir -p $(INSTALL_MANDIR)/man1
    cp -f -m 444 omake.1 $(INSTALL_MANDIR)/man1
    ln-or-cp $(INSTALL_MANDIR)/man1/omake.1 $(INSTALL_MANDIR)/man1/osh.1


#
# Documentation files
#

BUILTIN = $(dir $(ROOT)/src/builtin)

MAN_BASE_FILES[] =
    $(BUILTIN)/omake_builtin_base.ml
    $(BUILTIN)/omake_builtin_arith.ml
    $(BUILTIN)/omake_builtin_fun.ml
    $(BUILTIN)/omake_builtin_object.ml
    $(BUILTIN)/omake_builtin_file.ml
    $(BUILTIN)/omake_builtin_test.ml

MAN_SYSTEM_FILES[] =
    $(BUILTIN)/omake_builtin_io.ml
    $(BUILTIN)/omake_builtin_io_fun.ml
    $(BUILTIN)/omake_builtin_sys.ml
    $(BUILTIN)/omake_builtin_shell.ml

MAN_PERVASIVES_FILES[] =
    $(LIB)/Pervasives.om
    $(BUILTIN)/omake_builtin_rule.ml
    $(BUILTIN)/omake_builtin_target.ml

MAN_ROOT_FILES[] =
    $(LIB)/build/Common.om
    $(LIB)/build/C.om
    $(LIB)/build/OCaml.om
    $(LIB)/build/LaTeX.om

MAN_CONFIGURE_FILES[] =
    $(LIB)/configure/Configure.om
    $(LIB)/configure/ncurses.om
    $(LIB)/configure/readline.om
    $(LIB)/configure/snprintf.om

MAN_BASE_FILES       = $(file $(MAN_BASE_FILES))
MAN_SYSTEM_FILES     = $(file $(MAN_SYSTEM_FILES))
MAN_PERVASIVES_FILES = $(file $(MAN_PERVASIVES_FILES))
MAN_ROOT_FILES       = $(file $(MAN_ROOT_FILES))
MAN_CONFIGURE_FILES  = $(file $(MAN_CONFIGURE_FILES))
MAN_ALL_FILES[] =
    $(MAN_BASE_FILES)
    $(MAN_SYSTEM_FILES)
    $(MAN_PERVASIVES_FILES)
    $(MAN_ROOT_FILES)
    $(MAN_CONFIGURE_FILES)

########################################################################
# Documentation
#
Shell. +=
   #
   # Extract the text between the following delimiters.
   #
   # \begin{doc}
   # ...
   # \end{doc}
   #
   extract-doc-text(argv) =
      print = false

      awk(b, $(argv))
      case $'\\end\{doc\}'
         print = false
         export
      case $'\\begin\{doc\}'
         print = true
         export
      default
         if $(print)
            println($0)

   remove-leading-comment-text(argv) =
      fsubst()
      case $'^[ \t]*[*#] ?'
         value

   extract-docs(argv) =
      extract-doc-text $(argv) | remove-leading-comment-text

#
# Extract the documentation from the src files.
#
ExtractManDocument(dst, files) =
    tex/$(dst).tex: $(files)
        extract-docs $(files) > $@

ExtractManDocument(omake-base,       $(MAN_BASE_FILES))
ExtractManDocument(omake-system,     $(MAN_SYSTEM_FILES))
ExtractManDocument(omake-pervasives, $(MAN_PERVASIVES_FILES))
ExtractManDocument(omake-root,       $(MAN_ROOT_FILES))
ExtractManDocument(omake-autoconf,   $(MAN_CONFIGURE_FILES))

#
# All the TeX files - both generated and the source ones.
#
TEX_FILES[] =
    src/omake-doc
    src/omake-index
    src/omake-examples
    src/omake-detail
    src/omake-build-examples
    src/omake-detail
    src/omake-language-naming
    src/omake-language-examples
    src/omake-options
    src/omake-language
    src/omake-detail
    src/omake-rules
    tex/omake-base
    tex/omake-system
    tex/omake-pervasives
    tex/omake-root
    tex/omake-autoconf
    src/osh
    src/omake-grammar

TEX_FILES = $(file $(addsuffix .tex, $(TEX_FILES)))

#
# HTML files that will be generated out of the ocaml-doc one
#
HTML_FILES[] =
    omake-doc.css
    omake-contents.html
    omake.html
    omake-quickstart.html
    omake-build-examples.html
    omake-language.html
    omake-detail.html
    omake-language-naming.html
    omake-language-examples.html
    omake-rules.html
    omake-base.html
    omake-system.html
    omake-shell.html
    omake-pervasives.html
    omake-build.html
    osh.html
    omake-options.html
    omake-grammar.html
    omake-all-index.html
    omake-var-index.html
    omake-fun-index.html
    omake-obj-index.html
    omake-target-index.html
    omake-option-index.html
    omake-references.html
    omake-toc.html
    omake-autoconf.html

HTML_FILES = $(file $(addprefix html/, $(HTML_FILES)))

#
# Generated info files.
#
# XXX: HACK: we do not know how many pieces we'd have. For now we hardcode the number.
#
NUM_INFOS = 11
INFO_BASE = $(file info/omake-doc.info)
INFO_FILES[] = $(INFO_BASE)
section
    i = 1
    while $(le $i, $(NUM_INFOS))
        INFO_FILES[] += $(file $(INFO_BASE)-$(i))
        i = $(add $i, 1)
    export INFO_FILES

public.HEVEA = hevea
public.HACHA = hacha
.STATIC:
    HEVEA_DIR =
    HEVEA_VERSION =
    HAVE_HEVEA_PROG = $(and $(CheckProg $(HEVEA)), $(CheckProg $(HACHA)))
    if $(HAVE_HEVEA_PROG)
        ConfMsgChecking(for $(HEVEA) configs and version)
        HEVEA_VERSION = $(shell $(HEVEA) -version)
        HEVEA_DIR = $(dir $(last $(HEVEA_VERSION)))
        HEVEA_VERSION = $(nth 1, $(HEVEA_VERSION))
        ConfMsgResult($(HEVEA_VERSION) in $(HEVEA_DIR))
        export HEVEA_DIR HEVEA_VERSION
    HEVEA_OK = $(and $(HAVE_HEVEA_PROG) $(test -d $(HEVEA_DIR)) $(test -f $(HEVEA_DIR)/hevea.sty))
    if $(not $(HEVEA_OK))
        ConfMsgWarn($'Hevea not found or Hevea installation is incomplete;')
        ConfMsgWarn($'will neither be able to build plain text, Info,')
        ConfMsgWarn($'nor HTML version of OMake documentation')

HEVEA_OPTIONS = -fix -I $(dir src) -I $(dir tex) -O -exec xxdate.exe
HACHA_OPTIONS = -tocter -nolinks

HEVEA_DEPS[] =
    $(glob src/*.hva)

if $(HEVEA_OK)
    .SUBDIRS: html
        omake-doc.html: $(TEX_FILES) $(HEVEA_DEPS) :value: $(HEVEA_VERSION)
            $(HEVEA) $(HEVEA_OPTIONS) omake-doc

        index.html: ../src/index.html
            cp ../src/index.html .

        $(HTML_FILES): omake-doc.html index.html
            $(HACHA) $(HACHA_OPTIONS) -o omake-toc.html $<

    if $(file-exists txt)
        .SUBDIRS: txt
            omake-doc.txt: $(TEX_FILES) $(HEVEA_DEPS) :value: $(HEVEA_VERSION)
               $(HEVEA) $(HEVEA_OPTIONS) -text omake-doc

    if $(file-exists info)
        .SUBDIRS: info
            $(INFO_FILES): $(TEX_FILES) $(HEVEA_DEPS) :value: $(HEVEA_VERSION)
                rm -f omake-doc.info*
                $(HEVEA) $(HEVEA_OPTIONS) -info omake-doc
                if $(test -e $(INFO_BASE)-$(add $(NUM_INFOS), 1))
                    eprintln($'Wrong number of info files: too many!')
                    eprintln($"Increase the NUM_INFOS (=$(NUM_INFOS)) variable in doc/OMakefile")
                    exit 1
                if $(not $(test -e $(INFO_BASE)-$(NUM_INFOS)))
                    eprintln($'Wrong number of info files: too few!')
                    eprintln($"Decrease the NUM_INFOS (=$(NUM_INFOS)) variable in doc/OMakefile")
                    exit 1

if $(file-exists ps)
   .SUBDIRS: ps
       TEXINPUTS[] += $(dir ../src ../tex $(HEVEA_DIR))

       omake-doc.tex: ../src/omake-doc.tex
           ln-or-cp $< $@

       TEXDEPS[] += $(TEX_FILES)

       LaTeXDocument(omake-doc, omake-doc)

dvi: ps/omake-doc.dvi
html: html/omake-doc.html $(HTML_FILES)
info: $(INFO_FILES)
pdf: ps/omake-doc.pdf
ps: ps/omake-doc.ps
txt: txt/omake-doc.txt

distclean:
    rm -f {txt,html,info}/*.{hind,h{t,o,f,v,opt}ind,htoc,haux} *.omc
    rm -f ps/*.{aux,fls,log,out,{t,o,v,f,opt}ids,ind,toc,ilg,{,v}idx,opts} ps/omake-doc.tex tex/*.tex

clean:
    rm -f html/* info/* ps/* tex/* txt/*
