title: Makefile Vpath multiple src Directories
body: <p>I have a directory structure like</p>
  
  <pre><code>edited/
      Betrayal/
          index.md
      Bloodlines/
          cover.jpg
          index.md
      ...
  </code></pre>
  
  <p>and a Makefile like</p>
  
  <pre><code>PANDOC      := pandoc
  PANDOC_OPTS := -t epub --smart --toc
  EBOOK_CONVERT := ebook-convert
  
  MODULES   := Betrayal Bloodlines Tempest Exile Sacrifice Inferno Fury Revelation Invincible
  SRC_DIR   := $(addprefix edited/,$(MODULES))
  BUILD_DIR := $(addprefix build/,$(MODULES))
  
  SRC       := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.md))
  EPUB      := $(patsubst edited/%.md,build/%.epub,$(SRC))
  MOBI      := $(patsubst edited/%.md,build/%.mobi,$(SRC))
  
  vpath %.md $(SRC_DIR)
  
  define make-epub
  $1/%.epub: %.md
      $(PANDOC) $(PANDOC_OPTS) $$&lt; -o $$@
  endef
  
  define make-mobi
  $1/%.mobi: $1/%.epub
      $(EBOOK_CONVERT) $$&lt; $$@ &gt;/dev/null
  endef
  
  .PHONY: all epubs mobis checkdirs clean
  
  all: checkdirs mobis
  
  epubs: $(EPUB)
  
  mobis: $(MOBI)
  
  checkdirs: $(BUILD_DIR)
  
  $(BUILD_DIR):
      @mkdir -p $@
  
  clean:
      @rm -rfv $(BUILD_DIR)
  
  $(foreach bdir,$(BUILD_DIR),$(eval $(call make-epub,$(bdir))))
  
  $(foreach bdir,$(BUILD_DIR),$(eval $(call make-mobi,$(bdir))))
  </code></pre>
  
  <p>(I still haven't added conditionals to test for a cover and metadata or added emailing via mutt to kindle, but here comes the current issue)</p>
  
  <p>The only problem is how every directory has an index.md and the VPATH method will always pull the index from the first module ex</p>
  
  <pre><code>pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Betrayal/index.epub
  pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Bloodlines/index.epub
  pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Tempest/index.epub
  pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Exile/index.epub
  ...
  </code></pre>
  
  <p>I could rename each index.md to the unique name of each folder like <code>(for d in *; do echo "$d"; cd "$d"; mv index.md "$d".md; cd ..; done)</code> and everything would work fine, but I want to learn makefile syntax and there must be a more elegant way to handle src directories without tons of recursive makefiles.</p>