# makefile
# Machine Dossier:  Benton Engraving Machine BEM2a-53
#                   (Vertical Pantograph, Type 2a, 1899 Patent)
#                   At CircuitousRoot
#                   GNU Make makefile
#
# Copyright 2023 by Dr. David M. MacMillan
#
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU Lesser General Public License as published by 
# the Free Software Foundation, either version 3 of the License, 
# or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License 
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Encoding:  UTF-8, Unicode
# Requires:  XeLaTeX (xelatex)

TEXFRAGS=$(wildcard *.texfrag)
TEXS=$(wildcard *.tex)
TEXBASES=$(basename $(TEXS))
PDFS=$(patsubst %.tex, %.pdf, $(TEXS))

RETAUX=-1
RETTOC=-1

all: $(PDFS)

all:

.ONESHELL:

$(PDFS): %.pdf : %.tex $(TEXFRAGS)
	# save the old .aux file
	if [ -f "$*.aux" ]; then \
	   cp -f $*.aux $*-old.aux; \
	else\
	   touch $*-old.aux; \
	fi
	# save the old .toc file
	if [ -f "$*.toc" ]; then \
	   cp -f $*.toc $*-old.toc; \
	else\
	   touch $*-old.toc; \
	fi
	xelatex $*.tex
#
#	Check for first potential re-run:
	echo TESTING for rerun; \
	ls -l $*.aux $*-old.aux; \
	cmp -s $*-old.aux $*.aux; \
	if [ $$? -eq 1 ]; then \
	   echo cmp aux returns nonzero, maybe rerun; \
	   cmp -s $*-old.toc $*.toc; \
	   if [ $$? -eq 1 ]; then \
	      echo cmp toc returns nonzero, do rerun; \
	      xelatex $*.tex; \
           else \
	      echo cmp toc returns zero, DO NOT rerun; \
	   fi \
	else \
	   echo cmp aux returns zero, DO NOT rerun; \
	fi
#
#	Check for second potential re-run:
	echo TESTING for rererun; \
	ls -l $*.aux $*-old.aux; \
	cmp -s $*-old.aux $*.aux; \
	if [ $$? -eq 1 ]; then \
	   echo cmp aux returns nonzero, maybe rererun; \
	   cmp -s $*-old.toc $*.toc; \
	   if [ $$? -eq 1 ]; then \
	      echo cmp toc returns nonzero, do rererun; \
	      xelatex $*.tex; \
           else \
	      echo cmp toc returns zero, DO NOT rererun; \
	   fi \
	else \
	   echo cmp aux returns zero, DO NOT rererun; \
	fi
#
	pkill -HUP mupdf


clean:
	rm -f *.aux
	rm -f *.log
	rm -f *.out
	rm -f *.toc

veryclean: clean
	rm -f *.pdf

