# Copyright (C) 2005-2014  Free Software Foundation, Inc.
#      Written by Keith Marshall (keith.d.marshall@ntlworld.com)
#
# 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/>.

MAN1=\
  pdfroff.n

CMDFILES=\
  pdfroff

TMACFILES=\
  pdfmark.tmac \
  spdf.tmac

PDFDOCFILES=\
  pdfmark.pdf

MOSTLYCLEANDIRADD=\
  pdfroff-*

MOSTLYCLEANADD=\
  gnu.eps \
  $(PDFDOCFILES) \
  $(CMDFILES) \
  pdf[0-9]*

GROFF_BIN_DIR=$(top_builddir)/src/roff/groff
GROFF_OTHER_BIN_DIRS=\
  $(top_builddir)/src/roff/troff \
  $(top_builddir)/src/devices/grops
GROFF_BIN_DIRS=$(GROFF_BIN_DIR) $(GROFF_OTHER_BIN_DIRS)
GROFF_BIN_PATH=`echo $(GROFF_BIN_DIRS) | sed -e 's|  *|$(SH_SEP)|g'`

FFLAG=-F$(top_builddir)/font -F$(top_srcdir)/font
MFLAG=-M$(srcdir) -M$(top_builddir)/tmac -M$(top_srcdir)/tmac
PFLAG=-dpaper=$(PAGE) -P-p$(PAGE)

PDFROFF=\
  GROFF_TMPDIR=. \
  GROFF_COMMAND_PREFIX= \
  GROFF_BIN_DIR="$(GROFF_BIN_DIR)" \
  GROFF_BIN_PATH="$(GROFF_BIN_PATH)" \
  ./pdfroff --keep-temporary-files $(FFLAG) $(MFLAG) $(PFLAG)

RM=rm -f

.SUFFIXES: .ms .pdf
.ms.pdf:
	$(PDFROFF) -mspdf --stylesheet=$(srcdir)/cover.ms $< >$@

all: pdfroff $(make_pdfdoc)

# The pdf files use the local script to generate.
$(PDFDOCFILES): pdfroff

pdfdoc: gnu.eps $(PDFDOCFILES)

gnu.eps:
	if test -f $(top_srcdir)/doc/gnu.eps; then \
	  cp $(top_srcdir)/doc/gnu.eps . ; \
	elif test -f $(top_builddir)/doc/gnu.eps; then \
	  cp $(top_builddir)/doc/gnu.eps . ; \
	else \
	  xpmtoppm $(top_srcdir)/doc/gnu.xpm | pnmdepth 15 \
	    | $(pnmtops_nosetpage) -noturn -rle >$@ ; \
	fi

pdfroff: pdfroff.sh $(SH_DEPS_SED_SCRIPT)
	sed -f $(SH_DEPS_SED_SCRIPT) \
	    -e "s|@VERSION@|$(version)$(revision)|" \
	    -e "s|@GROFF_AWK_INTERPRETERS@|$(ALT_AWK_PROGS)|" \
	    -e "s|@GROFF_GHOSTSCRIPT_INTERPRETERS@|$(ALT_GHOSTSCRIPT_PROGS)|" \
	    -e "s|@GROFF_BIN_DIR@|$(bindir)|" $(srcdir)/pdfroff.sh >$@
	chmod +x $@

install_data: install_always $(make_install_pdfdoc)

install_always:
	-test -d $(DESTDIR)$(bindir) || $(mkinstalldirs) $(DESTDIR)$(bindir)
	for f in $(CMDFILES); do \
	  $(RM) $(DESTDIR)$(bindir)/$$f; \
	  $(INSTALL_SCRIPT) $$f $(DESTDIR)$(bindir)/$$f; \
	done
	-test -d $(DESTDIR)$(tmacdir) || $(mkinstalldirs) $(DESTDIR)$(tmacdir)
	for f in $(TMACFILES); do \
	  $(RM) $(DESTDIR)$(tmacdir)/$$f; \
	  $(INSTALL_DATA) $(srcdir)/$$f $(DESTDIR)$(tmacdir)/$$f; \
	done

install_pdfdoc: install_always
	-test -d $(DESTDIR)$(pdfdocdir) \
	  || $(mkinstalldirs) $(DESTDIR)$(pdfdocdir)
	for f in $(PDFDOCFILES); do \
	  $(RM) $(DESTDIR)$(pdfdocdir)/$$f; \
	  $(INSTALL_DATA) $$f $(DESTDIR)$(pdfdocdir)/$$f; \
	done

uninstall_sub: uninstall_always $(make_uninstall_pdfdoc)

uninstall_always:
	-for f in $(CMDFILES); do $(RM) $(DESTDIR)$(bindir)/$$f; done
	-for f in $(TMACFILES); do $(RM) $(DESTDIR)$(tmacdir)/$$f; done

uninstall_pdfdoc: uninstall_always
	-for f in $(PDFDOCFILES); do $(RM) $(DESTDIR)$(pdfdocdir)/$$f; done

# Emacs settings
########################################################################
#
# Local Variables:
# mode: makefile
# End:
