# -*- makefile -*-
# Copyright (C) 2002-2014  Free Software Foundation, Inc.
#      Written by Werner Lemberg (wl@gnu.org)
#
# This file is part of groff.
#
# groff is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# groff is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# These may be overridden if cross-compiling.
GROFFBIN=$(top_builddir)/src/roff/groff/groff
GROFF_BIN_PATH=`echo $(groff_bin_dirs) | sed -e 's|  *|$(SH_SEP)|g'`
PDFMOMBIN=$(top_builddir)/src/devices/gropdf/pdfmom

groff_bin_dirs=\
  $(top_builddir)/src/roff/groff \
  $(top_builddir)/src/roff/troff \
  $(top_builddir)/src/devices/grops \
  $(top_builddir)/src/devices/gropdf

FFLAG=-F$(top_builddir)/font -F$(top_srcdir)/font
TFLAG=-M$(top_builddir)/tmac -M$(top_srcdir)/tmac -M$(srcdir)
KFLAG=-k

GROFF=\
  GROFF_COMMAND_PREFIX= \
  GROFF_BIN_PATH="$(GROFF_BIN_PATH)" \
  $(GROFFBIN) $(FFLAG) $(TFLAG)

PDFMOM=\
  GROFF_COMMAND_PREFIX= \
  GROFF_BIN_PATH="$(GROFF_BIN_PATH)" \
  PDFMOM_BIN_PATH="$(top_builddir)/src/devices/gropdf" \
  $(PDFMOMBIN) $(FFLAG) $(TFLAG) $(KFLAG)

MAN7=\
  groff_mom.n

NORMALFILES=\
  mom.tmac

# These files are processed with `strip.sed'.
STRIPFILES=\
  om.tmac

HTMLDOCFILES=\
  momdoc/stylesheet.css \
  momdoc/appendices.html \
  momdoc/color.html \
  momdoc/cover.html \
  momdoc/definitions.html \
  momdoc/docelement.html \
  momdoc/docprocessing.html \
  momdoc/goodies.html \
  momdoc/graphical.html \
  momdoc/headfootpage.html \
  momdoc/images.html \
  momdoc/inlines.html \
  momdoc/intro.html \
  momdoc/letters.html \
  momdoc/macrolist.html \
  momdoc/rectoverso.html \
  momdoc/refer.html \
  momdoc/reserved.html \
  momdoc/tables-of-contents.html \
  momdoc/toc.html \
  momdoc/typesetting.html \
  momdoc/using.html \
  momdoc/version-2.html

EXAMPLEFILES=\
  examples/letter.mom \
  examples/mom-pdf.mom \
  examples/sample_docs.mom \
  examples/typesetting.mom \
  examples/README.txt \
  examples/elvis_syntax \
  examples/elvis_syntax.new \
  examples/penguin.ps \
  examples/penguin.pdf \
  examples/mom.vim

PROCESSEDEXAMPLEFILES=\
  examples/letter.pdf \
  examples/mom-pdf.pdf \
  examples/sample_docs.pdf \
  examples/typesetting.pdf

HTMLDOCFILES_=`echo $(HTMLDOCFILES) | sed 's|momdoc/||g'`
EXAMPLEFILES_=`echo $(EXAMPLEFILES) | sed 's|examples/||g'`
PROCESSEDEXAMPLEFILES_=`echo $(PROCESSEDEXAMPLEFILES) | sed 's|examples/||g'`
PDFDOCFILE=mom-pdf.pdf

MOSTLYCLEANADD=\
  stamp-strip \
  penguin.ps \
  penguin.pdf \
  $(PROCESSEDEXAMPLEFILES) \
  examples/stamp \
  om.tmac-s

RM=rm -f

#.SUFFIXES: .mom .ps
#.mom.ps:
#	$(GROFF) -Tps -mom $< >$@

.SUFFIXES: .mom .pdf
.mom.pdf:
	LC_ALL=C $(PDFMOM) $< >$@

all: stamp-strip $(make_pdfexamples)

$(PROCESSEDEXAMPLEFILES): penguin.ps penguin.pdf examples/stamp

pdfexamples: $(PROCESSEDEXAMPLEFILES)

penguin.ps:
	cp $(srcdir)/examples/penguin.ps .
penguin.pdf:
	cp $(srcdir)/examples/penguin.pdf .

examples/stamp:
	-test -d examples || $(mkinstalldirs) examples
	touch $@

install_data: install_always \
	      $(make_install_pdfdoc) $(make_install_shipped_htmldoc) \
	      $(make_install_examples)

install_always: stamp-strip $(NORMALFILES)
	-test -d $(DESTDIR)$(tmacdir) || $(mkinstalldirs) $(DESTDIR)$(tmacdir)
	for f in $(NORMALFILES); do \
	  $(RM) $(DESTDIR)$(tmacdir)/$$f; \
	  $(INSTALL_DATA) $(srcdir)/$$f $(DESTDIR)$(tmacdir)/$$f; \
	done
	for f in $(STRIPFILES); do \
	  $(RM) $(DESTDIR)$(tmacdir)/$$f; \
	  $(INSTALL_DATA) $$f-s $(DESTDIR)$(tmacdir)/$$f; \
	done

install_pdfdoc:
# Since this uses examples/, it's in install_pdfexamples

install_shipped_htmldoc: install_always $(HTMLDOCFILES)
	-test -d $(DESTDIR)$(htmldocdir)/mom \
	  || $(mkinstalldirs) $(DESTDIR)$(htmldocdir)/mom
	for f in $(HTMLDOCFILES_); do \
	  $(RM) $(DESTDIR)$(htmldocdir)/mom/$$f; \
	  $(INSTALL_DATA) $(srcdir)/momdoc/$$f \
	    $(DESTDIR)$(htmldocdir)/mom/$$f; \
	done


install_examples: install_examples_always $(make_install_pdfexamples)

install_examples_always: $(EXAMPLEFILES)
	-test -d $(DESTDIR)$(exampledir)/mom \
	  || $(mkinstalldirs) $(DESTDIR)$(exampledir)/mom
	for f in $(EXAMPLEFILES_); do \
	  $(RM) $(DESTDIR)$(exampledir)/mom/$$f; \
	  $(INSTALL_DATA) $(srcdir)/examples/$$f \
	    $(DESTDIR)$(exampledir)/mom/$$f; \
	done

install_pdfexamples: install_examples_always $(PROCESSEDEXAMPLEFILES)
	for f in $(PROCESSEDEXAMPLEFILES_); do \
	  $(RM) $(DESTDIR)$(exampledir)/mom/$$f; \
	  $(INSTALL_DATA) examples/$$f $(DESTDIR)$(exampledir)/mom/$$f; \
	done
	-test -d $(DESTDIR)$(pdfdocdir) \
	  || $(mkinstalldirs) $(DESTDIR)$(pdfdocdir)
	for f in $(PDFDOCFILE); do \
	  $(RM) $(DESTDIR)$(pdfdocdir)/$$f; \
	  ln -s $(exampledir)/mom/$$f $(DESTDIR)$(pdfdocdir)/$$f; \
	done

stamp-strip: $(STRIPFILES)
	for f in $(STRIPFILES); do \
	  $(RM) $$f-s; \
	  LANG=C LC_ALL=C \
	    sed -f $(top_srcdir)/tmac/strip.sed $(srcdir)/$$f >$$f-s; \
	done
	touch $@

uninstall_sub: uninstall_always \
		$(make_uninstall_pdfdoc) $(make_uninstall_shipped_htmldoc) \
		$(make_uninstall_examples)

uninstall_always:
	-for f in $(NORMALFILES) $(STRIPFILES); do \
	  $(RM) $(DESTDIR)$(tmacdir)/$$f; \
	done

uninstall_pdfdoc: uninstall_always
# Since that used examples/, it's in uninstall_pdfexamples

uninstall_shipped_htmldoc: uninstall_always
	-for f in $(HTMLDOCFILES_); do \
	  $(RM) $(DESTDIR)$(htmldocdir)/mom/$$f; \
	done
	-test -d $(DESTDIR)$(htmldocdir)/mom && \
	  rmdir $(DESTDIR)$(htmldocdir)/mom

uninstall_examples: uninstall_examples_always $(make_uninstall_pdfexamples)
	-test -d $(DESTDIR)$(exampledir)/mom && \
	  rmdir $(DESTDIR)$(exampledir)/mom

uninstall_examples_always:
	-for f in $(EXAMPLEFILES_); do \
	  $(RM) $(DESTDIR)$(exampledir)/mom/$$f; \
	done

uninstall_pdfexamples: uninstall_examples_always
	-for f in $(PDFDOCFILE); do \
	  $(RM) $(DESTDIR)$(pdfdocdir)/$$f; \
	done
	-for f in $(PROCESSEDEXAMPLEFILES_); do \
	  $(RM) $(DESTDIR)$(exampledir)/mom/$$f; \
	done
