
TARGETS = design.pdf creatingFlashCards.pdf

all: $(TARGETS)

# Generat a list of tex files from the target directory if exists:
define SetDepList
  $(1)_DEPLIST := $(shell			\
	dirname=$(1);				\
	if test -d $$dirname; then		\
	  find $$dirname/*.tex;			\
	  if (test $$? -eq 0); then		\
	    echo $$dirname/*.tex;		\
	  fi					\
	fi)
endef

define SetPdfSvn
  # Script below mostly copied from doxygen:
  $(1).pdf : $(1).tex $$($(1)_DEPLIST)
	@(pdflatex -file-line-error -halt-on-error		\
	    -output-directory $$(dir $$<) $$<;			\
	  if (test -f $$*.idx); then				\
	    makeindex $$*.idx;					\
	  fi;							\
	  pdflatex -file-line-error -halt-on-error		\
	    -output-directory $$(dir $$<) $$<;			\
	  latex_count=5 ;					\
	  while egrep -s 'Rerun'				\
	    $$*.log && [ $$$$latex_count -gt 0 ] ; do		\
	      echo "Rerunning latex...." ;			\
	      pdflatex -file-line-error -halt-on-error		\
	        -output-directory $$(dir $$<) $$< ;		\
	      latex_count=`expr $$$$latex_count - 1` ;		\
	  done)
        # Test to see if tex is different form the svn, if not, rever
        # the pdf.
	-svn diff $$^ > $(1).svnlogs
	@(if (! test -s $(1).svnlogs); then	\
	  svn revert $$@;			\
	  touch $$@;				\
	fi)
endef

# Generate dependent lists:
$(foreach target,$(TARGETS), $(eval $(call SetDepList,$(basename $(target)))))

# Generate pdf rules that check for svn status:
$(foreach target,$(TARGETS), $(eval $(call SetPdfSvn,$(basename $(target)))))

# Clean ups:
clean:
	-@($(RM) *.log *.aux *.out *.ilg *.lot *.lof *.toc *.idx *.ind)
	-@($(RM) *.bbl *.dvi *.blg)
	-@($(RM) BROWSE TAGS $(TARGETS) *.svnlogs)