ifeq (,$(shell sh -c 'cygpath --version 2> /dev/null'))
  # Unix
  pwd := $$(pwd)
  translate = $1
else
  # Windows mit MSys2/Cygwin
  pwd := $$(cygpath -m "$$(pwd)")
  translate = $(shell echo '$1' | sed 's/:/;/g')
endif

all: build/loesung.pdf

build/loesung.pdf: FORCE $(addprefix build/loesung-, table.tex a.tex b.tex c.tex d.tex plot.pdf) | build
	TEXINPUTS="$(call translate,build:)" \
	max_print_line=1048576 \
	latexmk \
	  --lualatex \
	  --output-directory=build \
	  --interaction=nonstopmode \
	  --halt-on-error \
	loesung.tex

build/loesung-plot.pdf: loesung.py curve_fit.py data.txt loesung-matplotlibrc loesung-header-matplotlib.tex | build
	MATPLOTLIBRC=loesung TEXINPUTS="$(call translate,$(pwd):)" python loesung.py

$(addprefix build/loesung-, table.tex a.tex b.tex c.tex d.tex): build/loesung-plot.pdf

data.txt: data.py
	python $<

build:
	mkdir -p build

clean:
	rm -rf build
	rm -f data.txt

FORCE:

.PHONY: all clean
