# find all .md files in the directory
md=$(shell ls etherdump/*.md)
# map *.mp => *.html for mdsrc
md2html=$(md:%.md=%.html)
md2pdf=$(md:%.md=%.pdf)

# inputs
# .mdsrcs: A listing of (local) markdown files
mdsrcs=$(shell ls etherdump/*.mdsrcs)
2col=$(mdsrcs:%.mdsrcs=%.2col.pdf)
pdocpdf=$(mdsrcs:%.mdsrcs=%.pdoc.pdf)
pdochtml=$(mdsrcs:%.mdsrcs=%.html)

# .pdfsrc: A listing of local PDF URLS, optionally with #page= fragment
pdfsrcs=$(shell ls etherdump/*.pdfsrcs)
scrp=$(pdfsrcs:%.pdfsrcs=%.scrp.pdf)

all: $(md2html) $(md2pdf) $(pdochtml) $(pdocpdf) $(2col) $(scrp)

dump:
        cd etherdump && etherdump pull --all --pub /home/pi/etherdump --css lib/styles.css --script lib/versions.js --no-raw-ext
        cd etherdump && etherdump index *.meta.json --templatepath /home/pi/etherdump/lib --template index.template.html > _index.html

.PHONY: fixnames
fixnames:
        rename "s/ /_/g" *
        rename "s/[\(\)\?\']//g" *
        rename "s/^(\d)([^\d])/0\1\2/g" *

today:
        touch `date +"%Y-%m-%d.md"`

now_folder:
        mkdir `date +"%Y-%m-%d-%H%M%S"`

# .html <== .md using pandoc
%.html: %.md
        pandoc --from markdown \
                --to html \
                --standalone \
                $< \
                -o $@

# .pdf <== .md using pandoc/latex
%.pdf: %.md
        pandoc --from markdown \
                --table-of-contents \
                --standalone \
                $< \
                -o $@

#############################################################################
# Recipes for lists of MARKDOWN SOURCES

# pandoc/latex pdf assembled from markdown sources
%.pdoc.pdf: %.mdsrcs
        cat $< | python scripts/urls2paths.py | xargs cat | pandoc --from markdown -o $@

# html from markdown sources
%.pdoc.html: %.mdsrcs
        cat $< | python scripts/urls2paths.py | xargs cat | pandoc --from markdown --to html --standalone -o $@

# 2 column bare bones PDF using Report Lab from markdown sources
%.2col.pdf: %.mdsrcs
        cat $< | \
        python scripts/urls2paths.py | \
        xargs cat | \
        pandoc --from markdown --to html --standalone | \
        python scripts/rl2cols.py --output $@

#############################################################################
# Recipes for lists of PDF SOURCES (with possibly #page=start,end fragments)

%.scrp.pdf: %.pdfsrcs
        python scripts/pdfsrcs.py $< $@ | bash




# special rule for debugging variables
print-%:
        @echo '$*=$($*)'