# Makefile for eigo
# Documentation generation and CTAN preparation

PACKAGE = eigo
VERSION = 0.9
DATE = 2025-09-09

# Source files
STYLE = $(PACKAGE).sty
DOC = $(PACKAGE).tex
README = README.md

# Generated files
PDF = $(PACKAGE).pdf
INS = $(PACKAGE).ins
DTX = $(PACKAGE).dtx

# Distribution directory
DISTDIR = $(PACKAGE)-$(VERSION)

.PHONY: all doc clean dist ctan-zip test install ctan-archive

all: doc

# Documentation generation
doc: $(PDF)

$(PDF): $(DOC) $(STYLE)
	pdflatex $(DOC)
	pdflatex $(DOC)
	pdflatex $(DOC)

# Compilation tests with different options
test: $(STYLE)
	@echo "Testing basic compilation..."
	echo 'x' | pdflatex "\documentclass{article}\usepackage{$(PACKAGE)}\begin{document}\blackstones{d4}\showfullgoban\end{document}" || true
	@echo "Testing with frame option..."
	echo 'x' | pdflatex "\documentclass{article}\usepackage[frame]{$(PACKAGE)}\begin{document}\blackstones{d4}\showfullgoban\end{document}" || true
	@echo "Testing color mode..."
	echo 'x' | pdflatex "\documentclass{article}\usepackage[color]{$(PACKAGE)}\begin{document}\blackstones{d4}\showfullgoban\end{document}" || true
	@echo "Testing LuaLaTeX compatibility..."
	echo 'x' | lualatex "\documentclass{article}\usepackage{$(PACKAGE)}\begin{document}\blackstones{d4}\showfullgoban\end{document}" || true
	@echo "Tests completed (errors expected for inline tests)!"

# Code quality validation
validate: $(STYLE)
	@echo "Validating package structure..."
	@grep -q "NeedsTeXFormat{LaTeX2e}" $(STYLE) || (echo "Missing LaTeX2e requirement" && exit 1)
	@grep -q "ProvidesPackage{$(PACKAGE)}" $(STYLE) || (echo "Missing package declaration" && exit 1)
	@grep -q "LaTeX Project Public License" $(STYLE) || (echo "Missing license declaration" && exit 1)
	@echo "Package structure validation passed!"

# Clean temporary files
clean:
	rm -f *.aux *.log *.toc *.out *.fdb_latexmk *.fls *.synctex.gz
	rm -f texput.pdf texput.aux texput.log

# Complete distribution
dist: doc validate
	mkdir -p $(DISTDIR)
	cp $(STYLE) $(DISTDIR)/
	cp $(DOC) $(DISTDIR)/
	cp $(PDF) $(DISTDIR)/
	cp $(README) $(DISTDIR)/
	cp LICENSE.txt $(DISTDIR)/
	cp Makefile $(DISTDIR)/

# CTAN-compliant archive creation
ctan-archive: doc validate
	@echo "Creating CTAN-compliant archive..."
	mkdir -p $(DISTDIR)
	mkdir -p $(DISTDIR)/$(PACKAGE)
	cp $(STYLE) $(DISTDIR)/$(PACKAGE)/
	cp $(DOC) $(DISTDIR)/$(PACKAGE)/
	cp $(PDF) $(DISTDIR)/$(PACKAGE)/
	cp $(README) $(DISTDIR)/$(PACKAGE)/
	cp LICENSE.txt $(DISTDIR)/$(PACKAGE)/
	cp Makefile $(DISTDIR)/$(PACKAGE)/
	cd $(DISTDIR) && zip -r ../$(PACKAGE)-$(VERSION).zip $(PACKAGE)/
	@echo "CTAN archive created: $(PACKAGE)-$(VERSION).zip"
	@echo "Archive size: $$(du -h $(PACKAGE)-$(VERSION).zip | cut -f1)"
	@echo "Archive structure:"
	@unzip -l $(PACKAGE)-$(VERSION).zip
	rm -rf $(DISTDIR)

# Standard archive for CTAN
ctan-zip: ctan-archive

# Local installation
install: $(STYLE) $(PDF)
	mkdir -p ~/texmf/tex/latex/$(PACKAGE)
	mkdir -p ~/texmf/doc/latex/$(PACKAGE)
	cp $(STYLE) ~/texmf/tex/latex/$(PACKAGE)/
	cp $(PDF) ~/texmf/doc/latex/$(PACKAGE)/
	cp $(README) ~/texmf/doc/latex/$(PACKAGE)/
	texhash ~/texmf