FILES = ack.tex eating.tex entertain.tex map.tex miniconf.tex papers.tex programme.tex professionals.tex sponsors.tex thebook.tex transport.tex notes.tex
SSH = ssh conf
SCP = scp $(1) conf:public_html/handbook

all: thebook.dvi

professionals.tex: professionals.header.tex professionals.footer.tex professionals.dump
	cat professionals.header.tex > professionals.tex
	#cat professionals.dump | tail +2 | perl -pe 's/([^\t\r\n]+)/\\small{$$1}/g;s/NULL$$//;s/&/\\&/g;s/$$/ \\\\/;s/\t/ \& /g' >> professionals.tex
	#cat professionals.dump | tail +2 | perl -e 'sub escape { $$_ = shift; chop; s/NULL$$//;s/&/\\&/g;s/\t/ \& /g; $$_ } while (<STDIN>) { $$first = escape($$_); $$s = <STDIN>; if (defined $$s) { $$second = escape($$s); } print "$$first & $$second \\\\\n" }' >> professionals.tex # this is for four cols...
	cat professionals.dump | tail +2 | perl -pe 's/NULL$$//;s/&/\\&/g;s/$$/ \\\\/;s/\t/ \& /g' >> professionals.tex
	cat professionals.footer.tex >> professionals.tex

professionals.dump:
	echo "select Name,Company_Name from Person left outer join Receipt on Person.CustNum = Receipt.CustNum where ((Registration_Type_ID = 2 and Special_Requirements not like '[%') or (Receipt.CustNum is null and (Special_Requirements like '[fsmaj%' or Special_Requirements like '[sun%' or Special_Requirements like '[ucs%'))) order by Company_Name,Name" | $(SSH) mysql LCA2003-Reg --raw | tools/nullsatend.pl > professionals.dump
#echo "select Name,Company_Name from Person inner join Receipt on Person.CustNum = Receipt.CustNum where Registration_Type_ID = 2 and Special_Requirements not like '[%' order by Company_Name,Name" | $(SSH) mysql LCA2003-Reg --raw | tools/nullsatend.pl > professionals.dump
	$(MAKE) professionals.tex

notes.tex:
	rm -f *.aux
	echo -e '\lcasection{Notes}\n\n' > notes.tex
	yes 'R' | latex thebook.tex|grep "Output written"|perl -e '$$_=<>; s/^.*\((\d+) pages.*$$/$$1/; print STDERR "Pages: $$_\n";system("echo \" \\\\setcounter{lastpage}{".((int(($$_-1)/4)+1)*4)."}\" >> thebook.aux"); $$scribble = "\\vspace*{2cm}\\ifSpecial{\\centerline{\\includegraphics[width=13cm]{tux-watermark}}}\\fi\n"; print "\\lcasection{Notes}\n\n$$scribble\n\n"; for($$i=$$_;$$i%4;$$i++) { print "\\newpage\n\n$$scribble\n\n";} ' > notes.tex.new
	/bin/mv notes.tex.new notes.tex
	#yes 'R' | latex thebook.tex |grep "Output written" || exit 0

thebook.dvi: $(FILES)
	latex thebook.tex

show: thebook.dvi
	xdvi thebook.dvi

book: thebook.ps
	psbook thebook.ps handbook.ps
	psnup -pa4 -Pa4 -s1 -l -n2 handbook.ps | perl -pe 's/(%%DocumentPaperSizes: )a5/\1a4\n%%Orientation: Landscape/' > finals.ps
	killall -1 gv

thebook.ps: $(FILES) thebook.dvi
	#pslatex thebook.tex
	dvips -t a5 thebook.dvi

maintainer-clean: clean
	rm -f professionals.dump

clean:
	rm -f *.dvi *.ps *.pdf *.log *.toc *.aux *~ notes.tex professionals.tex *.abs

push: all
	$(call SCP,$(FILES) Makefile)

miniconf.dvi: miniconf.tex
	latex miniconf.tex

abstracts:
	echo "DONT DO IT!!!"
	#for i in abstracts/*.html ; do echo $$i; (echo "<body>";cat $$i;echo "</body>")|tools/gnuhtml2latex -s -| tools/abstracts-rewrite.pl > $${i/.html/.tex}; done
	#cd abstracts ; patch -p1 < ../abstracts.diff

.PHONY: notes.tex push clean book thebook.dvi abstracts
