# Copyright (C) 2011-2020 Free Software Foundation, Inc.
#      Written by Deri James <deri@chuzzlewit.myzen.co.uk>
#      Automake migration by Bertrand Garrigues
#        <bertrand.garrigues@laposte.net>
#
# 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/>.

devpdf_srcdir = $(top_srcdir)/font/devpdf
GROFF_FONT_FILES=`cd $(top_builddir)/font/devpdf \
  && grep -ls internalname [A-CEG-Z]*`
ENC_FILES=`cd $(top_builddir)/font/devpdf; ls enc/*`
MAP_FILES=`cd $(top_builddir)/font/devpdf; ls map/*`

MOSTLYCLEANFILES += \
  font/devpdf/download \
  font/devpdf/DESC \
  font/devpdf/Foundry \
  font/devpdf/util/BuildFoundries

devpdffontdir = $(fontdir)/devpdf
devpdffont_DATA = \
  font/devpdf/DESC \
  font/devpdf/Foundry

devpdffontencdir = $(devpdffontdir)/enc
devpdffontenc_DATA = font/devpdf/enc/text.enc

devpdffontmapdir = $(devpdffontdir)/map
devpdffontmapdata = \
  font/devpdf/map/dingbats.map \
  font/devpdf/map/symbol.map \
  font/devpdf/map/symbolchars \
  font/devpdf/map/text.map
devpdffontmap_DATA = $(devpdffontmapdata)

EXTRA_DIST += \
  font/devpdf/DESC.in \
  font/devpdf/Foundry.in \
  font/devpdf/util/BuildFoundries.pl

all: font/devpdf/stamp

font/devpdf/enc/text.enc:
	$(AM_V_at)$(MKDIR_P) $(top_builddir)/font/devpdf/enc
	$(AM_V_at)cp -f $(top_srcdir)/font/devps/text.enc \
	  $(top_builddir)/font/devpdf/enc

$(devpdffontmapdata):
	$(AM_V_at)$(MKDIR_P) $(top_builddir)/font/devpdf/map
	$(AM_V_at)cp -f \
	  $(top_srcdir)/font/devps/generate/`basename $@` \
	  $(top_builddir)/font/devpdf/map

MOSTLYCLEANFILES += font/devpdf/util/BuildFoundries
font/devpdf/util/BuildFoundries: \
  $(devpdf_srcdir)/util/BuildFoundries.pl $(SH_DEPS_SED_SCRIPT) afmtodit
	$(AM_V_GEN)$(MKDIR_P) $(top_builddir)/font/devpdf/util \
	&& if test -n "$(PERL)"; then \
	     sed -f $(SH_DEPS_SED_SCRIPT) \
	         -e "s|/usr/bin/perl|$(PERL)|" \
	         -e "s|[@]GHOSTSCRIPT[@]|$(GHOSTSCRIPT)|" \
	         -e "s|[@]PATH_SEPARATOR[@]|$(PATH_SEPARATOR)|" \
	         -e "s|[@]VERSION[@]|$(VERSION)|" \
	         -e "s|[@]GROFF_FONT_DIR[@]|$(fontdir)|" \
	         $(devpdf_srcdir)/util/BuildFoundries.pl \
	         >$(top_builddir)/font/devpdf/util/BuildFoundries; \
	   else \
	     sed -f $(SH_DEPS_SED_SCRIPT) \
	         -e "s|[@]VERSION[@]|$(VERSION)|" \
	         -e "s|[@]GROFF_FONT_DIR[@]|$(fontdir)|" \
	         $(devpdf_srcdir)/util/BuildFoundries.pl \
	         >$(top_builddir)/font/devpdf/util/BuildFoundries; \
	   fi \
	&& chmod +x $(top_builddir)/font/devpdf/util/BuildFoundries

MOSTLYCLEANFILES += font/devpdf/DESC
font/devpdf/DESC: $(devpdf_srcdir)/DESC.in
	$(AM_V_GEN)$(MKDIR_P) $(top_builddir)/font/devpdf \
	&& cat $(devpdf_srcdir)/DESC.in \
	  >$(top_builddir)/font/devpdf/DESC \
	&& if test "$(PAGE)" = A4; then \
	     echo "papersize a4" >>$(top_builddir)/font/devpdf/DESC; \
	   else \
	     echo "papersize letter" \
	       >>$(top_builddir)/font/devpdf/DESC; \
	   fi

font/devpdf/Foundry: $(devpdf_srcdir)/Foundry.in
	$(AM_V_at)$(MKDIR_P) $(top_builddir)/font/devpdf/
if HAVE_URW_FONTS
	$(AM_V_GEN)sed "s|[@]urwfontsdir[@]|$(urwfontsdir)|" \
	  $(devpdf_srcdir)/Foundry.in >$@
else
	$(AM_V_GEN)sed "/BEGIN URW/,/END URW/d" \
	  $(devpdf_srcdir)/Foundry.in >$@
endif

# This is kind of a cheat, but works because BuildFoundries produces so
# many artifacts.
$(DEFAULT_BASE35_FONTS) $(URW_BASE35_FONTS): font/devpdf/download

MOSTLYCLEANFILES += font/devpdf/download
font/devpdf/download: $(devpdffontmapdata) font/devpdf/DESC \
  font/devpdf/Foundry font/devpdf/enc/text.enc \
  font/devpdf/util/BuildFoundries
	$(AM_V_GEN)$(MKDIR_P) $(top_builddir)/font/devpdf \
	&& echo "# foundry	ps-font-name	ps-font-file" >$@.tmp \
	&& echo "# A blank foundry is the default." >>$@.tmp \
	&& PATH="$(abs_top_builddir)$(GROFF_PATH_SEPARATOR)$(abs_top_builddir)/build-aux$(GROFF_PATH_SEPARATOR)$(PATH)" \
	     $(abs_top_builddir)/font/devpdf/util/BuildFoundries \
	       --strict $(abs_top_builddir)/font/devpdf \
	       '$(abs_top_srcdir)/font/devps:$(abs_top_builddir)/font/devps' \
	       >>$@.tmp \
	&& mv $@.tmp $@

# PDFs to be produced by troff should depend on this stamp file to
# ensure that gropdf's device and font description files are available.
MOSTLYCLEANFILES += font/devpdf/stamp
font/devpdf/stamp: font/devpdf/DESC font/devpdf/download \
  $(devpdffontenc_DATA) $(devpdffontmap_DATA) $(devpdffont_DATA)
	$(AM_V_at)>$@

mostlyclean-local: mostlyclean_devpdf_extra
mostlyclean_devpdf_extra:
	@echo Cleaning font/devpdf
	rm -rf $(top_builddir)/font/devpdf/enc \
	  $(top_builddir)/font/devpdf/map;
	if test -d $(top_builddir)/font/devpdf; then \
	  for f in $(GROFF_FONT_FILES); do \
	    rm -f $(top_builddir)/font/devpdf/$$f; \
	  done; \
	fi

install-data-local: install_devpdf
install_devpdf:
	@echo Installing devpdf files
	-@test -d $(DESTDIR)$(devpdffontdir) \
	  || $(mkinstalldirs) $(DESTDIR)$(devpdffontdir)
	@for f in $(GROFF_FONT_FILES); do \
	  $(RM) $(DESTDIR)$(devpdffontdir)/$$f; \
	  if test -f $$f; then \
	    $(INSTALL_DATA) $$f $(DESTDIR)$(devpdffontdir)/$$f; \
	  else \
	    $(INSTALL_DATA) $(top_builddir)/font/devpdf/$$f \
	      $(DESTDIR)$(devpdffontdir)/$$f; \
	  fi; \
	done
	$(INSTALL_DATA) $(top_builddir)/font/devpdf/download \
	  $(DESTDIR)$(devpdffontdir)/

uninstall_groffdirs: uninstall_devpdf
uninstall_devpdf:
	@echo Uninstalling devpdf files
	if test -d $(DESTDIR)$(devpdffontdir); then \
	  for f in $(GROFF_FONT_FILES); do \
	    rm -f $(DESTDIR)$(devpdffontdir)/$$f; \
	  done; \
	  if test -d $(DESTDIR)$(devpdffontencdir); then \
	    rmdir $(DESTDIR)$(devpdffontencdir); \
	  fi; \
	  if test -d $(DESTDIR)$(devpdffontmapdir); then \
	    rmdir $(DESTDIR)$(devpdffontmapdir); \
	  fi; \
	  rm -f $(DESTDIR)$(devpdffontdir)/download; \
	  rmdir $(DESTDIR)$(devpdffontdir); \
	fi

font_devpdf_default_test=\
  $(devpdf_srcdir)/tests/check-default-foundry.sh.in
font_devpdf_urw_test=$(devpdf_srcdir)/tests/check-urw-foundry.sh.in

font/devpdf/tests/check-default-foundry.sh: $(font_devpdf_default_test)
	$(AM_V_GEN)$(MKDIR_P) `dirname $@` \
	&& sed -e "s|[@]GHOSTSCRIPT[@]|$(GHOSTSCRIPT)|g" \
	  $(font_devpdf_default_test) > $@ \
	&& chmod +x $@

font/devpdf/tests/check-urw-foundry.sh: $(font_devpdf_urw_test)
	$(AM_V_GEN)$(MKDIR_P) `dirname $@` \
	&& sed -e "s|[@]urwfontsdir[@]|$(urwfontsdir)|g" \
	  $(font_devpdf_urw_test) > $@ \
	&& chmod +x $@

font_devpdf_TESTS = \
  font/devpdf/tests/check-default-foundry.sh \
  font/devpdf/tests/check-urw-foundry.sh

TESTS += $(font_devpdf_TESTS)
MOSTLYCLEANFILES += $(font_devpdf_TESTS)
EXTRA_DIST += $(font_devpdf_default_test) $(font_devpdf_urw_test)


# Local Variables:
# mode: makefile-automake
# fill-column: 72
# End:
# vim: set autoindent filetype=automake textwidth=72:
