# Copyright (C) 2014-2020 Free Software Foundation, Inc.
#
# 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/>.

libgroff_srcdir = $(top_srcdir)/src/libs/libgroff
noinst_LIBRARIES += libgroff.a
libgroff_a_CPPFLAGS = \
  $(AM_CPPFLAGS) \
  -D__GETOPT_PREFIX=groff_ \
  -DENABLE_RELOCATABLE=1 \
  -DLIBDIR=\"$(libdir)\"

# Build from OBJS
libgroff_a_SOURCES = \
  src/libs/libgroff/assert.cpp \
  src/libs/libgroff/change_lf.cpp \
  src/libs/libgroff/cmap.cpp \
  src/libs/libgroff/color.cpp \
  src/libs/libgroff/cset.cpp\
  src/libs/libgroff/curtime.cpp \
  src/libs/libgroff/device.cpp \
  src/libs/libgroff/errarg.cpp \
  src/libs/libgroff/error.cpp \
  src/libs/libgroff/fatal.cpp \
  src/libs/libgroff/filename.cpp \
  src/libs/libgroff/font.cpp \
  src/libs/libgroff/fontfile.cpp \
  src/libs/libgroff/geometry.cpp \
  src/libs/libgroff/getopt.c \
  src/libs/libgroff/getopt1.c \
  src/libs/libgroff/glyphuni.cpp \
  src/libs/libgroff/htmlhint.cpp \
  src/libs/libgroff/hypot.cpp \
  src/libs/libgroff/iftoa.c \
  src/libs/libgroff/invalid.cpp \
  src/libs/libgroff/itoa.c \
  src/libs/libgroff/lf.cpp \
  src/libs/libgroff/lineno.cpp \
  src/libs/libgroff/localcharset.c \
  src/libs/libgroff/macropath.cpp \
  src/libs/libgroff/matherr.c \
  src/libs/libgroff/maxfilename.cpp \
  src/libs/libgroff/maxpathname.cpp \
  src/libs/libgroff/mksdir.cpp \
  src/libs/libgroff/nametoindex.cpp \
  src/libs/libgroff/paper.cpp \
  src/libs/libgroff/prime.cpp \
  src/libs/libgroff/progname.c \
  src/libs/libgroff/ptable.cpp \
  src/libs/libgroff/quotearg.c \
  src/libs/libgroff/relocate.cpp \
  src/libs/libgroff/searchpath.cpp \
  src/libs/libgroff/spawnvp.c \
  src/libs/libgroff/string.cpp \
  src/libs/libgroff/strsave.cpp \
  src/libs/libgroff/symbol.cpp \
  src/libs/libgroff/tmpfile.cpp \
  src/libs/libgroff/tmpname.cpp \
  src/libs/libgroff/unicode.cpp \
  src/libs/libgroff/uniglyph.cpp \
  src/libs/libgroff/uniuni.cpp \
  src/libs/libgroff/relocatable.h
if USE_GROFF_ALLOCATOR
libgroff_a_SOURCES += \
  src/libs/libgroff/new.cpp
endif
nodist_libgroff_a_SOURCES = src/libs/libgroff/version.cpp

# TODO: these .c files could be removed (use gnulib instead).
EXTRA_DIST += \
  src/libs/libgroff/mkstemp.cpp \
  src/libs/libgroff/fmod.c \
  src/libs/libgroff/getcwd.c \
  src/libs/libgroff/putenv.c \
  src/libs/libgroff/strcasecmp.c \
  src/libs/libgroff/strerror.c \
  src/libs/libgroff/strncasecmp.c \
  src/libs/libgroff/strtol.c \
  src/libs/libgroff/config.charset \
  src/libs/libgroff/ref-add.sin \
  src/libs/libgroff/ref-del.sin \
  src/libs/libgroff/make-uniuni

CLEANFILES += \
  src/libs/libgroff/version.cpp \
  charset.alias \
  ref-add.sed \
  ref-del.sed

# .o files have a 'libgroff_a-' prefix because we set
# libgroff_a_CPPFLAGS.
src/libs/libgroff/libgroff_a-device.$(OBJEXT): defs.h
src/libs/libgroff/libgroff_a-fontfile.$(OBJEXT): defs.h
src/libs/libgroff/libgroff_a-macropath.$(OBJEXT): defs.h
src/libs/libgroff/libgroff_a-relocate.$(OBJEXT): defs.h

src/libs/libgroff/version.cpp: $(top_srcdir)/.version
	$(AM_V_at)printf 'const char *version_string = "%s.%s";\n' \
	  $(MAJOR_VERSION) $(MINOR_VERSION) > $@.tmp
	$(AM_V_at)printf 'const char *revision_string = "%s";\n' \
	  $(REVISION) >> $@.tmp
	$(AM_V_at)printf \
	  'extern "C" {\nconst char *Version_string = "%s";\n}\n' \
	  $(VERSION) >> $@.tmp
	$(AM_V_GEN)mv $@.tmp $@

# Data for localcharset.c.  Taken from libiconv/libcharset.

LIBGROFF_PACKAGE = groff

all: charset.alias ref-add.sed ref-del.sed

charset.alias: $(libgroff_srcdir)/config.charset
	$(AM_V_GEN)$(SHELL) $(libgroff_srcdir)/config.charset \
	    '$(HOST)' > t-$@ \
	  && mv t-$@ $@

ref-add.sed: $(libgroff_srcdir)/ref-add.sin
	$(AM_V_GEN)sed -e '/^#/d' \
	    -e 's/@''PACKAGE''@/$(LIBGROFF_PACKAGE)/g' \
	    $(libgroff_srcdir)/ref-add.sin > t-$@ \
	  && mv t-$@ $@

ref-del.sed: $(libgroff_srcdir)/ref-del.sin
	$(AM_V_GEN)sed -e '/^#/d' \
	    -e 's/@''PACKAGE''@/$(LIBGROFF_PACKAGE)/g' \
	    $(libgroff_srcdir)/ref-del.sin > t-$@ \
	  && mv t-$@ $@

install-data-local: install_charset_data
install_charset_data:
	-test $(GLIBC21) != no || $(mkinstalldirs) $(DESTDIR)$(libdir)
	if test -f $(DESTDIR)$(libdir)/charset.alias; then \
	  sed -f ref-add.sed $(DESTDIR)$(libdir)/charset.alias \
	    > $(DESTDIR)$(libdir)/t-charset.alias; \
	  $(INSTALL_DATA) $(DESTDIR)$(libdir)/t-charset.alias \
	    $(DESTDIR)$(libdir)/charset.alias; \
	  rm -f $(DESTDIR)$(libdir)/t-charset.alias; \
	else \
	  if test $(GLIBC21) = no; then \
	    sed -f ref-add.sed charset.alias \
	      > $(DESTDIR)$(libdir)/t-charset.alias; \
	    $(INSTALL_DATA) $(DESTDIR)$(libdir)/t-charset.alias \
	      $(DESTDIR)$(libdir)/charset.alias; \
	    rm -f $(DESTDIR)$(libdir)/t-charset.alias; \
	  fi; \
	fi

uninstall-local: uninstall_charset_data
uninstall_charset_data:
	-if test -f $(DESTDIR)$(libdir)/charset.alias; then \
	  sed -f ref-del.sed $(DESTDIR)$(libdir)/charset.alias \
	    > $(DESTDIR)$(libdir)/t-charset.alias; \
	  if grep '^# Packages using this file: $$' \
	          $(DESTDIR)$(libdir)/t-charset.alias > /dev/null; then \
	    rm -f $(DESTDIR)$(libdir)/charset.alias; \
	  else \
	    $(INSTALL_DATA) $(DESTDIR)$(libdir)/t-charset.alias \
	      $(DESTDIR)$(libdir)/charset.alias; \
	  fi; \
	  rm -f $(DESTDIR)$(libdir)/t-charset.alias; \
	fi


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