# -*- makefile -*-

# Assumes each *.slides-beamer.tex file is a file that will create
# both a Beamer slide file and a handout.  Should work for all files
# matching that pattern without any extra customisation.

# Copyright (C) 2005  Nick Urbanik <nicku@nicku.org>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#LATEX = latex --interaction=batchmode
#PDFLATEX = pdflatex --interaction=batchmode
LATEX = latex
PDFLATEX = pdflatex

#tex_slide_files =  $(filter-out gl2.101.1.slides.tex gl2.101.3.slides.tex gl2.101.5.slides.tex gl2.102.1.slides.tex gl2.102.3.slides.tex,$(wildcard *.slides.tex))
#tex_slide_files =  $(wildcard *.slides.tex)
#tex_slide_files = 102-5.tex $(wildcard *-slides.tex)

# beamer_slide_files = $(tex_slide_files:%.tex=%-beamer.tex)
# slides =             $(tex_slide_files:%.tex=%-beamer.pdf)
# handouts =           $(tex_slide_files:%.tex=%-beamer-handout.pdf)
# handouts_ps =        $(tex_slide_files:%.tex=%-beamer-handout.ps)

directories := $(shell for f in *;do [ -d "$$f" -a "$$f" != "template" ] && echo "$$f";done)

all: $(directories)

$(directories):
	$(MAKE) -C $@ all

clean:
	rm -f *.dvi $(shell ls *.pdf 2>/dev/null|egrep -v gnu-head.pdf)
	@echo $(directories)
	@for dir in $(directories); \
	do \
	    $(MAKE) -C $$dir clean; \
	done

distclean:
	rm -f *.dvi $(shell ls *.pdf 2>/dev/null|egrep -v gnu-head.pdf)
	@echo $(directories)
	@for dir in $(directories); \
	do \
	    $(MAKE) -C $$dir distclean; \
	done

tidy:
	rm -f *.log *.ps *.toc *.aux *.eepic *.bak *.lg *.out *.idx *~ \
	*.nav *.snm *.out *.vrb
	@echo $(directories)
	@for dir in $(directories); \
	do \
	    $(MAKE) -C $$dir tidy; \
	done


list:
	@echo $(directories)
	@for dir in $(directories); \
	do \
	    $(MAKE) -C $$dir list; \
	done

lpr:
	@echo Print all documents:
	@for dir in $(directories); \
	do \
	    $(MAKE) -C $$dir lpr; \
	done

print: lpr

%:
	@echo Setting up new directory $@
	@mkdir $@
	@echo Creating $@/Makefile
	@sed 's/_TEMPLATE_/$@/g' template/Makefile.template > $@/Makefile
	ln -s ../template/beamer-slide-header.tex $@
	@for f in template/*; do \
		[ $$f == template/Makefile.template ] && continue; \
		[ $$f == template/beamer-slide-header.tex ] && continue; \
		echo $$f|grep -q "~$$" && continue; \
		target=`echo $$f|sed "s/template/$@/g"`; \
		echo "Creating $$target from $$f"; \
		sed "s/_TEMPLATE_/$@/g" $$f > $$target; \
	done


# Stop overzealous deletion of intermediate files
.PRECIOUS: %.eps $(pdffigs) %.slides-beamer.tex

.PHONY: all clean tidy distclean list lpr print $(directories)
