# Copyright (C) 2025 by Charles P. Schaum <charles[dot]schaum@comcast.net>
# -----------------------------------------------
#
# This file may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either
# version 1.3 of this license or (at your option) any
# later version. The latest version of this license is in:
#
#   http://www.latex-project.org/lppl.txt
#
# and version 1.3 or later is part of all distributions
# of LaTeX version 2005/12/01 or later.
#
# This work includes all source and generated files
# described as such in README.md, included herein.  
#
# -----------------------------------------------
#
# Usage:
#
# 	Delete all generated files, do clean build on
# 	POSIX--compliant systems, WSL, or Cygwin.
#
# 		make
#
#	This target does the same default build as above.
#
# 		make release
#
# 	Just do a quick rebuild; keep generated files.
# 	One must edit or touch `$(NAME).dtx'.
#
#		make package
#
#	Install/remove supplied files into the user's texmf tree.
#	These targets only work in a POSIX environment and do not
#	install from WSL or Cygwin into a Windows environment
#	They have not been tested on MikTeX for Linux.
#
#		make inst
#		make uninst
#
#	Install/remove supplied files into the local texmf tree.
#	Needs an appropriate level of permission. These targets
#	only work in a fully POSIX environment. They have not
#	been tested on MikTeX for Linux. Note that one should
#	not put "sudo" before these commands because they will
#	not find the proper path variables. Just let the make
#	target ask for the password.
#
#		make install
#		make uninstall
#
#	Add options if needed, such as
#
#		make ADDOPTS="--synctex=1"
#
#	Compile examples file with pdflatex.
#
#		make examples
#
#	Compile examples file with all different engines.
#
#		make examples ENGINE=xelatex
#		make examples ENGINE=lualatex
#		make examples ENGINE=dvilualatex
#		make examples ENGINE=pdflatex
#		make examples ENGINE=latex
#
#	Compile test files with pdflatex.
#
#		make tests
#
#	Compile test files with all different engines.
#
#		make tests ENGINE=xelatex
#		make tests ENGINE=lualatex
#		make tests ENGINE=dvilualatex
#		make tests ENGINE=pdflatex
#		make tests ENGINE=latex
#
#	Clean up auxiliary files for the package, examples, and tests.
#
#		make clean
#		make exclean
#		make testclean
#
#	Remove all generated files.
#
#		make distclean
#
#	Regenerate files from the dtx file if needed. This target
#	does not build the package. It is handy when editing the
#	dtx to work on README.md, examples.tex, and the like.
#
#		make unpack
#
#	Generate manual illustrations on POSIX-compliant systems
#	and TeX distributions that support doing so. This requires
#	the use of pdfjam, which is not available in some cases.
#
#		make images
#
#	Do a clean package build and create a zip archive
#	ready for distribution.
#
#		make zip
#
# -----------------------------------------------
#
# Programs necessary to execute this make file:
#
#	POSIX Build Utilities
#		bash
#		make
#		sed
#		zip
#	TeX Distro Programs
#		pdflatex
#		pdftex
#		kpsewhich
#		makeindex
#
# -----------------------------------------------
#
# Recommended prerequisites, primary tier:
#
#	TeX Distro Programs
#		xelatex
#		lualatex
#		dvilualatex
#		latex
#		ltxfileinfo
#		dvipdfmx
#
# -----------------------------------------------
#
# Recommended prerequisites, secondary tier:
#
#	POSIX Build Utilities
#		dvipdf
#	TeX Distro Programs
#		pdfjam
#
# -----------------------------------------------
#

#
# Assign most variables in this section. Here we determine what
# programs exist and choices related to that.
#

# Name of package

NAME  = nameauth

# Shell to use; default is bash

SHELL = bash

# Value of current working directory

PWD = $(shell pwd)

# LaTeX engines to use for building the package and for typesetting
# the examples file and testing files; below is the default.
# One can choose from latex, pdflatex, xelatex, lualatex, dvilualatex.

ENGINE ?= pdflatex
BUILDENGINE := $(shell command -v pdflatex 2> /dev/null)
TESTENGINE := $(shell command -v $(ENGINE) 2> /dev/null)

# If we cannot find the engine, add a .exe extension and try that instead.
# This usually occurs only when using WSL and a Windows-native TeX distro.

ifeq ($(strip $(BUILDENGINE)),)
	BUILDENGINE = pdflatex
	EXT = .exe
else
	BUILDENGINE = pdflatex
	EXT =
endif

ifeq ($(strip $(TESTENGINE)),)
	TESTENGINE = $(ENGINE)
	EXT = .exe
else
	TESTENGINE = $(ENGINE)
	EXT =
endif

# Now if we still can't find things, something is very wrong.

ifeq ($(strip $(BUILDENGINE)$(EXT)),)
	$(error Cannot find $(BUILDENGINE). Please check your installation.")
endif

ifeq ($(strip $(TESTENGINE)$(EXT)),)
	$(error Cannot find $(TESTENGINE). Please check your installation.")
endif

# Get package version info; check if not available in some environments.
# Create a zip file name that will not cause problems, e.g., in Windows.

DOVERSION := $(shell command -v ltxfileinfo 2> /dev/null)

ifeq ($(strip $(DOVERSION)),)
	ZIPNAME = $(NAME)
else
	ZIPNAME = $(NAME)-$(shell ltxfileinfo -v $(NAME).dtx|sed -e 's/^v//')
endif

# Determine which dvi to pdf converter to use. If Ghostscript is installed
# use dvipdf, otherwise dvipdfmx.

DVIPDF := $(shell command -v dvipdf 2> /dev/null)

ifeq ($(strip $(DVIPDF)),)
	DVIPDF = dvipdfmx
else
	DVIPDF = dvipdf
endif

# Test if pdfjam is available.

PDFJAM := $(shell command -v pdfjam 2> /dev/null)

# Build options are in this variable.

BUILDOPTS = --recorder --interaction=nonstopmode

# Test and example options are in this variable.

TESTOPTS = --interaction=nonstopmode

# Add options with this variable, e.g.: make ADDOPTS="--synctex=1"

ADDOPTS ?= 

# Core options for generating the sty file and included files
# the normal way. Alternate is via make unpack.

COREOPTS = --shell-escape --recorder --interaction=batchmode

# Local, system-wide tex tree

LOCAL = $(shell kpsewhich$(EXT) --var-value TEXMFLOCAL)

# Tex tree in user's home directory

UTREE = $(shell kpsewhich$(EXT) --var-value TEXMFHOME)

#
# Package Building Section
#

# Default make target is the package release and its dependencies.
# When building, erase all generated files to make a fresh build.

release : distclean package clean

# Use this recipe to make the package without removing all prior files.

package : $(NAME).pdf
	@echo "Package has been made successfully."

# This is the recipe for the package and the docs.

$(NAME).pdf : $(NAME).sty
	$(BUILDENGINE)$(EXT) $(BUILDOPTS) $(ADDOPTS) $(NAME).dtx > /dev/null
	makeindex$(EXT) -q -s gglo.ist -o $(NAME).gls $(NAME).glo
	makeindex$(EXT) -q -s gind.ist -o $(NAME).ind $(NAME).idx
	$(BUILDENGINE)$(EXT) $(BUILDOPTS) $(ADDOPTS) $(NAME).dtx > /dev/null
	$(BUILDENGINE)$(EXT) $(BUILDOPTS) $(ADDOPTS) $(NAME).dtx > /dev/null

# This is the core dependency. When run we get all extracted files:
# README.md, nameauth.ins, nameauth.sty, examples.tex, and the test
# files as well.

$(NAME).sty : $(NAME).dtx
	$(BUILDENGINE)$(EXT) $(COREOPTS) $(ADDOPTS) $(NAME).dtx > /dev/null

#
# Examples and Testing Section
#

# Compile the test files into their output documents.

tests :
	$(TESTFILES)
	$(MAKE) testout
	@echo "Test files have been made successfully."

testout : test01_01.pdf \
	test05_01.pdf \
	test07_01.pdf test07_02.pdf \
	test09_01.pdf \
	test11_01.pdf test11_02.pdf test11_03.pdf test11_04.pdf \
	test11_05.pdf test11_06.pdf test11_07.pdf test11_08.pdf \
	test11_09.pdf test11_10.pdf \
	test13_01.pdf test13_02.pdf

# Regardless of what engine is used for tests, one must use the
# build engine (pdflatex) to extract the tests.

TESTFILES = $(BUILDENGINE)$(EXT) \
		"\def\NameauthDoTestFiles{}\input{nameauth.dtx}" \
		> /dev/null

# Make the example pdf using the TeX to pdf pattern rule.
# Ensure that at least the style file exists when doing so.

examples : $(NAME).sty examples.pdf
	@echo "Examples file has been made successfully."

# Pattern for pdf files; works on tests or examples with or without
# indexes, those with multiple indexes, and with or without TOC.

%.pdf : %.tex
	$(TESTENGINE)$(EXT) $(TESTOPTS) $< > /dev/null
	$(TESTENGINE)$(EXT) $(TESTOPTS) $< > /dev/null
	if [ -f $*.idx ]; \
		then makeindex$(EXT) -q -o $*.ind $*.idx > /dev/null; fi
	if [ -f $*.rdx ]; \
		then makeindex$(EXT) -q -o $*.rnd $*.rdx > /dev/null; fi
	$(TESTENGINE)$(EXT) $(TESTOPTS) $< > /dev/null
	$(TESTENGINE)$(EXT) $(TESTOPTS) $< > /dev/null
	if [ -f $*.dvi ]; then $(DVIPDF)$(EXT) $*; fi
	if [ -f $*.out.ps ]; then rm $*.out.ps; fi

#
# Generate Manual Images from Examples
#

# Create example images instead of using the supplied pdf files.
# For this recipe one needs the pdfjam script to be somewhere in
# the command search path. Otherwise, this recipe will fail.
# In case of failure, use another application to split the pdf
# files and rename the appropriate pages, or use the pdfpages
# package. Normally, users should not need to make this recipe.
# This target may fail if using MikTeX.

images :
	$(TESTFILES)
	$(MAKE) imageout ENGINE=pdflatex
ifeq ($(strip $(PDFJAM)),)
	@echo "Image files have been typeset, but you need to split \
	       and rename the pages manually; pdfjam is not available. Sorry."
	@echo "Page 3 of test07_02.pdf -> cat01.pdf"
	@echo "Pages 1,2,3 of testo9_01.pdf -> beamer01.pdf, \
	       beamer02.pdf, beamer03.pdf"
else
	pdfjam --paper a6paper --landscape --quiet \
	       test07_02.pdf 3 --outfile cat01.pdf
	pdfjam --paper a5paper --landscape --quiet \
	       test09_01.pdf 1 --outfile beamer01.pdf
	pdfjam --paper a5paper --landscape --quiet \
	       test09_01.pdf 2 --outfile beamer02.pdf
	pdfjam --paper a5paper --landscape --quiet \
	       test09_01.pdf 3 --outfile beamer03.pdf
	@echo "Image files have been made successfully."
endif

imageout : test07_02.pdf test09_01.pdf

#
# Utility Section
#

# Set up phony targets that do not depend on any specific files.

.PHONY: tests images unpack clean exclean testclean distclean uninst ununstall

# Simply unpack the other supplied files from the dtx file.
# Not usually needed to build the package, but can be used,
# for example, in case one accidentally deletes a file.

unpack :
	pdftex$(EXT) $(NAME).dtx

# Delete all auxiliary and log files for the package; prep for
# re-running building and packaging targets. The file
# "nameauth.dtx.aux" has appeared in some systems. Just in case
# someone tries to create a "nameauth.dvi" file, we delete it.

clean :
	rm -f $(NAME).{aux,glo,gls,idx,rdx,ilg,ind,rnd,toc}
	rm -f $(NAME).{dvi,fls,hd,log,out,tmp}
	rm -f manhook.tmp nullhook.tmp
	rm -f $(NAME).synctex.gz
	rm -f $(NAME).'synctex(busy)'
	rm -f $(NAME).dtx.aux

# Delete all auxiliary and log files for the examples files; leave
# just the TeX file for re-running the examples target.

exclean :
	rm -f examples.{aux,glo,gls,idx,rdx,ilg,ind,rnd,toc}
	rm -f examples.{dvi,fls,hd,log,out,pdf,tmp}
	rm -f examples.synctex.gz
	rm -f examples.'synctex(busy)'

# Delete all test-related files to prep for re-running tests.

testclean :
	rm -f test*

# Delete all generated files related to package building except zip.

distclean : clean exclean testclean
	rm -f $(NAME).{pdf,ins,sty,zip}
	rm -f README.md
	rm -f compat.tex
	rm -f examples.tex

# Install the package release into the user's tree.
# Note that this target depends on the "package" target,
# so we do not do a clean build. This may aid testing.

inst : package
ifeq ($(strip $(UTREE)),)
	$(error "Unable to install; no path to user tree." )
else
	mkdir -p $(UTREE)/{tex,source,doc}/latex/$(NAME)
	cp $(NAME).dtx $(UTREE)/source/latex/$(NAME)
	cp Makefile $(UTREE)/source/latex/$(NAME)
	cp $(NAME).sty $(UTREE)/tex/latex/$(NAME)
	cp $(NAME).pdf $(UTREE)/doc/latex/$(NAME)
	cp *.tex $(UTREE)/doc/latex/$(NAME)
	cp README.md $(UTREE)/doc/latex/$(NAME)
endif

# Install the package release into the system tree.
# Note that this target depends on the "package" target,
# so we do not do a clean build. This may aid testing.

install : package
ifeq ($(strip $(LOCAL)),)
	$(error "Unable to install; no path to local tree." )
else
	sudo mkdir -p $(LOCAL)/{tex,source,doc}/latex/$(NAME)
	sudo cp $(NAME).dtx $(LOCAL)/source/latex/$(NAME)
	sudo cp Makefile $(LOCAL)/source/latex/$(NAME)
	sudo cp $(NAME).sty $(LOCAL)/tex/latex/$(NAME)
	sudo cp $(NAME).pdf $(LOCAL)/doc/latex/$(NAME)
	sudo cp *.tex $(LOCAL)/doc/latex/$(NAME)
	sudo cp README.md $(LOCAL)/doc/latex/$(NAME)
endif

# Uninstall the package release from the user's tree.
# Show the state of the texmf tree thereafter.

uninst : 
ifeq ($(strip $(LOCAL)),)
	$(error "Unable to uninstall; no path to user tree." )
else
	rm -f $(UTREE)/source/latex/$(NAME)/$(NAME).dtx 
	rm -f $(UTREE)/source/latex/$(NAME)/Makefile
	rm -f $(UTREE)/tex/latex/$(NAME)/$(NAME).sty
	rm -f $(UTREE)/doc/latex/$(NAME)/$(NAME).pdf 
	rm -f $(UTREE)/doc/latex/$(NAME)/*.tex
	rm -f $(UTREE)/doc/latex/$(NAME)/README.md 
	rmdir $(UTREE)/{tex,source,doc}/latex/$(NAME)
	@echo "The texmf tree now looks like:"
	ls $(UTREE)/source/latex/
	ls $(UTREE)/tex/latex/
	ls $(UTREE)/doc/latex/
endif

# Uninstall the package release from the system tree.

uninstall : 
ifeq ($(strip $(LOCAL)),)
	$(error "Unable to uninstall; no path to local tree." )
else
	sudo rm -f $(LOCAL)/source/latex/$(NAME)/$(NAME).dtx 
	sudo rm -f $(LOCAL)/source/latex/$(NAME)/Makefile
	sudo rm -f $(LOCAL)/tex/latex/$(NAME)/$(NAME).sty
	sudo rm -f $(LOCAL)/doc/latex/$(NAME)/$(NAME).pdf
	sudo rm -f $(LOCAL)/doc/latex/$(NAME)/*.tex
	sudo rm -f $(LOCAL)/doc/latex/$(NAME)/README.md
	sudo rmdir $(LOCAL)/{tex,source,doc}/latex/$(NAME)
	@echo "The texmf tree now looks like:"
	ls $(LOCAL)/source/latex/
	ls $(LOCAL)/tex/latex/
	ls $(LOCAL)/doc/latex/
endif

# Create a zip file for upload to CTAN. On systems where ltxfileinfo
# cannot be found, we create a zip file without a version in its name
# instead of a zip file with a dash at the end, which can cause some
# problems on Windows in certain cases.

zip : release
	rm -f $(NAME)*.zip
	ln -sf . $(NAME)
	zip -Drq $(PWD)/$(ZIPNAME).zip \
	$(NAME)/{Makefile,\
	cat01.pdf,beamer01.pdf,beamer02.pdf,beamer03.pdf,\
	$(NAME).dtx,\
	compat.tex,examples.tex,$(NAME).pdf,README.md}
	rm $(NAME)
	@echo "Zip file has been made successfully."