# ====== CONFIG ======
# Add names (without extension). Example: NAMES = report thesis notes
NAMES = arch_ef_encoder arch_ef_decoder arch_lenet_encoder arch_lenet_decoder

TEX = $(NAMES:%=%.tex)
PDF = $(NAMES:%=%.pdf)

.PHONY: all clean clean-aux clobber
# Keep generated .tex even if a rule aborts
.PRECIOUS: %.tex

# Default: build all PDFs
all: $(PDF) $(TEX)

# ====== Rules ======
# Generate {name}.tex from {name}.py
# Pre-clean: remove aux + old .tex only if {name}.py is newer (or .tex missing)
%.tex: %.py
	@if [ ! -e "$@" ] || [ "$<" -nt "$@" ]; then \
	  echo "[preclean] $*: removing aux + old .tex"; \
	  rm -f "$*.aux" "$*.log" "$*.fdb_latexmk" "$*.fls" "$*.tex"; \
	fi
	@echo "python $< > $@"
	@python "$<" > "$@"

# Generate {name}.pdf from {name}.tex
# Pre-clean: remove aux (keep .tex) only if PDF is out-of-date
# Post-clean: remove aux (keep .tex) after successful pdflatex
%.pdf: %.tex
	@if [ ! -e "$@" ] || [ "$<" -nt "$@" ]; then \
	  echo "[preclean] $*: removing aux (keeping .tex)"; \
	  rm -f "$*.aux" "$*.log" "$*.fdb_latexmk" "$*.fls"; \
	fi
	@echo "pdflatex $<"
	@pdflatex --interaction=nonstopmode "$<" || { echo "pdflatex failed; keeping logs for debugging."; exit 1; }
	@rm -f "$*.aux" "$*.log" "$*.fdb_latexmk" "$*.fls"

# ====== Convenience targets ======
# Clean everything for listed names (including .tex)
clean:
	@echo "[clean] removing aux + .tex for: $(NAMES)"
	@for n in $(NAMES); do \
	  rm -f "$$n.aux" "$$n.log" "$$n.fdb_latexmk" "$$n.fls" "$$n.tex"; \
	done

# Remove only aux/log-type files (keep .tex)
clean-aux:
	@echo "[clean-aux] removing aux (keeping .tex) for: $(NAMES)"
	@for n in $(NAMES); do \
	  rm -f "$$n.aux" "$$n.log" "$$n.fdb_latexmk" "$$n.fls"; \
	done

# Nuke everything, including PDFs
clobber: clean
	@echo "[clobber] removing PDFs for: $(NAMES)"
	@for n in $(NAMES); do rm -f "$$n.pdf"; done

