SHELL := bash
build_dir := build
basefn := lifeofrachael
base_outfn := $(build_dir)/$(basefn)
input_files := $(wildcard *.md)
anonymize_files := $(wildcard [0-9][0-9][0-9]-*.md)
include_dir := include
title := Life of Rachael
flags_title_single := -V title="$(title)" -V title-plain="$(title)" -V title-meta="$(title)"

version_file := $(build_dir)/version.txt
git_tag := $(shell (which -s git && git describe --always --long --dirty 2>/dev/null) || echo "(none)")
version := $(git_tag) ($(shell date))

venv_root := venv
VENV = source $(venv_root)/bin/activate &&

img_in := img_src
img_out := img_full
img_resize_out := img
images_in := $(wildcard $(img_in)/*)
images_out := $(patsubst $(img_in)/%, $(img_out)/%, $(images_in))
images_resize_out := $(patsubst $(img_in)/%, $(img_resize_out)/%, $(images_in))
img_width := 1024

base_deps := $(wildcard $(include_dir)/*) $(images_out) $(images_resize_out) \
	$(venv_root)
dependencies := $(input_files) $(base_deps)

pandoc_base_cmd := $(VENV) pandoc --file-scope --mathjax \
	--metadata-file $(include_dir)/frontmatter.yaml \
	-M subtitle="$(version)"
pandoc_std_cmd := $(pandoc_base_cmd) --pdf-engine=lualatex \
	-F $(include_dir)/panfilters.py

# Translate 000-my-thing.md -> my-thing
PERMA_SLUG = $(shell echo $(basename $<) | cut -d- -f2-)

define pandoc_single
	@mkdir -p $(build_dir)
	$(pandoc_std_cmd) $(1) -o $@ $(input_files)
endef

std: pdf html html-single version
all: std tex src

html: $(patsubst %.md, $(build_dir)/parts/%.html, $(input_files))
html-single: $(base_outfn).html
pdf: $(base_outfn).pdf
epub:  $(base_outfn).epub
tex: $(base_outfn).tex
json: $(base_outfn).json
docx: $(base_outfn).docx

images: $(images_out) $(images_resize_out)
src: $(base_outfn).zip

clean:
	rm -rf $(build_dir)
	! [ -d $(img_in) ] || rm -rf $(img_out) $(img_resize_out)

clean-all: clean
	rm -rf venv/
	rm -f .oembed-cache.sqlite

spellcheck:
	hunspell -l $(input_files) | sort | uniq | less

wordcount:
	wc $(input_files)

proper-nouns:
	grep -oh '\w\+' $(input_files) | \
		grep -E '^[A-Z].+$$' | \
		tr '[:upper:]' '[:lower:]' |  \
		sed 's/.*/\u&/' | \
		sort | uniq | less

version:
	@[ "$(version)" ]
	@mkdir -p $(build_dir)
	echo "$(version)" > $(version_file)

anonymize: $(anonymize_files)
	m4 etc/anonymize.sed.m4 | perl -i -p - $(anonymize_files)

$(venv_root):
	python3 -m venv $(venv_root)
	$(VENV) pip install panflute requests-cache emoji

$(build_dir)/parts/%.html: %.md $(base_deps)
	@mkdir -p $(build_dir)/parts
	$(pandoc_std_cmd) --template=$(include_dir)/template.html \
		-V layout=life_page -V perma_slug="parts/$(PERMA_SLUG)" -o $@ $<

$(base_outfn).html: $(dependencies)
	$(call pandoc_single, --template=$(include_dir)/template.html \
		-V layout=fancy_document -V perma_slug="lifeofrachael" $(flags_title_single))

$(base_outfn).pdf: $(dependencies)
	$(call pandoc_single, $(flags_title_single))

$(base_outfn).tex: $(dependencies)
	$(call pandoc_single, --standalone $(flags_title_single))

$(base_outfn).epub: $(dependencies)
	$(call pandoc_single)

$(base_outfn).docx: $(dependencies)
	$(call pandoc_single, --standalone)

$(base_outfn).json: $(dependencies)
	@mkdir -p $(build_dir)
	$(pandoc_base_cmd) -o $@ $(input_files)

$(img_out)/%: $(img_in)/%
	@mkdir -p $(img_out)
	@rm -f $@
	exiftool -q -EXIF= -XResolution=1 -YResolution=1 -ResolutionUnit=None -o $@ $<

$(img_resize_out)/%: $(img_out)/%
	@mkdir -p $(img_resize_out)
	convert $< -resize $(img_width) $@

$(build_dir)/%.zip: $(dependencies) version
	$(eval tmp := $(shell mktemp -d))
	mkdir $(tmp)/$(basefn)
	-cp $(version_file) * $(tmp)/$(basefn)
	cp -r $(include_dir) $(img_resize_out) ext/ $(tmp)/$(basefn)
	cd $(tmp) && zip -r $(basefn).zip $(basefn)
	@mkdir -p $(build_dir)
	mv -f $(tmp)/$(basefn).zip build/
	rm -rf $(tmp)
