# Drop-In Makefile for LaTeX with Bibtex: Minimal number of operations.

all : emissions-tokyo.pdf emissions-tokyo-auxilliary.zip

AUXILLIARY_FILES := $(wildcard *.py *.json */*.json odiac/*tif *.bib *.sty copernicus.*) tccon-data/tk20110804_20160330.public-fit-xco2.pdf tccon-data/tk20110804_20160330.public-fit-daily-xco2.pdf tateno-sounding-profiles-boxplot.pdf tccon-data/tk20110804_20160330.public-tsukuba-residuals-xco2.pdf plot-tokyo-2015-01.pdf tccon-data/tk20110804_20160330.public-hexbin-wind-speed-xco2.pdf odiac-emissions-by-angle.pdf tk20110804-20160330-public-fit-daily-xco2.pdf emissions-tokyo.tex emissions-tokyo-auxilliary.tex setupbody.tex tsukuba-wind-direction-vs-time-of-day.pdf 

emissions-tokyo-auxilliary.zip: emissions-tokyo-auxilliary.pdf Makefile README odiac_emissions.json $(AUXILLIARY_FILES)
	zip -9r $@ $^

# dependencies which need to be defined for the build
TEX_MASTER = emissions-tokyo.tex
BIBTEX_FILES = ref.bib
# are there more files needed by pdflatex? Are there included TeX-files?
# ADDITIONAL_PDFLATEX_DEPENDENCIES := 
# singly expanded variable (a := b) to run the shell only once.

# Include all png images found in the tex master. 
# sed for 2ed.pdf removes the pdf file referenced in a comment in the
# copernicus template.  FIXME: ignore files in comments.
ADDITIONAL_PDFLATEX_DEPENDENCIES := setupbody.tex $(shell grep -oe "[0-9a-zA-Z\\-]*\\.\(png\|pdf\)" $(TEX_MASTER) | sed "s/2ed\\.pdf//" | xargs)

# TODO: Generalize this for getting all includes (recursively?)

#### YOUR RULES ####

# to keep this file tidy, use this space to define all the rules
# needed to create the ADDITIONAL_PDFLATEX_DEPENDENCIES
# ORG_BABEL_TANGLE_EXECUTED = .org-babel-tangle-executed
# $(ADDITIONAL_PDFLATEX_DEPENDENCIES) : $(ORG_BABEL_TANGLE_EXECUTED)
# $(ORG_BABEL_TANGLE_EXECUTED) : graphs.org
# 	emacs --eval '(setq safe-local-variable-values (quote ((org-babel-noweb-wrap-end . "}}}") (org-babel-noweb-wrap-start . "{{{") (*org-babel-use-quick-and-dirty-noweb-expansion* . t) (org-export-allow-bind-keywords . t) (org-confirm-babel-evaluate))))' --batch --visit $< -f org-babel-tangle >/dev/null
# 	bash gen-graphs-fast.sh >/dev/null 
# 	bash gen-graphs-slow.sh >/dev/null

tk20110804-20160330-public-fit-xco2.pdf : tccon-data/tk20110804_20160330.public-fit-xco2.pdf
	cp $^ $@
tk20110804-20160330-public-fit-daily-xco2.pdf : tccon-data/tk20110804_20160330.public-fit-daily-xco2.pdf
	convert -density 300 $^ $@ # use convert to make the pdf cheaper to load (reduces the resolution)
tk20110804-20160330-public-hexbin-wind-speed-xco2.pdf : tccon-data/tk20110804_20160330.public-hexbin-wind-speed-xco2.pdf
	cp $^ $@
tk20110804-20160330-public-tsukuba-residuals-xco2.pdf : tccon-data/tk20110804_20160330.public-tsukuba-residuals-xco2.pdf
	cp $^ $@

tateno-sounding-profiles-boxplot.pdf: average_sounding_data.py
	./$<

ODIAC_DATAFILES := $(shell ./odiac_emissions.py --print-required-datafiles)

odiac_emissions.json: odiac_emissions.py $(ODIAC_DATAFILES) 
	./$< -o $@

odiac-emissions-by-angle.pdf: plot_odiac.py odiac_emissions.json 
	./$<

plot-tokyo-2015-01.pdf: odiac_emissions.json
	ls $@

tsukuba-wind-direction-vs-time-of-day.pdf: plot_daytime_vs_direction.py tccon-data/tk20110804_20160330.public.nc 
	./$<

emissions-tokyo-auxilliary.pdf: emissions-tokyo-auxilliary.tex tsukuba-wind-direction-vs-time-of-day.pdf 2017-06-30-fhtagn-steno-tokyo-rlyeh-cthulhu-fhtagn-trajectories-from-tokyo.jpg trajplot_197390.pdf trajplot_198071.pdf tokyo-5-hours-trajplot_14938.pdf
	pdflatex $(basename $<); pdflatex $(basename $<); bibtex $(basename $<); pdflatex $(basename $<); bibtex $(basename $<); pdflatex $(basename $<)

odiac/%.tokyo.tif: odiac/%.tif.gz
	gdal_translate -projwin 138. 37. 142. 34. /vsigzip/$< $@
odiac/%.tif.gz: 
	wget -c -O $@ "http://db.cger.nies.go.jp/nies_data/10.17595/20170411.001/odiac2016/1km_tiff/20"$(shell echo $@ | sed s/.*ac2016_1km_excl_intl_// | cut -c 1,2)"/"$(notdir $@)

# ...


###### ###### You should not need to touch anything below ###### ######

## Requirements definition ##

# 1. Full run: If the tex file changes the citations, pdflatex
#    executes, then bibtex executes, then pdflatex executes and
#    executes again until all references are correct.

# 2. bibtex: If a bibtex reference changes which is used in the
#    tex-file, bibtex executes, then pdflatex executes and executes
#    again until all references are correct.

# 3. pdflatex: If the tex file changes but the citations stay the
#    same, pdflatex executes and executes again until all references
#    are correct.

# 4. bibtex no change: If a bibtex reference changes which is not used
#    in the tex-file, only bibtex executes.

# Failure states to consider:
# (f1) pdflatex fails to produce a new *.aux
# (f2) pdflatex fails to produce a *.pdf, but produces an *.aux
# (f3) pdflatex fails to produce an *.aux, but produces a *.pdf
# (f4) pdflatex has errors. When running make again, pdflatex must be run again.
# (f5) the tex file contains a citation which is not in the bibtex file. When running make again, bibtex and pdflatex must be run again. bibtex should fail and inform the user about the problem.
# (f6) bibtex fails to produce a *.bbl


# TODO: Check how to make this work for projects without bibtex.

# FIXME: If the texbuilding fails due to changes in bibtex it did not
# spot, then this can go into a failure mode where I have to touch the
# bibtex file to make everything work again. I don’t yet know why.

# FIXME: When I add a citation in the tex-file, the first run will not
# capture that citation correctly and trigger a second run of bibtex
# and the pdflatex loop.

# FIXME: This currently greps the tex log for “There were undefined
# citations” to see whether it has to rebuild.

#### Definitions ####

# automatically derived information.
PDFLATEX_DEPENDENCIES = $(TEX_MASTER).md5 $(ADDITIONAL_PDFLATEX_DEPENDENCIES)
TEX_MASTER_BASE = $(basename $(TEX_MASTER))
TEX_MASTER_AUX_MD5 = $(TEX_MASTER_BASE).aux.md5
BIBTEX_DEPENDENCIES = $(TEX_MASTER_BASE).aux.md5 $(BIBTEX_FILES)
INTERMEDIATE_FILES = $(TEX_MASTER_BASE).log $(TEX_MASTER_BASE).blg $(TEX_MASTER_BASE).out

# status files
BIBTEX_EXECUTED = .bibtex-executed
BIBTEX_EXECUTED_TWICE = .bibtex-executed-twice
PDFLATEX_EXECUTED = .pdflatex-executed
PDFLATEX_EXECUTED_TWICE = .pdflatex-executed-twice
BUILD_LOG = build.lastlog

# intermediate targets are deleted after creating the target. But only
# if they were actually used by another rule.
.INTERMEDIATE : $(INTERMEDIATE_FILES)
# secondary targets are created as side-effect, but not deleted if the
# build succeeded. This gives our md5-based dependency resolution
# persistency.
.SECONDARY : $(TEX_MASTER_BASE).bbl $(TEX_MASTER_BASE).aux $(BIBTEX_EXECUTED) $(BIBTEX_EXECUTED_TWICE) $(PDFLATEX_EXECUTED) $(PDFLATEX_EXECUTED_TWICE) $(TEX_MASTER_BASE).aux.md5  $(TEX_MASTER_BASE).bbl.md5 $(TEX_MASTER).md5 $(BUILD_LOG)


#### Build Dependencies ####

# to create a final document, I need the output of executing bibtex 
# and bibtex run needs the output of a pdflatex run.
# *.tex -> *.aux -> *.bbl -> *.aux (new) -> *.pdf

# Let’s start with the basic trick which allows us to reduce the
# number of required runs: We checksum the files and then depend on
# the md5-files instead of the real files. The following pattern rules
# allow us to simply depend on *.md5 and let make figure out which md5
# files we really need. The *.md5 files only get updated when the
# content of the checksummed file changes. As such they only trigger
# later rebuilds when the actual file content changed, not when the
# timestamp changed.

# ensure that we have all md5 files we want to use. This builds on a trick from http://www.cmcrossroads.com/article/rebuilding-when-files-checksum-changes?page=0%2C1
# TODO: Check how to merge this into a single rule (it failed last time I tried).
%.aux.md5: %.aux
	@$(eval CHECKSUM := $(shell md5sum $*.aux))$(if $(filter-out $(shell cat $@ 2>/dev/null),$(CHECKSUM)),echo $(CHECKSUM) > $@) 
%.bbl.md5: %.bbl
	@$(eval CHECKSUM := $(shell md5sum $*.bbl))$(if $(filter-out $(shell cat $@ 2>/dev/null),$(CHECKSUM)),echo $(CHECKSUM) > $@) 
%.tex.md5: %.tex
	@$(eval CHECKSUM := $(shell md5sum $*.tex))$(if $(filter-out $(shell cat $@ 2>/dev/null),$(CHECKSUM)),echo $(CHECKSUM) > $@) 



# for the final step, we run pdflatex once, and if that changed the
# *.aux, we run pdflatex again until *.aux does not change anymore. Depend on *.bbl.md5, because 
$(TEX_MASTER_BASE).pdf $(BUILD_LOG) : $(PDFLATEX_DEPENDENCIES) $(TEX_MASTER_AUX_MD5) $(TEX_MASTER_BASE).bbl.md5 
	pdflatex -interaction nonstopmode $(TEX_MASTER_BASE) > $(BUILD_LOG)  || (if grep -q Error $(BUILD_LOG); then rm $(TEX_MASTER_BASE).aux* && false; fi)
	if grep -q "There were undefined citations" "$(TEX_MASTER_BASE).log"; then pdflatex -interaction nonstopmode $(TEX_MASTER_BASE) > $(BUILD_LOG) || (rm $(TEX_MASTER_BASE).aux* && false); fi
	while test ! -e $(TEX_MASTER_BASE).aux || test "x$(md5sum $(TEX_MASTER_BASE).aux)" != "x$(cat $(TEX_MASTER_BASE).aux.md5)"; do md5sum $(TEX_MASTER_BASE).aux | cut -d " " -f 1  > $(TEX_MASTER_BASE).aux.md5 ; pdflatex -interaction nonstopmode $(TEX_MASTER_BASE); done  > $(BUILD_LOG)
	if test -e "$(TEX_MASTER_BASE).log"; then if grep -q "There were undefined citations" "$(TEX_MASTER_BASE).log"; then rm $(TEX_MASTER_BASE).aux.md5 && false; else rm -f $(INTERMEDIATE_FILES); fi ; fi

# pdflatex needs the output from bibtex to create the correct
# bibliography.
$(TEX_MASTER_BASE).bbl $(TEX_MASTER_BASE).blg $(BUILD_LOG) : $(BIBTEX_EXECUTED)
# and the initial bibtex files need the output of pdflatex (once)
$(BIBTEX_EXECUTED) : $(BIBTEX_DEPENDENCIES)
	bibtex $(TEX_MASTER_BASE) > $(BUILD_LOG) || (rm $(TEX_MASTER_BASE).bbl* && false) && if grep -q "I didn't find a database entry for" $(TEX_MASTER_BASE).blg; then rm $(TEX_MASTER_BASE).bbl*; false; fi


# finally, bibtex requires the template.aux from an initial pdflatex
# run. This run is not necessary when only the bibtex file
# changed.

# This actually also creates a *.pdf file, but we do not tell make
# about that to avoid shortcutting the full build. It gets created
# anyway and since make uses timestamps instead of md5 for the basic
# dependency resolution, this created *pdf is recognized as newer on
# the next run.
$(TEX_MASTER_BASE).aux $(TEX_MASTER_BASE).log $(TEX_MASTER_BASE).out $(BUILD_LOG) : $(PDFLATEX_EXECUTED)
# rebuilding of all files is necessary if either template.tex or graphs.org changes.
$(PDFLATEX_EXECUTED) : $(PDFLATEX_DEPENDENCIES) 
	pdflatex -interaction nonstopmode $(TEX_MASTER_BASE)  > $(BUILD_LOG) || (if grep -q Error $(BUILD_LOG); then rm $(TEX_MASTER_BASE).aux* && false; fi)


#### Cleanup ####

clean : 
	rm $(TEX_MASTER_BASE).aux $(TEX_MASTER_BASE).bbl $(TEX_MASTER_BASE).pdf $(TEX_MASTER_BASE).aux.md5  $(TEX_MASTER_BASE).bbl.md5 $(TEX_MASTER).md5 $(BUILD_LOG)




