## ********************************************************************* ##
## Copyright 2015-7                                                      ##
## Robert A. Beezer, Michael Gage, Geoff Goehle, Alex Jordan             ##
##                                                                       ##
## This file is part of PreTeXt.                                         ##
##                                                                       ##
## PreTeXt is free software: you can redistribute it and/or modify       ##
## it under the terms of the GNU General Public License as published by  ##
## the Free Software Foundation, either version 2 or version 3 of the    ##
## License (at your option).                                             ##
##                                                                       ##
## PreTeXt 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 General Public License for more details.                          ##
##                                                                       ##
## You should have received a copy of the GNU General Public License     ##
## along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.       ##
## ********************************************************************* ##

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

#   1) Make a copy of Makefile.paths.original
#      as Makefile.paths
#   2) Edit Makefile.paths as directed there, possibly
#      using Makefile.paths.example as an example
#   3) This file (Makefile), Makefile.paths.original,
#      and Makefile.paths.example are managed by version
#      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

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

#   install         (system tool to make directories)
#   jing-trang      (only for source validation)
#   <helpers>       (PDF viewer, web browser, pager, Sage executable, etc)

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

#	A) Change directory to the location of this file
#	B) At command line:  make <some-target>
#    where <some-target> is one of the recipes below
#    for example, sample-chapter-representations

# 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 PreTeXt distribution
PTXXSL = $(PTX)/xsl
WWEXAMPLE = $(PTX)/examples/webwork

# XML to apply templates to
SMPC = $(WWEXAMPLE)/sample-chapter/sample-chapter.ptx
MINI = $(WWEXAMPLE)/minimal/mini.xml

# Publisher files
SMPCPUB = $(WWEXAMPLE)/sample-chapter/publisher/publication.xml
SMPRSPUB = $(WWEXAMPLE)/sample-chapter/publisher/publication-academy.xml
MINIPUB = $(WWEXAMPLE)/minimal/publication.xml

# These paths are subdirectories of
# the scratch directory
TEXOUT     = $(SCRATCH)/latex
PDFOUT     = $(SCRATCH)/pdf
IMGOUT     = $(SCRATCH)/images
HTMLOUT    = $(SCRATCH)/html
RSOUT      = $(SCRATCH)/runestone
EPUBOUT    = $(SCRATCH)/epub
PGOUT      = $(SCRATCH)/pg


##################
# Targets for make
##################

# Build representations first
#   Then build PDF or Build HTML
# Build PG (does not need representations)

#########################################################################
# Verbose Output
# Applications of the pretext/pretext  script use extra-verbose output
# via the  -vv  switch so that temporary directories are preserved for
# testing purposes.This should not be taken as necessary or best
# practice for a real PreTeXt project using WeBWorK problems.


#########################################################################
#  Construct macros file. This must be uploaded into the macros folder
#  of the server's host course before building representations below.
#  There are no macros for the minimal example, so omitted

sample-chapter-macros:
	install -d $(PGOUT)/macros
	cd $(PGOUT)/macros; \
	$(PTX)/pretext/pretext -v -c pg-macros -p $(SMPCPUB) $(SMPC)


#########################################################################
#  Extract webwork problems into a single XML file.
#  Location is within generated folder (named in the publisher file),
#  within "webwork" subfolder

sample-chapter-representations:
	$(PTX)/pretext/pretext -v -c webwork -p $(SMPCPUB) $(SMPC)

mini-representations:
	$(PTX)/pretext/pretext -v -c webwork -p $(MINIPUB) $(MINI)


#########################################################################
# PDF versions

sample-chapter-pdf:
	-rm -r $(PDFOUT)
	install -d $(PDFOUT)
	$(PTX)/pretext/pretext -vv -c all -f pdf -p $(SMPCPUB) -d $(PDFOUT) $(SMPC)

mini-pdf:
	-rm -r $(PDFOUT)
	install -d $(PDFOUT)
	$(PTX)/pretext/pretext -vv -c all -f pdf -p $(MINIPUB) -d $(PDFOUT) $(MINI)


#########################################################################
# TeX versions
# use -x latex.fillin.style box for box answer blanks instead of underlines

sample-chapter-latex:
	-rm -r $(TEXOUT)
	install -d $(TEXOUT)
	$(PTX)/pretext/pretext -vv -c all -f latex -p $(SMPCPUB) -d $(TEXOUT) $(SMPC)

mini-latex:
	-rm -r $(TEXOUT)
	install -d $(TEXOUT)
	$(PTX)/pretext/pretext -vv -c all -f latex -p $(MINIPUB) -d $(TEXOUT) $(MINI)


#########################################################################
# HTML output
# Note that for the sample chapter, first we make the latex-image svg.
# We turn off dates in file headers, so when we use these for testing we
# can get more accurate diffs. Dates are useful for single file output
# (or at least less of an annoyance).  This should not be taken as necessary
# or best practice for a real PreTeXt project using WeBWorK problems.

sample-chapter-html:
	-rm -r $(WWEXAMPLE)/sample-chapter/generated/latex-image
	$(PTX)/pretext/pretext -c latex-image -f svg -p $(SMPCPUB) $(SMPC)
	-rm -r $(HTMLOUT)
	install -d $(HTMLOUT)
	$(PTX)/pretext/pretext -vv -c all -f html -p $(SMPCPUB) -x debug.datedfiles no -d $(HTMLOUT) $(SMPC); \

sample-chapter-runestone:
	-rm -r $(WWEXAMPLE)/sample-chapter/generated/latex-image
	$(PTX)/pretext/pretext -c latex-image -f svg -p $(SMPRSPUB) $(SMPC)
	-rm -r $(RSOUT)
	install -d $(RSOUT)
	$(PTX)/pretext/pretext -vv -c all -f html -p $(SMPRSPUB) -x debug.datedfiles no -d $(RSOUT) $(SMPC); \

mini-html:
	-rm -r $(HTMLOUT)
	install -d $(HTMLOUT)
	$(PTX)/pretext/pretext -vv -c all -f html -p $(MINIPUB) -x debug.datedfiles no -d $(HTMLOUT) $(MINI); \


#########################################################################
# PG output
# Make an archive of webwork problem sets for each "section" (as specified
# by chunk level) with set definition files, set header files, and problem
# files in a file tree. Separate problem sets for inline and divisional
# problems. This does not require having the representations file.
# We turn off dates in file headers, so when we use these for testing we
# can get more accurate diffs. Dates are useful for single file output
# (or at least less of an annoyance).  This should not be taken as necessary
# or best practice for a real PreTeXt project using WeBWorK problems.
# Adding a "-z" switch will create a "tgz" compressed archive

sample-chapter-problem-sets:
	install -d $(PGOUT)
	cd $(PGOUT); \
	rm -r Integrating_WeBWorK_into_Textbooks; \
	$(PTX)/pretext/pretext -v -c all -f webwork-sets -x chunk.level 1 debug.datedfiles no -p $(SMPCPUB) $(SMPC)

mini-problem-sets:
	install -d $(PGOUT)
	cd $(PGOUT); \
	rm -r WeBWorK_Minimal_Example; \
	$(PTX)/pretext/pretext -v -c all -f webwork-sets -x chunk.level 1 debug.datedfiles no -p $(MINIPUB) $(MINI)


#########################################################################
# Verify Source integrity
# Leaves "jingreport.txt" in SCRATCH
# Automatically invokes the "less" pager, could configure as $(PAGER)

sample-chapter-check:
	install -d $(SCRATCH)
	-rm $(SCRATCH)/jingreport.txt
	-java -classpath $(JINGTRANG) -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XIncludeParserConfiguration -jar $(JINGTRANG)/jing.jar $(PTX)/schema/pretext.rng $(SMPC) > $(SCRATCH)/jingreport.txt
	less $(SCRATCH)/jingreport.txt

webwork-mini-check:
	install -d $(SCRATCH)
	-rm $(SCRATCH)/jingreport.txt
	-java -classpath $(JINGTRANG) -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XIncludeParserConfiguration -jar $(JINGTRANG)/jing.jar $(PTX)/schema/pretext.rng $(MINI) > $(SCRATCH)/jingreport.txt
	less $(SCRATCH)/jingreport.txt


#########################################################################
#########################################################################

# DO NOT MODEL YOUR PROJECT AFTER VARIABLES AND RECIPES BELOW
# THESE ARE ONLY HERE FOR DEVELOPER TESTING

SMPCPUBNOMAN = $(WWEXAMPLE)/sample-chapter/publisher/pub-not-managed.xml
MINIPUBNOMAN = $(WWEXAMPLE)/minimal/pub-not-managed.xml
WWOUT        = $(SCRATCH)/webwork-representations

#########################################################################
# Extract webwork problems into a single XML file.
# Location is specified by -d option.

# DO NOT MODEL YOUR PROJECT AFTER THIS
sample-chapter-representations-not-managed:
	-rm -r $(WWOUT)
	install -d $(WWOUT)
	$(PTX)/pretext/pretext -vv -c webwork -p $(SMPCPUBNOMAN) -d $(WWOUT) $(SMPC)

# DO NOT MODEL YOUR PROJECT AFTER THIS
mini-representations-not-managed:
	-rm -r $(WWOUT)
	install -d $(WWOUT)
	$(PTX)/pretext/pretext -vv -c webwork -p $(MINIPUBNOMAN) -d $(WWOUT) $(MINI)


#########################################################################
# PDF output
# Note that webwork-representations file must be copied from where it was
# written (in scratch) to where the publisher file says it should be
# (which is relative to the main file). We then delete it to clean up.
# Note that pretext/pretext makes latex, not pdf. This is so we can make
# sure that images are available relative to the .tex file.

# DO NOT MODEL YOUR PROJECT AFTER THIS
sample-chapter-pdf-not-managed:
	-rm -r $(PDFOUT)
	install -d $(PDFOUT)
	install -d $(PDFOUT)/images
	cp -a $(WWOUT)/*.png $(PDFOUT)/images
	cp -a $(WWOUT)/*.pdf $(PDFOUT)/images
	cp $(WWOUT)/webwork-representations.xml $(WWEXAMPLE)/sample-chapter
	cd $(PDFOUT); \
	$(PTX)/pretext/pretext -vv -c all -f latex -p $(SMPCPUBNOMAN) -d $(PDFOUT) $(SMPC); \
	xelatex sample-chapter.tex; \
	xelatex sample-chapter.tex; \
	rm $(WWEXAMPLE)/sample-chapter/webwork-representations.xml; \

# DO NOT MODEL YOUR PROJECT AFTER THIS
mini-pdf-not-managed:
	-rm -r $(PDFOUT)
	install -d $(PDFOUT)
	cp $(WWOUT)/webwork-representations.xml $(WWEXAMPLE)/minimal
	cd $(PDFOUT); \
	$(PTX)/pretext/pretext -vv -c all -f pdf -p $(MINIPUBNOMAN) -d $(PDFOUT) $(MINI); \
	rm $(WWEXAMPLE)/minimal/webwork-representations.xml; \


#########################################################################
# HTML output
# Note that for the sample chapter, first we make the latex-image svg.
# We turn off dates in file headers, so when we use these for testing we
# can get more accurate diffs. Dates are useful for single file output
# (or at least less of an annoyance).  This should not be taken as necessary
# or best practice for a real PreTeXt project using WeBWorK problems.
# Note that webwork-representations file must be copied from where it was
# written (in scratch) to where the publisher file says it should be
# (which is relative to the main file). We then delete it to clean up.
# Note that pretext/pretext makes latex, not pdf. This is so we can make
# sure that images are available relative to the .tex file.

# DO NOT MODEL YOUR PROJECT AFTER THIS
sample-chapter-html-not-managed:
	-rm -r $(IMGOUT)
	install -d $(IMGOUT)
	cp $(WWOUT)/webwork-representations.xml $(WWEXAMPLE)/sample-chapter
	$(PTX)/pretext/pretext -vv -c latex-image -f svg -p $(SMPCPUBNOMAN) -d $(IMGOUT) $(SMPC)
	-rm -r $(HTMLOUT)
	install -d $(HTMLOUT)
	install -d $(HTMLOUT)/images
	cp -a $(WWEXAMPLE)/sample-chapter/external/images/webwork-logo.png $(HTMLOUT)/images
	cp -a $(WWOUT)/*.png $(HTMLOUT)/images
	cp -a $(WWOUT)/*.svg $(HTMLOUT)/images
	cp -a $(IMGOUT)/*.svg $(HTMLOUT)/images
	cd $(HTMLOUT); \
	$(PTX)/pretext/pretext -vv -c all -f html -p $(SMPCPUBNOMAN) -x webwork.divisional.static no -d $(HTMLOUT) $(SMPC); \
	rm $(WWEXAMPLE)/sample-chapter/webwork-representations.xml; \

# DO NOT MODEL YOUR PROJECT AFTER THIS
mini-html-not-managed:
	-rm -r $(HTMLOUT)
	install -d $(HTMLOUT)
	cp $(WWOUT)/webwork-representations.xml $(WWEXAMPLE)/minimal
	cd $(HTMLOUT); \
	$(PTX)/pretext/pretext -vv -c all -f html -p $(MINIPUBNOMAN) -d $(HTMLOUT) $(MINI); \
	rm $(WWEXAMPLE)/minimal/webwork-representations.xml; \
