PLANTUML_JAR_URL = https://sourceforge.net/projects/plantuml/files/plantuml.jar/download
PLANTUML_STDLIB_C4_CONTEXT = https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml
PLANTUML_STDLIB_C4_COMPONENT = https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Component.puml

DIAGRAMS_SRC := $(wildcard *.uml)
DIAGRAMS_PNG := $(addsuffix .png, $(basename $(DIAGRAMS_SRC)))
DIAGRAMS_SVG := $(addsuffix .svg, $(basename $(DIAGRAMS_SRC)))

# Default target first; build PNGs, probably what we want most of the time
png: plantuml.jar $(DIAGRAMS_PNG)

# SVG are nice-to-have but don't need to build by default
svg: plantuml.jar $(DIAGRAMS_SVG)

# clean up compiled files
clean:
	rm -f plantuml.jar $(DIAGRAMS_PNG) $(DIAGRAMS_SVG)

# If the JAR file isn't already present, download it
plantuml.jar: C4_Context.puml C4_Component.puml
	curl -SfL --progress-bar $(PLANTUML_JAR_URL) -o plantuml.jar

C4_Context.puml:
	curl -SfL --progress-bar $(PLANTUML_STDLIB_C4_CONTEXT) -o C4_Context.puml

C4_Component.puml:
	curl -SfL --progress-bar $(PLANTUML_STDLIB_C4_COMPONENT) -o C4_Component.puml


# Each PNG output depends on its corresponding .plantuml file
%.png: %.uml
	java -jar plantuml.jar -tpng $^

# Each SVG output depends on its corresponding .plantuml file
%.svg: %.uml
	java -jar plantuml.jar -tsvg $^

# Quirk of GNU Make: https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
.PHONY: png svg clean
