## This file is part of the book                 ##
##                                               ##
##   Abstract Algebra: Theory and Applications   ##
##                                               ##
## Copyright (C) 2015-2022  Robert A. Beezer     ##
## See the file COPYING for copying conditions.  ##

#######################
# DO NOT EDIT THIS FILE
#######################

#   1) Do make a copy of Makefile.paths.original
#      as Makefile.paths
#   2) Edit Makefile.paths as directed there
#   3) This file (Makefile) and Makefile.paths.original
#      are managed by revision control and edits will conflict
#   4) See updated history in Makefile.paths.original
#      for changes, or follow the revision control history

##############
# Introduction
##############

# This is not a "true" makefile, since it does not
# operate on dependencies.  It is more of a shell
# script, sharing common configurations

# This is mostly offered as an example of one approach
# to managing a project with multiple output formats. and
# is not claimed to be "best practice"

######################
# System Prerequisites
######################

#   install         (system tool to make directories)
#   xsltproc        (xml/xsl text processor)
#   <helpers>       (PDF viewer, web browser, pager, Sage executable, etc)

#####
# Use
#####

#	A) Set default directory to be the location of this file
#	B) At command line:  make html  (and employ targets)

# The included file contains customized versions
# of locations of the principal components of this
# project and names of various helper executables
include Makefile.paths

# These paths are subdirectories of
# the Mathbook XML distribution
# MBUSR is where extension files get copied
# so relative paths work properly
MBXSL = $(MB)/xsl
MBSCRIPT = $(MB)/pretext
MBUSR = $(MB)/user

# These are source and custom XSL subdirectories
# for the two AATA repositories
SRC = $(AATA)/src
XSL = $(AATA)/xsl
PUB = $(AATA)/publisher

# These paths are subdirectories of
# a scratch directory
DIAGOUT    = $(SCRATCH)/diagram
HTMLOUT    = $(SCRATCH)/html
RSOUT      = $(SCRATCH)/runestone
PDFOUT     = $(SCRATCH)/pdf
LATEXOUT   = $(SCRATCH)/latex
DOCTEST    = $(SCRATCH)/doctest
EPUBOUT    = $(SCRATCH)/epub
JUPYTEROUT = $(SCRATCH)/jupyter
BRAILLEOUT = $(SCRATCH)/braille


###############
# Preliminaries
###############

# Diagrams
#   invoke mbx script to manufacture diagrams
#   tikz as SVG for HTML
#   one sageplot as PDF for LaTeX, SVG for HTML
#   these outputs are in source repo now, and
#   are typically just copied out
#   this should be run if diagram source changes
#   NB: need to manually copy these to commit to source,
#       on a branch for testing
#   NB: could upgrade to use publisher file for location?
diagrams:
	install -d $(DIAGOUT)/generated/latex-image  $(DIAGOUT)/generated/sageplot
	$(MBSCRIPT)/pretext -v -c latex-image -f svg -d $(DIAGOUT)/generated/latex-image $(SRC)/aata.xml
	$(MBSCRIPT)/pretext -v -c sageplot    -f pdf -d $(DIAGOUT)/generated/sageplot $(SRC)/aata.xml
	$(MBSCRIPT)/pretext -v -c sageplot    -f svg -d $(DIAGOUT)/generated/sageplot $(SRC)/aata.xml


##########
# Products
##########

# HTML version
#   Copies in image files from source directory
#   Publisher file is a relative path to master source file
#   Move to server: generated *.html and
#   entire directories - /images and /knowl
html:
	install -d $(HTMLOUT)
	$(MBSCRIPT)/pretext -vv -c all -f html -p $(PUB)/public.xml -d $(HTMLOUT) $(SRC)/aata.xml

viewhtml:
	$(HTMLVIEWER) $(HTMLOUT)/aata.html &

# Full PDF version with Sage
#   Includes *all* material, and is fully electronic
#   For the website downloads this is
#   aata-YYYYMMDD-sage-N-M.pdf version
sage:
	install -d $(MBUSR)
	cp $(XSL)/aata-latex.xsl $(MBUSR)
	install -d $(PDFOUT)
	$(MBSCRIPT)/pretext -vv -c all -f pdf -p $(PUB)/public.xml -X $(MBUSR)/aata-latex.xsl -d $(PDFOUT) $(SRC)/aata.xml
	mv $(PDFOUT)/aata.pdf $(PDFOUT)/aata-sage.pdf

# View PDF from correct directory
viewsage:
	$(PDFVIEWER) $(PDFOUT)/aata-sage.pdf &

# Electronic PDF version
#   Similar to "print" but with links, etc.
#   No Sage material
#   This is default downloadable Annual Edition
#   ie, aata-YYYYMMDD.pdf in repository download section
electronic:
	install -d $(MBUSR)
	cp $(XSL)/aata-latex.xsl $(MBUSR)
	install -d $(PDFOUT)
	$(MBSCRIPT)/pretext -vv -c all -f pdf -p $(PUB)/electronic.xml -X $(MBUSR)/aata-latex.xsl -d $(PDFOUT) $(SRC)/aata.xml
	mv $(PDFOUT)/aata.pdf $(PDFOUT)/aata-electronic.pdf

# View PDF from correct directory
viewelectronic:
	$(PDFVIEWER) $(PDFOUT)/aata-electronic.pdf &

# Print PDF version
#   A print version for print-on-demand
#   This will be source for the Annual Edition,
#     as sent to Orthogonal Publishing for modification
#   Black on white, no live URLs, etc
#   For the website downloads this is
#   aata-YYYYMMDD-print.pdf version
#   See below and keep in sync
print:
	install -d $(MBUSR)
	cp $(XSL)/aata-latex.xsl $(MBUSR)
	install -d $(PDFOUT)
	$(MBSCRIPT)/pretext -vv -c all -f pdf -p $(PUB)/print.xml -X $(MBUSR)/aata-latex.xsl -d $(PDFOUT) $(SRC)/aata.xml
	mv $(PDFOUT)/aata.pdf $(PDFOUT)/aata-print.pdf

# LaTeX for Print PDF version
#   See above, and keep in sync
latex-source:
	install -d $(MBUSR)
	cp $(XSL)/aata-latex.xsl $(MBUSR)
	install -d $(LATEXOUT)
	$(MBSCRIPT)/pretext -vv -c all -f latex -p $(PUB)/print.xml -X $(MBUSR)/aata-latex.xsl -d $(LATEXOUT) $(SRC)/aata.xml
	mv $(LATEXOUT)/aata.tex $(LATEXOUT)/aata-print.tex

# View PDF from correct directory
viewprint:
	$(PDFVIEWER) $(PDFOUT)/aata-print.pdf &

# Runestone HTML
#   Modeled on HTML
#   Not for production, just for local testing
runestone:
	install -d $(RSOUT)
	$(MBSCRIPT)/pretext -vv -c all -f html -p $(PUB)/runestone.xml -d $(RSOUT) $(SRC)/aata.xml

braille-emboss:
	install -d $(BRAILLEOUT)
	$(MBSCRIPT)/pretext -vv -c all -f braille-emboss -p $(PUB)/braille.xml -d $(BRAILLEOUT) $(SRC)/aata.xml

braille-electronic:
	install -d $(BRAILLEOUT)
	$(MBSCRIPT)/pretext -vv -c all -f braille-electronic -p $(PUB)/braille.xml -d $(BRAILLEOUT) $(SRC)/aata.xml


######
# Sage
######

# AATA has extensive support for Sage
# These targets are all related to that

# Doctest
#   All Sage material, but not solutions to exercises
#   Prepare location, remove *.py from previous runs
#   XSL dumps into current directory, Sage processes whole directory
#   chunk level 2 gives sections (commentaries, exercises)
doctest:
	-rm $(DOCTEST)/*.py; \
	install -d $(DOCTEST)
	cd $(DOCTEST); \
	xsltproc --xinclude --stringparam chunk.level 2 $(MBXSL)/pretext-sage-doctest.xsl $(SRC)/aata.xml; \
	$(SAGE) -tp 0 .

###########
# Utilities
###########

# Repetitive stanzas

# "Manually" copy extra files for compilation, as expected
#  by filenames generated by managed directories scheme

#  Moving latex-image SVG files includes a rename to
#  "generated" via a "\." construction, explained at
#  https://unix.stackexchange.com/questions/409225/
#  cp-behaves-weirdly-when-dot-or-dot-dot-are-the-source-directory
#  referenced from
#  https://unix.stackexchange.com/questions/412259/
#  how-can-i-copy-a-directory-and-rename-it-in-the-same-command
#  These become unnecessary once the pretext/pretext script is employed

place-jupyter-files:
	install -d $(JUPYTEROUT)/external $(JUPYTEROUT)/generated
	# /. forces copy to rename images as generated
	cp -a $(AATA)/images/. $(JUPYTEROUT)/generated
	cp -a $(AATA)/external $(JUPYTEROUT)

##############
# Experimental
##############

# These are in-progress, experimental, and/or totally broken

# via pretext/pretext, so no need to move any files, etc.
epub:
	install -d $(EPUBOUT)
	$(MBSCRIPT)/pretext -vv -c all -f epub-svg -p $(PUB)/epub.xml -d $(EPUBOUT) $(SRC)/aata.xml

# Jupyter Notebooks - experimental
jupyter: place-jupyter-files
	install -d $(JUPYTEROUT)
	cd $(JUPYTEROUT); \
	xsltproc --xinclude -stringparam publisher $(PUB)/public.xml $(MBXSL)/pretext-jupyter.xsl $(SRC)/aata.xml

# String parameter conveniences for Remakable target,
# despite being deprecated 2022-10. Could fork public.xml
# once some common material is modularized

# Designed to match a ReMarkable e-reader screen's shape
# CM Bright font is loaded *early* so as to not clobber the
#     latex.preaqmble.late  inside  aata-latex.xsl  in use
# ToC adjustment seems to be ineffective
TWELVE=latex.font.size 12pt
PAGEREMARKABLE=latex.geometry "paperwidth=5.750in, paperheight=7.666in, total={5.250in, 7.166in}" latex.preamble.early "\\usepackage{cmbright}" toc.level 1

# Like Sage-inclusive PDF, but with page geometry that matches the shape
# of a ReMarkable e-reader screen, see PAGEREMARKABLE defined above
remarkable:
	install -d $(MBUSR)
	cp $(XSL)/aata-latex.xsl $(MBUSR)
	install -d $(PDFOUT)
	$(MBSCRIPT)/pretext -vv -c all -f pdf -x $(TWELVE) $(PAGEREMARKABLE) -p $(PUB)/public.xml -X $(MBUSR)/aata-latex.xsl -d $(PDFOUT) $(SRC)/aata.xml
	mv $(PDFOUT)/aata.pdf $(PDFOUT)/aata-remarkable.pdf

