# expensive checks enabled: make -B flightcheck=1
# Final document: make -B final=1

stem = eut
source_baseurl = http://euwiki.org/
source_basename = EUT/2nd-edition

citeproc_file = $(stem).bib

templates = template.tex header.tex before.tex after.tex

filters = ./pandoc-memoir ./pandoc-emphasis ./pandoc-cs1 ./pandoc-todo
filters += $(if $(citeproc_file),pandoc-citeproc)
filters += ./pandoc-iri

markdown_includes = copyright.md colophon.md

includes += $(markdown_includes:.md=.tex)

localperlfilters = $(filter ./%,$(filters))

title = Ensuring utmost transparency
subtitle = Free Software and Open Standards under the Rules of Procedure of the European Parliament
env_filter += $(citeproc_file:%=PANDOC_CITEPROC_FILE=%)
env_filter += $(if $(flightcheck),NETWORK_TESTS=1)
args_filter += $(filters:%=--filter %)
args_meta += $(citeproc_file:%=-M bibliography=%)
args_meta += -V lang=english -V langoption=variant=british
args_meta += -M title="$(title) - $(subtitle)"
args_meta += -V title="$(title)"
args_meta += -V subtitle="$(subtitle)"
args_meta += -M author="Carlo Piana, Ulf Öberg"
args_meta += -V author="Carlo Piana" -V author="Ulf Öberg"
args_meta += -V date=""
args_meta += -M csl=oscola-no-ibid.csl
args_latex += --template=template.tex --latex-engine=xelatex
args_latex += -H header.tex -B before.tex -A after.tex
args_latex += -V mainfont="Lora" -V sansfont="Quattrocento Sans" -V monofont="Inconsolata"
args_latex += -V documentclass=memoir -V pagestyle=plain -V headstyles=komalike -V chapterstyle=ell
args_latex += -V frontmatter=yes -V classoption=titlepage -V classoption=$(if $(final),final,draft)
#flavors = a4 book ebook
flavors = a4 ebook
args_a4 = -V papersize=a4paper -V fontsize=10pt -V classoption=twoside
args_a4 += -M isbn="978-90-823692-1-2" -V coverpage="$(stem)-a4-front.pdf"
args_book = -V papersize=b5paper -V fontsize=10pt -V classoption=twoside
args_ebook = -V papersize=a5paper -V fontsize=12pt -V classoption=oneside -V classoption=openany
args_ebook += -M isbn="978-90-823692-0-5" -V isbn-nobarcode=1 -V coverpage="$(stem)-a4-front.pdf"
args_ebook += -H header-ebook.tex

all: $(flavors:%=$(stem)-%.pdf)

download:: get-$(stem).raw
download:: get-$(stem)-front-logo2.bmp get-$(stem)-front-a4-orig.svg
download:: get-$(stem)-front-photo-orig.jpg
download:: get-fonts

get-$(stem).raw: get-%:
	# FIXME: website apparently down (last checked 2017-03-30)
	#./mediawiki-fetch $(source_baseurl) $(source_basename) $*

get-$(stem)-front-photo-orig.jpg: get-%:
	curl -Ro '$*' 'http://audiovisual.europarl.europa.eu/Asset.aspx?type=nl&id=52c6e976-dd19-4a10-a8f0-afebae7fddd8'

get-$(stem)-front-logo2.bmp: get-%:
	curl -Ro '$*' 'http://siri.biks.dk/transparency/GreensEFA-EN.bmp'

get-$(stem)-front-a4-orig.svg: get-%:
	curl -Ro '$*' 'http://siri.biks.dk/transparency/transparency_front.svg'

get-fonts:
	curl -RLO https://github.com/cyrealtype/Lora-Cyrillic/raw/master/fonts/ttf/Lora-{Regular,Italic,Bold,BoldItalic}.ttf
	curl -RLo quattrocento-sans-v2.0.zip http://www.impallari.com/media/uploads/prosources/update-19-source.zip
	unzip -uo quattrocento-sans-v2.0.zip quattrocento-sans-v2.0/*.ttf

install-fonts:
	mkdir -p ~/.fonts
	cp -ft ~/.fonts Lora-*.ttf quattrocento-sans-v2.0/*.ttf

$(stem)-front-photo.jpg: $(stem)-front-photo-orig.jpg
	cp -f $< $@
	jpegoptim --strip-all --all-progressive --force $@

$(stem)-front-logo2.png: $(stem)-front-logo2.bmp
	optipng -clobber $<

$(stem)-front-a4.svg: $(stem)-front-a4-orig.svg eut-front-logo2.png eut-front-photo.jpg
	cp -f $< $@
	perl -i -pe 's,xlink:href="\K[^\"]+\.bmp,eut-front-logo2.png,' $@
	perl -i -pe 's,xlink:href="\K[^\"]+\.jpg,eut-front-photo.jpg,' $@

$(stem)-a4-front.pdf: $(stem)-front-a4.svg
	inkscape -z --export-pdf=$@ $<

$(stem).mw: $(stem).raw
	./mediawiki-trim $< $@
	./mediawiki-blockquote $@
	./mediawiki-uri-escape $@
	./mediawiki-matter $@

$(stem).native: $(stem).mw $(localperlfilters)
	# validate syntax and dependencies of local Perl filters
	set -e; $(foreach filter,$(localperlfilters),perl -c $(filter);)
	# convert content from mediawiki to pandoc-native
	$(env_filter) pandoc -f mediawiki --smart $(args_meta) $(args_filter) -o $@ $<
	# tweak pandoc-native data
	./native-hacks $@

$(markdown_includes:.md=.tex): %.tex: %.md
	pandoc -f markdown -t latex --chapters -o $@ $<

$(flavors:%=deps-%):: $(templates) $(includes) Makefile
deps-a4:: $(deps) $(stem)-a4-front.pdf
deps-ebook:: $(deps) $(stem)-a4-front.pdf header-ebook.tex

$(flavors:%=$(stem)-%.pdf): $(stem)-%.pdf: $(stem).native $(deps-all) deps-%
	pandoc --standalone --no-tex-ligatures $(args_meta) $(args_latex) $(args_$*) -o $@ $<

clean:
	rm -f $(foreach ext,native bib mw,$(stem:%=%.$(ext)))
	rm -f $(includes)

distclean: clean
	rm -f $(stem:%=%.raw) $(stem:%=%-a4-front.pdf)