#
#  Makefile.in
#
#  $Id: Makefile.in,v 1.35 1998/10/16 19:06:43 cg Exp $
#
#  © Copyright 1996 Sven Rudolph
#

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@BINDIR@
libdir=@LIBDIR@
LINUXDOCDOC=$(prefix)/doc/sgml-tools
mandir=@mandir@
mandir1=$(mandir)/man1
PERL=@PERL@

INSTALL=@INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@ -m 755
INSTALL_DATA=@INSTALL_DATA@

BUILD_SGMLSASP=@BUILD_SGMLSASP@
BUILD_NSGMLS=@BUILD_NSGMLS@
BUILD_ENTITY_MAP=@BUILD_ENTITY_MAP@
BUILD_ISO_ENTITIES=@BUILD_ISO_ENTITIES@

GENFILES := bin/sgmltools
progs    := sgml2html sgml2info sgml2latex sgml2lyx sgml2rtf \
            sgml2txt sgmlcheck

OPTIMIZE=-O

all: gen
ifeq ($(BUILD_SGMLSASP), true)
	@echo "Compiling the SGML parsing tools (in sgmls-1.1/)..."
	( cd sgmls-1.1 ; \
	  $(MAKE) OPTIMIZE="$(OPTIMIZE)" sgmlsasp )
endif
ifeq ($(BUILD_NSGMLS), true)
	@echo "Compiling the SGML parsing tools (in sp/)..."
	( cd sp ; \
	  $(MAKE) )
endif
ifeq ($(BUILD_ENTITY_MAP), true)
	@echo "Building Entity Mapping library (in entity-map)..."
	( cd entity-map ; \
	  $(MAKE) )
endif
	@echo "Compiling preprocessor (in sgmlpre/)..."
	( cd sgmlpre ; \
	  $(MAKE) CFLAGS="$(OPTIMIZE)" LEX=flex sgmlpre || exit -1 )
	@echo "Compiling RTF conversion tools (in rtf-fix/)..."
	( cd rtf-fix ; \
	  $(MAKE) CFLAGS="$(OPTIMIZE)" || exit -1 )

install::
	@echo "Installing binaries in $(bindir) ..."
	if [ ! -d $(bindir) ]; then mkdir -p $(bindir); fi
	-chmod 755 $(bindir)
ifeq ($(BUILD_NSGMLS), true)
	$(INSTALL_PROGRAM) sp/nsgmls/nsgmls $(bindir)/nsgmls
endif
ifeq ($(BUILD_SGMLSASP), true)
	$(INSTALL_PROGRAM) sgmls-1.1/sgmlsasp $(bindir)/sgmlsasp
endif
ifeq ($(BUILD_ENTITY_MAP), true)
	@echo "Installing Entity Mapping library (in entity-map)..."
	( cd entity-map; \
	  $(MAKE) install )
endif
ifeq ($(BUILD_ISO_ENTITIES), true)
	@echo "Installing ISO character entity sets (in iso-entities)..."
	( cd iso-entities; \
	  $(MAKE) install )
endif
	strip sgmlpre/sgmlpre
	for ii in sgmlpre/sgmlpre rtf-fix/rtf2rtf; do \
	  bn=`basename $$ii`; \
	  $(INSTALL_PROGRAM) $$ii $(bindir)/$$bn; \
	done
	$(INSTALL_PROGRAM) bin/sgmltools $(bindir)/sgmltools.v1
	@echo "Installing library and support files in $(libdir)..."
	if [ ! -d $(libdir) ]; then mkdir -p $(libdir); fi
	chmod 755 $(libdir)
	cp -r lib/* $(libdir)
	cp VERSION $(libdir)
	@echo "Installing man pages in $(mandir1) ..." 
	if [ ! -d $(mandir1) ]; then mkdir -p $(mandir1) ; fi
	-chmod 755 $(mandir1)
	for ii in man/sgml2* man/sgmlcheck.1 man/sgmltools.1; do \
	  bn=`basename $$ii`; \
	  $(INSTALL_DATA) $$ii $(mandir1)/$$bn; \
	done
ifeq ($(BUILD_NSGMLS), true)
	$(INSTALL_DATA) man/nsgmls.1 $(mandir1)/nsgmls.1
endif
ifeq ($(BUILD_SGMLSASP), true)
	$(INSTALL_DATA) man/sgmlsasp.1 $(mandir1)/sgmlsasp.1
endif
	find $(libdir) -type d -print | xargs chmod 755
	find $(libdir) -type f -print | xargs chmod 644
	(cd $(libdir)/dist/sgmltool/latin1; rm -f mapping; ln -s ../groff/mapping .)
	(cd $(libdir)/dist; rm -f linuxdoc; ln -s sgmltool linuxdoc)
	(cd $(libdir)/dist; rm -f linuxdoc96; ln -s sgmltool linuxdoc96)
	(cd $(libdir)/dist; rm -f linuxdoc97; ln -s sgmltool linuxdoc97)
	(cd $(libdir)/dtd;  rm -f linuxdoc.dtd; ln -s linuxdoc96.dtd linuxdoc.dtd)
	(cd $(bindir); for i in $(progs); do rm -f $$i; ln sgmltools.v1 $$i;done)
	@echo "Installing documentation in $(LINUXDOCDOC) ..."
	(cd doc; PATH=${PATH}:$(bindir) sh Makedoc.sh)
	if [ ! -d $(LINUXDOCDOC) ]; then mkdir -p $(LINUXDOCDOC); fi
	chmod 755 $(LINUXDOCDOC)
	cp -r doc/* $(LINUXDOCDOC) 
	find $(LINUXDOCDOC) -type d -print | xargs chmod 755
	find $(LINUXDOCDOC) -type f -print | xargs chmod 644

# what dirs do the targets clean and distclean need?
DDIRS=
MDIRS=
ifeq ($(BUILD_SGMLSASP), true)
     DDIRS += sgmls-1.1
endif
ifeq ($(BUILD_NSGMLS), true)
     DDIRS += sp
endif
ifeq ($(BUILD_ENTITY_MAP), true)
     MDIRS += entity-map
endif
ifeq ($(BUILD_ISO_ENTITIES), true)
     MDIRS += iso-entities
endif

clean::
	-rm -f *~ bin/*~
	for d in $(DDIRS) $(MDIRS) rtf-fix; do $(MAKE) -C $$d clean; done
	(cd sgmlpre ; rm -f sgmlpre.o sgmlpre)

distclean:: clean
	for d in $(MDIRS); do $(MAKE) -C $$d distclean; done
	-rm -f Makefile config.cache config.log config.status \
		entity-map/Makefile iso-entities/Makefile \
		sgmls-1.1/config.h \
		sp/Makefile sp/Makefile.lib sp/config.log sp/config.status \
		sp/*/Makefile.dep \
		doc/guide.tex doc/guide.dvi doc/guide.ps doc/guide.ps.gz \
		doc/guide.info doc/guide.lyx doc/html/guide*.html \
		doc/rtf/guide*.rtf $(GENFILES)

gen : $(GENFILES)

#
#  Not very clean, I know. But it works for now. Any better
#  solutions to this are gladly accepted - CdG
#
% : %.in config.status
	@ [ $< = Makefile.in ] || ( \
	    echo Creating $@ ... ; \
	    rm -f $@ 2>/dev/null; cat $< | sed \
	      -e 's!\@prefix\@!@prefix@!' \
	      -e 's!\@LIBDIR\@!@LIBDIR@!' \
	      -e 's!\@BINDIR\@!@BINDIR@!' \
	      -e 's!\@NSGMLS\@!@NSGMLS@!' \
	      -e 's!\@SGMLSASP\@!@SGMLSASP@!' \
	      -e 's!\@GROFF\@!@GROFF@!' \
	      -e 's!\@GROFFMACRO\@!@GROFFMACRO@!' \
	      -e 's!\@PERL\@!@PERL@!' \
	      -e 's!\@PERLWARN\@!@PERLWARN@!' \
	      >$@ )
