# Makefile for building Brevet card PDF in Docker

IMAGE_NAME := brevetcard-builder
CONTAINER_NAME := brevetcard-build
TEX_FILE_FRONT := brevetkarte.tex
TEX_FILE_BACK := brevetkarte-rueckseite.tex
TEX_FILE_PERSONALIZED := brevetkarte-personalized.tex
PDF_FILE_FRONT := brevetkarte.pdf
PDF_FILE_BACK := brevetkarte-rueckseite.pdf
PDF_FILE_PERSONALIZED := brevetkarte-personalized.pdf
CSV_FILE := Export Brevetkarte.csv

.PHONY: all build clean build-image build-pdf build-front build-back generate-personalized build-personalized run shell help

# Default target
all: build

# Build both PDFs (builds image if needed, then compiles)
build: build-image build-front build-back

# Build Docker image
build-image:
	@echo "Building Docker image..."
	docker build -t $(IMAGE_NAME) .

# Compile both PDFs in Docker container
build-pdf: build-front build-back

# Compile front side PDF
build-front:
	@echo "Compiling front side LaTeX to PDF..."
	docker run --rm \
		-v $(PWD):/workspace \
		$(IMAGE_NAME) \
		pdflatex -interaction=nonstopmode $(TEX_FILE_FRONT)
	@echo "PDF generated: $(PDF_FILE_FRONT)"

# Compile back side PDF
build-back:
	@echo "Compiling back side LaTeX to PDF..."
	docker run --rm \
		-v $(PWD):/workspace \
		$(IMAGE_NAME) \
		pdflatex -interaction=nonstopmode $(TEX_FILE_BACK)
	@echo "PDF generated: $(PDF_FILE_BACK)"

# Generate personalized cards from CSV
generate-personalized:
	@echo "Generating personalized cards from $(CSV_FILE)..."
	@if [ ! -f "$(CSV_FILE)" ]; then \
		echo "Error: $(CSV_FILE) not found!"; \
		exit 1; \
	fi
	python3 generate_cards.py
	@echo "Generated $(TEX_FILE_PERSONALIZED)"

# Build personalized cards PDF
build-personalized: generate-personalized build-image
	@echo "Compiling personalized cards to PDF..."
	docker run --rm \
		-v $(PWD):/workspace \
		$(IMAGE_NAME) \
		pdflatex -interaction=nonstopmode $(TEX_FILE_PERSONALIZED)
	@echo "PDF generated: $(PDF_FILE_PERSONALIZED)"

# Run container interactively
run:
	docker run --rm -it \
		-v $(PWD):/workspace \
		$(IMAGE_NAME) \
		/bin/bash

# Open shell in container for debugging
shell:
	docker run --rm -it \
		-v $(PWD):/workspace \
		--entrypoint /bin/bash \
		$(IMAGE_NAME)

# Clean generated files
clean:
	@echo "Cleaning generated files..."
	rm -f *.aux *.log *.out *.toc *.pdf

# Clean everything including Docker image
clean-all: clean
	@echo "Removing Docker image..."
	docker rmi $(IMAGE_NAME) 2>/dev/null || true

# Rebuild from scratch
rebuild: clean-all build

# Show help
help:
	@echo "Brevet Card PDF Builder"
	@echo ""
	@echo "Available targets:"
	@echo "  make build                 - Build Docker image and compile both PDFs (default)"
	@echo "  make build-image           - Build Docker image only"
	@echo "  make build-pdf             - Compile both front and back PDFs"
	@echo "  make build-front           - Compile front side PDF only"
	@echo "  make build-back            - Compile back side PDF only"
	@echo "  make generate-personalized - Generate personalized cards from CSV"
	@echo "  make build-personalized    - Generate and compile personalized cards"
	@echo "  make shell                 - Open interactive shell in container"
	@echo "  make clean                 - Remove generated files (aux, log, pdf)"
	@echo "  make clean-all             - Remove all files and Docker image"
	@echo "  make rebuild               - Clean everything and rebuild"
	@echo "  make help                  - Show this help message"
