TEXS = $(wildcard *.tex)
MAIN = sample-use-sty

.SILENT: distrib all


all: $(MAIN).pdf software-biblatex.pdf

biblatex-styles: stublist mkbiblatexstubs.sh
	bash mkbiblatexstubs.sh < stublist

biblio.bib: ../swentry.org
	../bin/extract-bibtex.pl < $< > $@

swentries.tex: ../swentry.org
	../bin/export-org-to-latex $<
	../bin/extract-entries.pl < ../swentry.tex > $@

software-biblatex.pdf: software-biblatex.tex swentries.tex history.tex biblio.bib manual.bib
	pdflatex software-biblatex.tex > /dev/null
	bibtex software-biblatex
	pdflatex software-biblatex.tex > /dev/null
	pdflatex software-biblatex.tex > /dev/null

sample-use-sty.pdf: biblio.bib sample-content.tex sample-use-sty.tex

sample-use-sty.nohyperref.tex: sample-use-sty.tex
	grep -v hyperref $< > $@

sample-use-sty.nohyperref.pdf: biblio.bib sample-content.tex sample-use-sty.nohyperref.tex

sample-use-sty.backref.pdf: biblio.bib sample-content.tex
	# test that backrefs work
	pdflatex -jobname sample-use-sty.backref "\PassOptionsToPackage{backref=true}{biblatex} \input{sample-use-sty.tex}" > /dev/null
	biber sample-use-sty.backref > /dev/null
	pdflatex -jobname sample-use-sty.backref "\PassOptionsToPackage{backref=true}{biblatex} \input{sample-use-sty.tex}" > /dev/null
	pdflatex -jobname sample-use-sty.backref "\PassOptionsToPackage{backref=true}{biblatex} \input{sample-use-sty.tex}" > /dev/null

history.tex:
	git for-each-ref --format="\begin{release}{%(refname:short)}{%(taggerdate:short)} \item %(subject) \end{release}" refs/tags | grep bltx | tac > history.tex

sample.tex : biblio.bib biblatex-styles

%.pdf: %.tex
ifeq (, $(shell PATH=$(PATH) which rubber))
	echo "No rubber in $(PATH), falling back to basic pdflatex calls"
	pdflatex $< > /dev/null
	biber $* > /dev/null
	pdflatex $< > /dev/null
	pdflatex $< > /dev/null
else
	rubber -m pdftex $<
endif

%.txt: %.pdf
	pdftotext $<

test:	sample-use-sty.txt sample-use-sty.nohyperref.txt sample-use-sty.backref.txt
	@echo "========================================"
	@echo "Running: Basic Functionality Tests"
	@echo "========================================"
	@echo ""
	@# check that we get the same results with and without hyperref
	@# normalize citations (hyperref changes [key] to [number], so we normalize both)
	@# and normalize whitespace to handle line wrapping differences
	@echo "Test: Hyperref vs Nohyperref Compatibility"
	@(sed 's/\[[^]]*\]/[CITATION]/g' sample-use-sty.txt | tr -s ' ' | sed 's/^[[:space:]]*//;s/[[:space:]]*$$//' > /tmp/test-with.txt && \
	 sed 's/\[[^]]*\]/[CITATION]/g' sample-use-sty.nohyperref.txt | tr -s ' ' | sed 's/^[[:space:]]*//;s/[[:space:]]*$$//' > /tmp/test-without.txt && \
	 diff /tmp/test-with.txt /tmp/test-without.txt > /dev/null 2>&1; \
	 if [ $$? -eq 0 ]; then \
	   printf "  \033[0;32m✓ PASS\033[0m: Hyperref vs Nohyperref Compatibility\n"; \
	 else \
	   printf "  \033[0;31m✗ FAIL\033[0m: Hyperref vs Nohyperref Compatibility\n"; \
	   echo "  Differences found between hyperref and nohyperref outputs"; \
	   exit 1; \
	 fi)
	@echo ""
	@# check that backrefs work
	@echo "Test: Backref Functionality"
	@(grep "cited on page" sample-use-sty.backref.txt > /dev/null 2>&1; \
	 if [ $$? -eq 0 ]; then \
	   printf "  \033[0;32m✓ PASS\033[0m: Backref Functionality\n"; \
	 else \
	   printf "  \033[0;31m✗ FAIL\033[0m: Backref Functionality\n"; \
	   echo "  Backref markers not found in output"; \
	   exit 1; \
	 fi)
	@echo ""
	@# run ultra-concise format tests
	@echo "========================================"
	@echo "Running: Ultra-Concise Format Tests"
	@echo "========================================"
	@cd tests && bash run-ultra-concise-tests.sh
	@echo ""
	@# run comprehensive bash test suite
	@echo "========================================"
	@echo "Running: Comprehensive Test Suite"
	@echo "========================================"
	@cd tests && bash run-all-tests.sh

distrib:
	# remind maintainer to update version number in the documentation
	echo -n "Updated version number in software-biblatex.tex, date strings in software.* ? [y/N]"
	read ans; if [ ".$$ans" != ".y" ]; then echo "Please update this information and run make distrib again"; exit 1; fi
	# update history
	rm history.tex
	make software-biblatex.pdf
	make showcase-crossref.pdf
	rm -rf biblatex-software
	mkdir biblatex-software
	# copy all tracked files except tests/ directory and HOWTO-RELEASE.md
	# use -L to follow symlinks (converts Changes symlink to plain file)
	git ls-files | grep -v "^tests/" | grep -v "^HOWTO-RELEASE.md$$" | while read file; do \
		if [ -e "$$file" ]; then \
			mkdir -p biblatex-software/$$(dirname "$$file" 2>/dev/null || echo .); \
			if [ -d "$$file" ]; then \
				cp -rL "$$file" biblatex-software/; \
			else \
				cp -L "$$file" biblatex-software/"$$file"; \
			fi; \
		fi; \
	done
	# explicitly include showcase-crossref.pdf if it exists (even if not yet tracked)
	if [ -f showcase-crossref.pdf ]; then \
		cp -L showcase-crossref.pdf biblatex-software/; \
	fi
	tar -czvf biblatex-software.tar.gz biblatex-software

.PHONY: preserve restore clean distclean test

clean:
	# preserve PDF files under version control
	mkdir -p .save
	mv `git ls-files | grep pdf` .save
	# clean up the rest
	make cleaninner
	# restore PDF files under version control
	mv .save/*.pdf .
	rmdir .save

cleaninner: $(patsubst %,%/clean,$(TEXS))
	rm -f *+sw.bbx *+sw.dbx *+sw.cbx *+sw.lbx *~ *.txt sample-use-sty.backref.* sample-use-sty.nohyperref.*

%/clean:
	if [ -f $* ]; then rubber -m pdftex --clean $* ; fi

distclean: clean
