TARGET = icrc0974

TARGETLIST    = $(TARGET).pdf
INTERMEDIATES = $(TARGET).dvi

all: 	$(TARGETLIST)

OPT = -file-line-error-style -interaction=nonstopmode

.SILENT:

define run-latex
	@echo " - Producing $@ from" $<
	! (latex $(OPT) $< | grep -E \.tex:[0-9]*:\ )
	bibtex --terse $(<:.tex=)                                      
        latex $(OPT) $< > /dev/null
        latex $(OPT) $< > /dev/null
        ! (latex $(OPT) $< | grep ^LaTeX\ Warning:\ )
#	! (latex $(OPT) $< | grep -E \.tex:[0-9]*:\ ) &&                \
#        bibtex --terse $(<:.tex=);                                      \
#        cp icrc0974.bbl icrc0974.bbl~ &&                                          \
#        sed icrc0974.bbl~ -e 's/^\(.*\)\\url\(.*\)%/\1\\url\2/' > icrc0974.bbl && \
#        (latex $(OPT) $< > /dev/null) &&                                \
#        (latex $(OPT) $< > /dev/null) &&                                \
#        ! (latex $(OPT) $< | grep ^LaTeX\ Warning:\ )
#	&&               \
#        ! (latex $(OPT) $< | grep ^LaTeX\ Warning:\ )
endef

%.dvi:	%.dtx
	$(run-latex)

%.dvi:	%.tex
	$(run-latex)

%.ps:	 %.dvi
	@echo " - Producing $@ from" $<
	dvips -q $< $@~ && mv $@~ $@

%.pdf:	 %.dvi
	@echo " - Producing $@ from" $<
	dvipdf -q $< $@~ && mv $@~ $@

%:	 %.dvi
	@echo " - Producing $@.pdf from" $<
	dvipdf -q $<

%.ps.gz:	%.ps
	@echo " - Producing booklet $@ from" $< 
	psbook -q $< | psnup -q -2 | pstops -q 2:0,1VH | gzip -c -9 > $@

%.bdf:	%.ps.gz
	@echo " - Producing booklet $@ from" $<
	gzip -cd $< | ps2pdf - $@

clean:
	@rm -f $(INTERMEDIATES) $(TARGETLIST) *.dvi *.aux *.blg *.lof *.log *.lot *.toc *.bbl *.bdf

mrproper:	clean
	@rm -f *~
