srcdir=oops-missing-srcdir
installdir=oops-missing-installdir
m=oops-missing-m
STEXLIB=oops-missing-stexlib
# Zuo overrides the above variables when running `make`
Scheme=../$m/bin/$m/scheme -b ../$m/boot/$m/petite.boot -b ../$m/boot/$m/scheme.boot
INSTALL=$(srcdir)/../makefiles/installsh

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

target: canned logcheck1 logcheck2 checklibs $(x).html $(x).pdf

install: target
	$(INSTALL) -m 2755 -d $(installdir)
	$(INSTALL) -m 0644 --ifdiff *.html *.css $(installdir)
	$(INSTALL) -m 0644 --ifdiff csug.pdf $(installdir)/csug.pdf
	$(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\
            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}

moresrc = tspl4-prep.stex priminfo.ss docond.ss csug8.cls csug8.hcls csug810.clo scheme.sty csug.bib \
 setup.ss summary.ss tspl.bst

$(stexsrc) $(moresrc):
ifeq ($(OS),Windows_NT)
	cp -p "${srcdir}"/$@ $@
else
	ln -s "${srcdir}"/$@ $@
endif

primdata.ss:
ifeq ($(OS),Windows_NT)
	cp -p "${srcdir}"/../s/$@ $@
else
	ln -s "${srcdir}"/../s/$@ $@
endif

canned:
ifeq ($(OS),Windows_NT)
	cp -r "${srcdir}"/$@ $@
else
	ln -s "${srcdir}"/$@ $@
endif

$(texsrc): $(moresrc)

$(srcdir)/title.tex $(srcdir)/contents.tex $(srcdir)/bibliography.tex:
	rm -f $*.tex
	echo "%%% DO NOT EDIT THIS FILE" > $*.tex
	echo "%%% Edit the .stex version instead" >> $*.tex
	echo "" >> $*.tex
	cat $(srcdir)/$*.stex >> $*.tex
	chmod -w $*.tex
title.tex: $(srcdir)/title.stex
contents.tex: $(srcdir)/contents.stex
bibliography.tex: $(srcdir)/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 setup.ss summary.ss tspl.bst
	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: ${TSPLdir}/tspl.aux
	cat ${TSPLdir}/*.aux | grep '\\newlabel' | \
         sed -e 's/\\newlabel{\(.*\){\([^}]*\)}}/\\newlabel{TSPL:\1{t\2}}/' > tspl.aux

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

tspl.rfm: ${TSPLdir}/tspl.rfm
	sed -e 's/\\pageref{/\\pageref{TSPL:/' ${TSPLdir}/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: ${TSPLdir}/tspl.idx
	sed -e 's/{\([1-9][0-9]*\)}$$/{t\1}/' ${TSPLdir}/tspl.idx | \
         sed -e 's/{\([ivx][ivx]*\)}$$/{t\1}/' > tspl.idx

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

$(texsrc): tspl4-prep.stex priminfo.ss 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:
	-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:
