PACKAGE_STY = $(wildcard *.sty)
PACKAGE_IMG = $(wildcard ccc_img/*)

DESTDIR     ?= $(shell kpsewhich -var-value=TEXMFHOME)
INSTALL_DIR  = $(DESTDIR)/tex/latex/ccc

EXAMPLE_SRC  = example.tex
EXAMPLE_PDF  = example.pdf

CACHE_DIR   := $(shell pwd)/.latex-cache

COMPILE_TEX := latexmk -lualatex  -latexoption="-shell-escape"  -output-directory="$(CACHE_DIR)"

.PHONY: install uninstall example all clean

all: example

example: $(EXAMPLE_PDF)

install: $(PACKAGE_STY)
	@mkdir -p "$(INSTALL_DIR)"
	@cp $(PACKAGE_STY) "$(INSTALL_DIR)"
	@mkdir -p "$(INSTALL_DIR)/ccc_img"
	@cp $(PACKAGE_IMG) "$(INSTALL_DIR)/ccc_img"

uninstall:
	@rm -f $(addprefix "$(INSTALL_DIR)"/, $(PACKAGE_STY))
	@rm -f $(addprefix "$(INSTALL_DIR)"/, $(PACKAGE_IMG))
	@rmdir "$(INSTALL_DIR)/ccc_img"
	@rmdir "$(INSTALL_DIR)"

clean:
	@rm -rf "$(CACHE_DIR)"
	@rm -f "$(EXAMPLE_PDF)"

$(EXAMPLE_PDF) : $(EXAMPLE_SRC)
	@$(COMPILE_TEX) $(EXAMPLE_SRC)
	@cp "$(CACHE_DIR)/$(EXAMPLE_PDF)" .