# 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
PDF_FILE_FRONT := brevetkarte.pdf
PDF_FILE_BACK := brevetkarte-rueckseite.pdf

.PHONY: all build clean build-image build-pdf build-front build-back 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)"

# 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 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"
