m = a6le
Scheme=../$m/bin/$m/scheme -b ../$m/boot/$m/petite.boot -b ../$m/boot/$m/scheme.boot
STEXLIB=../stex
installdir=/tmp/csug9
INSTALL=../$m/installsh

x = csug
latex = pdflatex
stexmacrofiles = tspl4-prep
bib = $(x).bib
index=yes
TSPL=tspl4
DIR=$(shell basename `pwd`)

target: logcheck1 logcheck2 checklibs $(x).html

install: target
	$(INSTALL) -m 2755 -d $(installdir)
	$(INSTALL) -m 0644 --ifdiff *.html *.css $(installdir)
	$(INSTALL) -m 2755 -d $(installdir)/canned
	$(INSTALL) -m 0644 --ifdiff canned/* $(installdir)/canned
	$(INSTALL) -m 2755 -d $(installdir)/gifs
	$(INSTALL) -m 0644 --ifdiff gifs/*.gif $(installdir)/gifs
	$(INSTALL) -m 2755 -d $(installdir)/$(mathdir)
	$(INSTALL) -m 0644 --ifdiff $(mathdir)/*.gif $(installdir)/$(mathdir)
	(cd $(installdir); ln -s -f $(x).html index.html)

# thrice is not enough when starting from scratch
logcheck1: $(x).thirdrun
	@if [ -n "`grep 'Warning: Label(s) may have changed' $(x).log`" ] ; then\
            /bin/rm -f $(x).thirdrun ;\
            $(MAKE) $(x).thirdrun;\
         fi

rerun: $(x).thirdrun

logcheck2: $(x).thirdrun
	@if [ -n "`grep Warning $(x).log | grep -v pdftex.map`" ] ; then\
            echo "`grep Warning $(x).log | grep -v pdftex.map`";\
            false;\
         fi
	@if [ -n "`grep Overfull $(x).log | grep -v pdftex.map`" ] ; then\
            echo "`grep Overfull $(x).log | grep -v pdftex.map`";\
            false;\
         fi

include $(STEXLIB)/Mf-stex

stexsrc = csug.stex title.stex copyright.stex contents.stex\
 preface.stex intro.stex use.stex expeditor.stex debug.stex foreign.stex\
 binding.stex control.stex objects.stex numeric.stex io.stex\
 libraries.stex syntax.stex system.stex smgmt.stex threads.stex\
 compat.stex bibliography.stex summary.stex
texsrc = ${stexsrc:%.stex=%.tex}

title.tex contents.tex bibliography.tex:
	/bin/rm -f $*.tex
	echo "%%% DO NOT EDIT THIS FILE" > $*.tex
	echo "%%% Edit the .stex version instead" >> $*.tex
	echo "" >> $*.tex
	cat $*.stex >> $*.tex
	chmod -w $*.tex
title.tex: title.stex
contents.tex: contents.stex
bibliography.tex: bibliography.stex

$(x).firstrun: $(x).prefirstrun
$(x).prefirstrun: tspl.aux tspl.rfm tspl.idx
	touch $x.sfm
	cat tspl.aux > $x.aux
	cat tspl.rfm > $x.rfm
	cat tspl.idx > $x.idx
	touch $(x).prefirstrun

$(x).secondrun: $(x).presecondrun
$(x).presecondrun: $(x).firstrun
	cat tspl.aux >> $x.aux
	cat tspl.rfm >> $x.rfm
	echo '(summary-make "$x")' | $(Scheme) setup.ss summary.ss
	cat tspl.idx >> $x.idx
	touch $(x).presecondrun

$(x).thirdrun: $(x).prethirdrun canned/cisco-logo.png
$(x).prethirdrun: $(x).secondrun
	cat tspl.aux >> $x.aux
	cat tspl.rfm >> $x.rfm
	echo '(summary-make "$x")' | $(Scheme) setup.ss summary.ss
	cat tspl.idx >> $x.idx
	touch $(x).prethirdrun

$(x).hfirstrun: $(x).hprefirstrun csug8.hcls
$(x).hprefirstrun: $(x).thirdrun tspl.haux in.hidx
	cat tspl.aux >> $x.aux
	cat tspl.rfm >> $x.rfm
	cat tspl.idx >> $x.idx
	cat tspl.haux > $x.haux
	touch $(x).hprefirstrun

$(x).hsecondrun: $(x).hpresecondrun
$(x).hpresecondrun: $(x).hfirstrun
	cat tspl.haux >> $x.haux
	touch $(x).hpresecondrun

$(x).hthirdrun: $(x).hprethirdrun
$(x).hprethirdrun: $(x).hsecondrun
	cat tspl.haux >> $x.haux
	touch $(x).hprethirdrun

$(x).prefirstrun: $(texsrc) csug8.cls csug810.clo

$(x).firstrun: scheme.sty

tspl.aux: ${TSPL}/tspl.aux
	cat ${TSPL}/*.aux | grep '\\newlabel' | \
         sed -e 's/\\newlabel{\(.*\){\([^}]*\)}}/\\newlabel{TSPL:\1{t\2}}/' > tspl.aux

tspl.haux: ${TSPL}/tspl.haux
	sed -e 's/(putprop (quote /(putprop (quote |TSPL|:/' ${TSPL}/tspl.haux | \
         sed -e 's;url) ";url) "http://scheme.com/${TSPL}/;' > tspl.haux

tspl.rfm: ${TSPL}/tspl.rfm
	sed -e 's/\\pageref{/\\pageref{TSPL:/' ${TSPL}/tspl.rfm > tspl.rfm

# this version leaves tspl entries out of the printed index
#tspl.idx:
#	echo -n > tspl.idx

# this version includes tspl entries in the printed index
tspl.idx: ${TSPL}/tspl.idx
	sed -e 's/{\([1-9][0-9]*\)}$$/{t\1}/' ${TSPL}/tspl.idx | \
         sed -e 's/{\([ivx][ivx]*\)}$$/{t\1}/' > tspl.idx

in.hidx: ${TSPL}/out.hidx
	sed -e 's;"\(.*\)\.html#;"http://scheme.com/${TSPL}/\1.html#;' ${TSPL}/out.hidx | \
         sed -e 's/"")$$/"t")/' > in.hidx

$(texsrc): tspl4-prep.stex priminfo.ss ../s/primdata.ss

checklibs: $(x).thirdrun
	sort libsrecorded | uniq > libsrecorded.sort
	sort libslisted | uniq > libslisted.sort
	diff libsrecorded.sort libslisted.sort

code: $(stexsrc)
	extract.pl $(stexsrc) > code
	echo '(load "code" pretty-print)' | $(Scheme) -q

$(x).clean:
	-/bin/rm -f $(x).rfm $(x).sfm $(x).prefirstrun $(x).presecondrun\
                    $(x).prethirdrun $(x).ans\
                    $(x).hprefirstrun $(x).hpresecondrun $(x).hprethirdrun\
                    tspl.aux tspl.haux tspl.rfm tspl.idx in.hidx\
                    libsrecorded{,.sort} libslisted{,.sort}\
                    code

$(x).reallyclean:

$(x).reallyreallyclean:
