#
#  Makefile.in
#
#  $Id: Makefile.in,v 1.7 2002/01/28 17:43:59 sano Exp $
#
#  Copyright 1996 Sven Rudolph
#

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
datadir=@datadir@
pkgdatadir=$(datadir)/linuxdoc-tools
LINUXDOCDOC=$(prefix)/share/doc/linuxdoc-tools
mandir=@mandir@
mandir1=$(mandir)/man1
PERL=@PERL@

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

#
# nsgmls or onsgmls should be installed already
# BUILD_NSGMLS=@BUILD_NSGMLS@

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

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

OPTIMIZE=-O

all: gen
ifeq ($(BUILD_SGMLSASP), true)
	@echo "Compiling the SGML translator tools (in sgmls-1.1/)..."
	( cd sgmls-1.1 ; \
	  $(MAKE) OPTIMIZE="$(OPTIMIZE)" sgmlsasp )
endif
# nsgmls or onsgmls should be installed already
# 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 )
	@echo "making man pages in genman ..." 
	if [ ! -d genman ]; then mkdir genman ; fi
	for ii in man/* ; do \
	  bn=`basename $$ii`; \
	  cat $$ii | sed \
	    -e "s,\$$LINUXDOCLIB,$(pkgdatadir)," \
	    -e "s,\$$LINUXDOCBIN,$(bindir)," \
	    > genman/$$bn; \
	done

install::
	@echo "Installing binaries in $(bindir) ..."
	if [ ! -d $(bindir) ]; then mkdir -p $(bindir); fi
	-chmod 755 $(bindir)
# nsgmls or onsgmls should be installed already
# 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/linuxdoc $(bindir)/linuxdoc
	@echo "Installing library and support files in $(pkgdatadir)..."
	if [ ! -d $(pkgdatadir) ]; then mkdir -p $(pkgdatadir); fi
	chmod 755 $(pkgdatadir)
	cp -r lib/* $(pkgdatadir)
	cp VERSION $(pkgdatadir)
	@echo "Installing man pages in $(mandir1) ..." 
	if [ ! -d $(mandir1) ]; then mkdir -p $(mandir1) ; fi
	-chmod 755 $(mandir1)
	for ii in genman/sgml2* genman/sgmlcheck.1 \
	          genman/rtf*.1 genman/sgmlpre*.1; do \
	  bn=`basename $$ii`; \
	  $(INSTALL_DATA) $$ii $(mandir1)/$$bn; \
	done
	$(INSTALL_DATA) genman/linuxdoc.1 $(mandir1)/linuxdoc.1
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 $(pkgdatadir) -type d -print | xargs chmod 755
	find $(pkgdatadir) -type f -print | xargs chmod 644
	(cd $(pkgdatadir)/dist/linuxdoc-tools/latin1; rm -f mapping; ln -s ../groff/mapping .)
	(cd $(pkgdatadir)/dist/linuxdoc-tools/nippon; rm -f mapping; ln -s ../groff/tr-mapping ./mapping)
	(cd $(pkgdatadir)/dist; rm -f linuxdoc; ln -s linuxdoc-tools linuxdoc)
	(cd $(pkgdatadir)/dist; rm -f linuxdoctr; ln -s linuxdoc-tools linuxdoctr)
	(cd $(pkgdatadir)/dist; rm -f linuxdoc96; ln -s linuxdoc-tools linuxdoc96)
	(cd $(pkgdatadir)/dist; rm -f linuxdoctr96; ln -s linuxdoc-tools linuxdoctr96)
	(cd $(pkgdatadir)/dist; rm -f linuxdoc97; ln -s linuxdoc-tools linuxdoc97)
	(cd $(pkgdatadir)/dtd;  rm -f linuxdoc.dtd; ln -s linuxdoc96.dtd linuxdoc.dtd)
	(cd $(pkgdatadir)/dtd;  rm -f linuxdoctr.dtd; ln -s linuxdoctr96.dtd linuxdoctr.dtd)
	(cd $(bindir); for i in $(progs); do rm -f $$i; ln -s linuxdoc $$i;done)
	@echo "Installing documentation in $(LINUXDOCDOC) ..."
	(cd doc; PATH=${PATH}:$(bindir) PREFIX=$(prefix) BINDIR=$(bindir) \
	 DATADIR=$(datadir) bash Makedoc.sh)
	if [ ! -d $(LINUXDOCDOC) ]; then mkdir -p $(LINUXDOCDOC); fi
	chmod 755 $(LINUXDOCDOC)
	cp -r doc/* $(LINUXDOCDOC)
	rm -f $(LINUXDOCDOC)/Makedoc.sh
	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)
	-rm -rf genman/

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!\@DATADIR\@!@datadir@/linuxdoc-tools!' \
	      -e 's!\@BINDIR\@!@bindir@!' \
	      -e 's!\@NSGMLS\@!@NSGMLS@!' \
	      -e 's!\@SGMLSASP\@!@SGMLSASP@!' \
	      -e 's!\@GROFF\@!@GROFF@!' \
	      -e 's!\@GROFFMACRO\@!@GROFFMACRO@!' \
	      -e 's!\@AWK\@!@AWK@!' \
	      -e 's!\@NKF\@!@NKF@!' \
	      -e 's!\@PERL\@!@PERL@!' \
	      -e 's!\@PERLWARN\@!@PERLWARN@!' \
	      >$@ )

