PDF_APP = zathura 
TEX_FILE = TD-TP.tex
OUTPUT_DIR = output
GENERATED_IMAGES_DIR = images/generated
SVG_FILES_DIR=images/svg
svgfiles = $(wildcard $(SVG_FILES_DIR)/*.svg)
figures = $(subst svg/,generated/,$(subst .svg,.png,$(svgfiles)))
pdf_file = $(subst tex,pdf,$(TEX_FILE))

default: build

images: $(figures)

clean:
	rm -rf $(GENERATED_IMAGES_DIR)/*
	rm -rf $(OUTPUT_DIR)/*

build: $(OUTPUT_DIR)/$(pdf_file) 

$(OUTPUT_DIR)/$(pdf_file): $(TEX_FILE)
	$(shell mkdir -p $(OUTPUT_DIR))
	latexmk -xelatex -output-directory=$(OUTPUT_DIR) $<

buildall: images build

$(GENERATED_IMAGES_DIR)/%.png : images/svg/%.svg
	@echo build $@
	@mkdir -p $(GENERATED_IMAGES_DIR)
	@inkscape --export-type=png --export-dpi=300 -o $@ $<

echo:
	@echo "svg files: $(svgfiles)"
	@echo "generated figures: $(figures)"

view: build
	$(PDF_APP) $(OUTPUT_DIR)/$(pdf_file)

build-container:
	podman build . -t framac

framac:
	podman run -it --rm \
		--volume=/tmp/.X11-unix/:/tmp/.X11-unix/ \
		--volume=./code/:/data \
		--userns "keep-id:uid=9000,gid=9000" \
		-e DISPLAY=$$DISPLAY \
		framac

.PHONY: echo view clean build-container framac
