# General



###############################
##### External dependency #####

make:=$(MAKE) # $(MAKE) in a command line implies "+" (run command even in -q mode). $(make) doesn't.

# Target to make external dependencies
extdep_create_%:
        f="` echo "$*" | sed 's:-SLASH-:/:g' `"; cd "`dirname "$$f"`" && $(make) "`basename "$$f"`"

# External dependency function.
# Put it in the dependency list of a target like this:
# mytarget: $(call extdep,../mydir/file.d)
# It will expand to
# ../mydir/file.d (If file.d doesn't need remake)
# or
# extdep_create_..-SLASH-mydir-SLASH-file.d (Which will remake file.d)
# or
# extdep_circdep (A non-existing target. Indicates circular dependency between directories)
# or
# extdep_error (A non-existing target. Indicates error or circular dependency)
define extdep
  $(shell \
    export EXTDEP_DONT_REENTER="$(EXTDEP_DONT_REENTER) `pwd`"; \
    cd "`dirname "$(1)"`"; \
    enter=yes; \
    for dir in $$EXTDEP_DONT_REENTER; do \
      if [ "x$$dir" = "x`pwd`" ] ; then \
        enter=no; \
      fi; \
    done; \
    if [ $$enter = yes ]; then \
      echo "Extdep-entering `pwd`" >&2; \
      $(MAKE) -q "`basename "$(1)"`" >&2; r=$$?; \
      if [ $$r = 0 ] ; then \
        echo "$(1)"; \
      elif [ $$r = 1 ] ; then \
        echo "extdep_create_`echo "$(1)" | sed 's:/:-SLASH-:g'`"; \
      else \
        echo "extdep_error"; \
      fi; \
      echo "Extdep-leaving `pwd`" >&2; \
    else \
      echo "extdep_circdep"; \
    fi; \
    )
endef

# Include all *.d files.
# The format of the *.d files is:
# 2 3
# file.d: file.tex
# file.d: file.eps
# The strange format is used because make has a bug:
# It can't $(eval $(shell ...)) on large data.
# So instead, we eval the lines one by one.
# The include statement is not used because the included files then become
# dependencies for simply parsing the makefile.
dummy:=$(foreach f,$(wildcard *.d), \
$(foreach n,$(shell head -n 1 "$(f)"), \
$(eval $(shell tail +$(n) "$(f)" | head -n 1) )))

### End external dependency ###
###############################



# Usage: Declare sources as the list of tex files you want to make, 
# then declare target all as what you want to make by default, dvi, ps or pdf.

.PHONY: pdf

pdf : $(sources:.tex=.pdf)
        
.PHONY: ps

ps : $(sources:.tex=.ps)

.PHONY: dvi

dvi : $(sources:.tex=.dvi)
        
%.pdf: %.ps
        ps2pdf14 "$*.ps" "$*.pdf"

%.ps: %.dvi
        dvips "$*.dvi" -o "$*.ps"

%.dvi: %.tex # Override builtin rule

%.dvi: %.d
        $(topdir)/latex.sh "$*"
        rm -f "$*.out"
        rm -f "$*.blg"

# Automagically find dependancies for tex files
$(foreach f,$(wildcard *.tex),$(eval $(f:.tex=.d): $(f))) # Prevent automatic deletion of intermediate files
%.d: %.tex
        $(topdir)/latex.sh -M "$*" > "$*.d"
# Recurse to make dependencies of the new *.d file
        $(MAKE) "$*.d"

%.fig.eps: %.fig
        fig2dev -L eps "$*.fig" "$*.fig.eps"

%.fig.png: %.fig
        fig2dev -L png -m 1 -S 4 "$*.fig" "$*.fig.png"

.PHONY: clean

clean::
        rm -f *.fig.eps
        rm -f *.d
        rm -f *.log
        rm -f *.out
        rm -f *.aux
        rm -f *.toc
        rm -f *.lof
        rm -f *.lot
        rm -f *.lol
        rm -f *.blg
        rm -f *.bbl
        rm -f *.dvi
        rm -f *.ps

.PHONY: cleanall

cleanall:: clean
        rm -f *.pdf
        rm -f *.fig.png