
# %full_filespec: %

srcdir=../pfe
VPATH=$(srcdir)
DOCS=doc
SRCS=src
prefix=/usr/local
exec_prefix=${prefix}
infodir=${prefix}/info
groups=${infodir}/../groups
docdir=${infodir}/../doc
pkgdocdir=${docdir}/$(PACKAGE)
pkghtdocs=${groups}/$(PACKAGE3)/htdocs
PACKAGE3=p/pf/pfe
PACKAGE=pfe
VERSION=0.32.x
top_srcdir=..
mkinstalldirs=$(top_srcdir)/mkinstalldirs
PERLXM=perlxm
# v=v

doc: src/index.html wordsets.tar words.tar htmfiles.tar pfe.1

pfe.1 : ../pfe.spec
	-perl help2man.pl pfe > $@.tmp && mv $@.tmp $@ || touch $@

src/index.html: $(srcdir)/*.c
	$(MAKE) -C src these

src/index.xml src/all-fcode.xml src/all-wordsets.xml : $(srcdir)/*.c
	$(MAKE) -C src `basename $@`

DBKS=   dbk
DBK = 	how-to-write-a-pfe-module.dbk \
	internal-decisions-about-headers.dbk \
	special-implementation-strategies.dbk \
	tuning.dbk literature.dbk license.dbk lgpl-license.dbk \
	dirk-zoller-readme.dbk locals.dbk wordset-modules.dbk \
	make-dist.dbk statesmart-words.dbk known-problems.dbk \
        quoted-parse-idea.dbk porting-sbr-threading.dbk

htm_FILES = pfe.htm index.htm documentation.htm authors.htm license.htm \
            changes.htm
htm_EXTRA = body.htm

WWWTEKFTH=/net/WWW/entwicklung/forth
install-tek: $(DOCS).tar src/index.html
	( test -d $(WWWTEKFTH)/$(DOCS) || mkdir $(WWWTEKFTH)/$(DOCS) )
	( p=`pwd` ; cd $(WWWTEKFTH)/$(DOCS) && tar xvf $$p/$(DOCS).tar )
	( test -d $(WWWTEKFTH)/$(DOCS)/src || mkdir $(WWWTEKFTH)/$(DOCS)/src )
	cp src/*.html $(WWWTEKFTH)/$(DOCS)/src
	-chmod 775 $(WWWTEKFTH)/$(DOCS)/src
	@ for d in $(WWWTEKFTH)/$(DOCS)/* \
	; do if test -d $$d ; then echo chmod 775 $$d ; chmod 775 $$d ; fi \
	; done ; true
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*.html
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*/*.html
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*.htm
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*/*.htm
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*.xml
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*/*.xml
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*.gif
	-chmod 664 $(WWWTEKFTH)/$(DOCS)/*/*.gif

# >> pkgdocdir
doc : $(DOCS).tar
install-doc: $(DOCS).tar 
	@ echo ':::' $(DOCS).tar '>>' :$(pkgdocdir)/$(VERSION)
	@ echo mkinstalldirs $(DESTDIR)$(pkgdocdir)/$(VERSION) \
	;            test -d $(DESTDIR)$(pkgdocdir)/$(VERSION) \
	||  $(mkinstalldirs) $(DESTDIR)$(pkgdocdir)/$(VERSION)
	@ echo un-tar '>>' :$(pkgdocdir)/$(VERSION)
	@ p=`pwd` ; cd $(DESTDIR)$(pkgdocdir)/$(VERSION) \
	; tar x$vf $$p/$(DOCS).tar ; cd $$p ; true
	-cp ../ChangeLog ../COPYING.LIB $(DESTDIR)$(pkgdocdir)/$(VERSION)
	@ echo ':::' ln -s $(pkgdocdir)/$(VERSION)/'*' $(pkgdocdir)/$(PACKAGE)/
	@ (cd $(DESTDIR)$(pkgdocdir)/ && ln -s $(VERSION)/* .) || true

# >> pkghtdocs
docs : $(DOCS).tar $(SRCS).tar ../dbk/index.html
install-docs: $(DOCS).tar $(SRCS).tar ../dbk/index.html
	@ echo ':::' $(DOCS).tar '>>' :$(pkghtdocs)/$(VERSION)
	@ echo mkinstalldirs $(DESTDIR)$(pkghtdocs)/$(VERSION) \
	;            test -d $(DESTDIR)$(pkghtdocs)/$(VERSION) \
	||  $(mkinstalldirs) $(DESTDIR)$(pkghtdocs)/$(VERSION)
	@ echo un-tar '>>' :$(pkghtdocs)/$(VERSION)
	@ (p=`pwd` ; cd $(DESTDIR)$(pkghtdocs)/$(VERSION) \
	; tar x$vf $$p/$(DOCS).tar \
	; tar x$vf $$p/$(SRCS).tar \
	; mkdir manual ; cp $$p/../dbk/*.html manual \
	; cd manual ; test -d ../dpans && ln -s ../dpans .) ; true
	- cp ../ChangeLog ../COPYING.LIB $(DESTDIR)$(pkghtdocs)/$(VERSION)
	- (cd $(DESTDIR)$(pkghtdocs)/ && ln -s $(VERSION)/* .)

# -------------- sf refdoc -------------

$(DOCS).tar : words.tar wordsets.tar htmfiles.tar
	@ if test -f $(DOCS)/all-$(DOCS).build ; then : \
	; echo rm -r $(DOCS) ; sleep 4 ; rm -r $(DOCS) \
	; fi
	mkdir $(DOCS) ; date > $(DOCS)/all-$(DOCS).build
	@ echo un-tar words.tar '>>' $(DOCS)/words
	@ (cd $(DOCS) && tar xf ../words.tar)
	@ echo un-tar wordsets.tar '>>' $(DOCS)/wordsets
	@ (cd $(DOCS) && tar xf ../wordsets.tar)
	@ echo un-tar htmfiles.tar '>>' $(DOCS)
	@ (cd $(DOCS) && tar xf ../htmfiles.tar)
	@ if test -f dpans.ar \
	; then mkdir $(DOCS)/dpans \
	; echo un-ar dpans.ar '>>' $(DOCS)/dpans \
	; (cd $(DOCS)/dpans && ar x ../../dpans.ar \
	&& ln -s dpans.htm index.html) \
	; fi ; true
	@ if test -f ../dpans/dpansf.html \
	; then mkdir $(DOCS)/dpans \
	; echo cp "../dpans/*" '>>' $(DOCS)/dpans \
	; (cd $(DOCS)/dpans && cp ../../../dpans/* . \
	&& ln -s dpans.htm index.html) \
	; fi ; true
	@ if test -f 4thtutor.ar \
	; then mkdir $(DOCS)/4thtutor \
	; echo un-ar 4thtutor.ar '>>' $(DOCS)/4thtutor \
	; (cd $(DOCS)/4thtutor && ar x ../../4thtutor.ar \
	&& ln -s 4thtutor.htm index.html) \
	; fi ; true
	@ if test -f Forthprimer.pdf \
	; then : \
	; echo cp Forthprimer.pdf '>>' $(DOCS) \
	; cp Forthprimer.pdf $(DOCS)/Forthprimer.pdf \
	; fi ; true
	@ -cp *.gif $(DOCS)
	@ echo tar $@ '<<' $(DOCS)
	@ (cd $(DOCS) && tar chf ../$@ .)
	@ echo rm -rf $(DOCS) -- skipped

$(SRCS).tar : src/all-wordsets.xml src/index.html
	@ for i in $(SRCS)/*.xml \
	; do case $$i in all-*) \
	;; *) $(MAKE) `echo $$i | sed -e 's/.xml$$/.html/'` \
	;; esac \
	; done
	@ tar chf $@ $(SRCS)/*.html
	@ echo rm '$(SRCS)/*.html -- skipped'

# -------------- wordsets -------------

all-wordsets.xml : src/all-wordsets.xml
	@ sed -e 's:</*ALLWORDSETS>::' $< \
	| $(PERLXM) xm::pfe::flattenwordrefs: - >$@

cat-wordsets.xml : all-wordsets.xml src/all-fcode.xml
	@ if test -f dpans.ar ; then test -d ../dpans || mkdir ../dpans \
	; echo       cd ../dpans "&&" ar x :/dpans.ar  \
	; (p=`pwd` ; cd ../dpans && ar x $$p/dpans.ar) ; fi ; true
	@ cat all-wordsets.xml src/all-fcode.xml \
	| $(PERLXM) xm::pfe::expandxrefwordrefsfcode: - \
	| $(PERLXM) xm::pfe::dpansaddfooter: - -- --dpans=../dpans >$@

index-wordsets.xml : cat-wordsets.xml
	@ list=` $(PERLXM) xm::pfe::wordset2index: cat-wordsets.xml ` \
	; $(PERLXM) xm::navheaderhrefsfromindex: $@ \
	; for i in $$list \
	; do echo $$i:html ; $(MAKE) `basename $$i .xml`.html ; done \
	; test -s $@

index-words.xml : cat-wordsets.xml
	@ list=` $(PERLXM) xm::pfe::wordset2words: cat-wordsets.xml ` \
	; $(PERLXM) xm::navheaderhrefsfromindex: $@ \
	; for i in $$list \
	; do echo $$i:html ; $(MAKE) `basename $$i .xml`.html ; done \
	; test -s $@

words.tar : index-words.html
	@ if test -f words/all-words.build ; then : \
	; echo rm -r words ; sleep 4 ; rm -r words \
	; fi 
	mkdir words ; date > words/all-words.build
	@ for i in w-*.html w-*.xml \
	; do if test -f "$$i" \
	; then sed -e 's/index-words.html/index.html/' $$i >words/$$i \
	; echo rm $$i ; rm $$i \
	; fi done
	@ echo keep index-words.xml index-words.html '(cp words/index.html)'
	@ cp index-words.xml  words/index.xml  # do not break dependency
	@ cp index-words.html words/index.html # for the tar-file !
	@ tar chf $@ words
	rm -r words

wordsets.tar : index-wordsets.html
	@ if test -f wordsets/all-wordsets.build ; then : \
	; echo rm -r wordsets ; sleep 4 ; rm -r wordsets \
	; fi 
	mkdir wordsets ; date > wordsets/all-wordsets.build
	@ for i in wordset-*.html wordset-*.xml \
	; do if test -f "$$i" \
	; then sed -e 's/index-wordsets.html/index.html/' $$i >wordsets/$$i \
	; fi done
	@ cp index-wordsets.xml  wordsets/index.xml  # do not break dependency
	@ cp index-wordsets.html wordsets/index.html # for the tar-file !
	@ tar chf $@ wordsets
	rm -r wordsets

# -------------- defaults -------------
# should be generated by configure...
sf-html.html : sf-html.html.in
	sed -e 's/@prefix@//g' -e 's/@PACKAGE@//g' $< >$@
sf-news.html : sf-news.html.in
	sed -e 's/@VERSION@/the latest from sourceforge/' $< >$@

clean: clean-doc
clean-docs: clean
clean-doc: 
	rm -f *.html *.xml
	$(MAKE) -C src clean 
distclean-doc: clean-doc
	if test -f doc/all-doc.build ; then rm -r doc ; fi
	if test -f words/all-words.build ; then rm -r words ; fi
	if test -f wordsets/all-wordsets.build ; then rm -r wordsets ; fi
	if test -f dpans/rfilist.htm ; then rm -r dpans ; fi

# have a look at http://sourceforge.net/projects/xm-tool 
%.html : %.xml
	@ $(PERLXM) xm::css::mkhtml4: -- --ext=xml --css=PFE.css $< 
# --------------------------------------------------------- NEW DBK STYLE
SORRY = "Sorry, the doc build system had been missing xmlg from xmlg.sf.net"
../dbk/index.html : *.dbk ../dbk/*.dbk ../pfe/*.c
	if test -f dpans.ar ; then mkdir tmp ; mkdir tmp/dpans \
	; (cd tmp/dpans && ar x ../../dpans.ar) \
	; (cd .. && ln -s doc/tmp/dpans .) ; fi ; true
	(cd ../dbk && $(MAKE) -f pfe-fth.mk index.html pfe-fth.html \
	"DPANS=--dpans=../dpans --nofileref")
	test -s ../dbk/index.html || echo $(SORRY) > ../dbk/index.html
	- rm -r tmp/dpans || true

# --------------------------------------------------------- NEW HTM STYLE

htmfiles.tar : $(htm_FILES:.htm=.html)
	tar chf $@ $(htm_FILES:.htm=.html)

clean-htmfiles:
	-rm $(DBK:.dbk=.html) $(DBK:.dbk=.xml) index.html

datehead = <hr><table border="0" width="100%"><tr><td> \
    <a href="mailto:guidod@gmx.de?subject=$(PACKAGE):">guidod@gmx.de</a> \
    </td><td align="right">
datetail = </td></tr></table>
borderhead = <table cellpadding="10"><tr><td>
bordertail = </td></tr></table>
DATESPEC=`date +%Y-%m-%d`
VERSIONSPEC=`sed -e '/Version:/!d' -e 's/.*://' ../pfe.spec`

htmfiles : $(htm_FILES:.htm=.html)
$(htm_FILES:.htm=.html) : body.htm
%.html : %.htm
	@  D=$(DATESPEC) ; V=$(VERSIONSPEC) ; test -z "$$V" && V="$(VERSION)" \
	; title=`sed -e '/<h2>/!d' -e 's:</h2>.*::' \
        -e 's:<[^<>]*>::g' -e 'q' $<` \
	; cat body.htm | sed \
	-e "s|<!--VERSION-->|$$V|" \
	-e "s|<!--DATE-->|$$D|" \
	-e "s|</title>| - $$title</title>|" \
	-e "/$@.>/s|\\([^<>]*\\)</a>|<b>\\1</b></a>|" \
	> $@
	@ echo $< '>>' $@; cat $< | sed -e "s/<!--VERSION-->/$(VERSION)/" \
	-e "s:</h2>\\(.*\\):</h2></td><td><i>\\1</i></td></tr></table>:" \
	-e 's:<h2>:<table width="100%"><tr valign="bottom"><td><h2>:' \
	-e 's:<td><i>:<td align="right"><br> <i>:' \
	-e 's:<P>:<p class="justify"><span class="justify">:g' \
	-e 's:</P>:</span></p>:g' \
	-e 's,<date>,<!--DATE: ,' -e 's,</date>, -->,' \
	-e 's,<!--border-->,$(borderhead)<!--BORDER-->,' >>$@
	@ if grep '<!--BORDER-->' $@ >/dev/null ; then : \
	; echo "$(bordertail)<!--/BORDER-->" >>$@ ; fi
	@ D=`grep '<!--DATE:' $@ | sed -e 's,.*<!--DATE: *,,' -e 's, *-->,,'` \
	; if test ".$$D" != "." ; then echo '$(datehead)' >>$@ \
	; echo "$$D" >> $@ ; echo "$(datetail)" >> $@ ;	fi
	@ echo "</td></tr></table></body></html>" >>$@

# PKGHTDOCS=/groups/f/fr/freespace/htdocs/$(PACKAGE)
# PKGDOCDIR=${datadir}${PKGHTDOCS}
PKGHTDOCDIR=${pkghtdocs}
install-htmfiles : $(htm_FILES:.htm=.html) 
	$(mkinstalldirs) $(DESTDIR)$(PKGDOCDIR)
	$(INSTALL_DATA) $(htm_FILES:.htm=.html) $(DESTDIR)$(PKGDOCDIR)
	- (cd $(DESTDIR)$(PKGDOCDIR) && ln -s $(PACKAGE).html index.html)

$(PACKAGE).htm : ../$(PACKAGE).spec
	- mv $(PACKAGE).htm $(PACKAGE)-tmp.htm
	- pfe --help    > $(PACKAGE)-tmp.hlp
	- pfe --version > $(PACKAGE)-tmp.ver
	grep -i summary $(PACKAGE).spec | sed -e 's,.*:,,' > $(PACKAGE)-tmp.sum
	echo " <h2> "`cat $(PACKAGE)-tmp.sum` \
	    " </h2> "`cat $(PACKAGE)-tmp.ver` > $@
	echo "<!--border--><date>generated "$(DATESPEC)"</date><pre>" >> $@
	cat $(PACKAGE)-tmp.hlp | sed -e "/^[a-z]/s:.*:<b>&</b>:" >>$@
	echo "</pre>" >> $@
	test -s $@ || mv $(PACKAGE)-tmp.htm $@
	- rm $(PACKAGE)-tmp.*

changes.htm : ../ChangeLog
	echo "<pre>" > $@ ; cat $< | sed -e "s,\\&,\\&amp\\;,g" \
	-e "s,<,\\&lt\\;,g" -e "s,>,\\&gt\\;,g" \
	-e "/^[0-9]/s,\\(.*\\),<b>&</b>," >> $@ ; echo "</pre>" >>$@


