# makefile
# "graphein" Tools
# See http://www.CircuitousRoot.com/graphein/ for documentation.
# Copyright 2006-2008, 2010 by David M. MacMillan and Rollande Krandall.
# Based on earlier work by David M. MacMillan that was copyright 2003-2005.
#
# This work 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 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 General Public License for more details.
#    
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.
# 59 Temple Place, Suite 330
# Boston, MA  02111-1307
# USA
# 
# Circuitous Root is a registered trademark of David M. MacMillan and 
# Rollande Krandall
# 
# Presented originally by Circuitous Root (R). http://www.CircuitousRoot.com/


# "P" is shorter than "PUBLIC_OR_PRIVATE"
#P=private
P=public

# Location of Xerces jars: xml-apis.jar xercesImpl.jar
XERCES=/usr/share/java
# # Location of Saxon-B release 8 jars (want "saxon8.jar")
#SAXONB=/usr/share/java/saxon-8.8
# # Location of Saxon-B release 9 jars (want "saxonb-9.0.jar")
SAXONB=/usr/share/java/
# # Location of XML Commons Resolver 1.1 jar
XMLCOMMONSRESOLVER=/usr/share/java

ROOTS=$(basename $(wildcard *.tei))
TEIS=$(patsubst %,%.tei,$(ROOTS))
LINTS=$(patsubst %,%.lint,$(ROOTS))
HTMLS=$(patsubst %,%.html,$(ROOTS))
FOS=$(patsubst %,%.fo,$(ROOTS))
PDFS=$(patsubst %,%.pdf,$(ROOTS))

MS=$(basename $(wildcard *.M))
MTXTS=$(patsubst %,%.M.txt,$(MS))

SUBDIRS= $(shell find . -mindepth 1 -maxdepth 1 -type d | grep -v -e "\.svn" -e notes -e image-development -e images-mastered-for-scaling -e private -e research -e new-static-files)

# Everything in new-static-files
STATIC_FILES=makefile makefile-mrproper \
             generate-dependencies.awk generate-dependencies.sh \
             graphein-tohtml.xsl graphein-tofo.xsl \
             main.css common.css \
             find-location.sh \
             current-depth-in-hierarchy.sh current-depth-in-category.sh \
             path-to-category-top.sh path-to-category-top.awk \
             path-to-current.sh path-to-current.awk \
             insert-img-dimensions.awk \
             xmllint-hack.awk \
             make-subdirectory.sh \
             gpl gfdl

# Only need note images at one SF; SF0 should always exist.
# Do not need to specify link-topic-sf?.jpg here.
LINKING_IMAGES=link-home-sf0.jpg link-category-sf0.jpg link-up-sf0.jpg 

all: lint html pdf mtxt subdirs

lint: $(LINTS)

html: $(HTMLS)

pdf:
#pdf: $(PDFS)

mtxt: $(MTXTS)

subdirs: 
	@for subdirectory in $(SUBDIRS); do \
	   $(call if-subdirectory-is-traversable,$$subdirectory); then \
	      cd $$subdirectory; \
	      $(call pull-static-file,makefile); \
              $(MAKE); \
	      cd ..; \
	   fi; \
	done

# this is really a macro, not a function call
if-subdirectory-is-traversable = \
	if [ $(1) != "./images-mastered-for-scaling" ] && \
	   [ $(1) != "./image-development" ] && \
           [ $(1) != "./new-static-files" ]  && \
           [ $(1) != "./notes" ]             && \
           [ $(1) != "./private" ]           && \
           [ $(1) != "./research" ]

$(LINTS): %.lint : %.tei $(STATIC_FILES) dependencies.dep
	rm -f temp1.linthack temp2-linthack
	xmllint --nonet --xinclude $*.tei > temp1-linthack
	awk -f xmllint-hack.awk temp1-linthack > temp2-linthack
	xmllint --nonet --noout --valid temp2-linthack
	rm -f temp1-linthack temp2-linthack
	if [ $$? -eq 0 ]; then touch $*.lint; fi


# Note: to add resolutions to be made, do, e.g.:
#       (for resolution in 0 1 2; ...
#
$(HTMLS): %.html : %.tei $(STATIC_FILES) $(LINKING_IMAGES) dependencies.dep images-scaled.dep images-scaled-private.dep
	(for resolution in 0 1; do \
	echo `pwd`/$*; \
	java \
	-Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl \
	-Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl \
	-Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XIncludeParserConfiguration \
	-classpath "$(XERCES)/xml-apis.jar:$(XERCES)/xercesImpl.jar:$(XMLCOMMONSRESOLVER)/xml-commons-resolver-1.1.jar:$(SAXONB)/saxonb-9.0.jar:/etc/java/resolver" \
	net.sf.saxon.Transform \
	-x org.apache.xml.resolver.tools.ResolvingXMLReader \
	-y org.apache.xml.resolver.tools.ResolvingXMLReader \
	-r org.apache.xml.resolver.tools.CatalogResolver \
	-u \
	$*.tei graphein-tohtml.xsl \
               own-basename=$* \
	       css-basename=main \
	       scale-factor=$$resolution \
               location=`./find-location.sh $*` \
               depth-in-hierarchy=`./current-depth-in-hierarchy.sh` \
               depth-in-category=`./current-depth-in-category.sh` \
               path-to-category-top=`./path-to-category-top.sh` \
               path-to-current=`./path-to-current.sh` \
	       public-or-private=$(P) > $*.html-tmp; \
	awk -f ./insert-img-dimensions.awk $*.html-tmp > $*-$$resolution.html; \
	rm -f $*.html-tmp; \
	if [ $$resolution -eq 0 ]; then \
	   rm -f $*.html; \
	   ln -s $*-0.html $*.html; \
	fi; \
	done )

$(PDFS): %.pdf : %.fo
	fop -fo $*.fo -pdf $*.pdf

$(FOS): %.fo : %.tei graphein-tofo.xsl static-files
	java \
	-classpath "/usr/share/java/xerces-j2.jar:/usr/share/saxon-6.5.3/saxon.jar:/usr/share/java/xml-commons-resolver-1.1.jar:/etc/java/resolver" \
	com.icl.saxon.StyleSheet \
	-x org.apache.xml.resolver.tools.ResolvingXMLReader \
	-y org.apache.xml.resolver.tools.ResolvingXMLReader \
	-r org.apache.xml.resolver.tools.CatalogResolver \
	-u \
	$*.tei graphein-tofo.xsl

$(MTXTS): %.M.txt : %.M
	cp $*.M $*.M.txt

# embed in ifeq to handle case where no *.tei-entity files exist
ifeq ($(shell [ -n "`ls *.tei-entityx 2> /dev/null`" ] && echo 1),1)
dependencies.dep: generate-dependencies.awk generate-dependencies.sh 
	chmod +x generate-dependencies.sh 
	./generate-dependencies.sh 2 > dependencies.dep
else
dependencies.dep: generate-dependencies.awk generate-dependencies.sh
	chmod +x generate-dependencies.sh 
	./generate-dependencies.sh 2 > dependencies.dep
endif

# STATIC_FILES
ifeq ($(shell [ -f new-static-files/makefile ] && echo 1),1)
makefile: new-static-files/makefile
	$(call pull-static-file,makefile)
endif
ifeq ($(shell [ -f ../makefile ] && echo 1),1)
makefile: ../makefile
	$(call pull-static-file,makefile)
endif

ifeq ($(shell [ -f new-static-files/makefile-mrproper ] && echo 1),1)
makefile-mrproper: new-static-files/makefile-mrproper
	$(call pull-static-file,makefile-mrproper)
endif
ifeq ($(shell [ -f ../makefile-mrproper ] && echo 1),1)
makefile-mrproper: ../makefile-mrproper
	$(call pull-static-file,makefile-mrproper)
endif

ifeq ($(shell [ -f new-static-files/generate-dependencies.awk ] && echo 1),1)
generate-dependencies.awk: new-static-files/generate-dependencies.awk
	$(call pull-static-file,generate-dependencies.awk)
endif
ifeq ($(shell [ -f ../generate-dependencies.awk ] && echo 1),1)
generate-dependencies.awk: ../generate-dependencies.awk
	$(call pull-static-file,generate-dependencies.awk)
endif

ifeq ($(shell [ -f new-static-files/generate-dependencies.sh ] && echo 1),1)
generate-dependencies.sh: new-static-files/generate-dependencies.sh
	$(call pull-static-file,generate-dependencies.sh)
	chmod +x generate-dependencies.sh
endif
ifeq ($(shell [ -f ../generate-dependencies.sh ] && echo 1),1)
generate-dependencies.sh: ../generate-dependencies.sh
	$(call pull-static-file,generate-dependencies.sh)
	chmod +x generate-dependencies.sh
endif


ifeq ($(shell [ -f new-static-files/graphein-tofo.xsl ] && echo 1),1)
graphein-tofo.xsl: new-static-files/graphein-tofo.xsl
	$(call pull-static-file,graphein-tofo.xsl)
endif
ifeq ($(shell [ -f ../graphein-tofo.xsl ] && echo 1),1)
graphein-tofo.xsl:  ../graphein-tofo.xsl
	$(call pull-static-file,graphein-tofo.xsl)
endif

ifeq ($(shell [ -f new-static-files/graphein-tohtml.xsl ] && echo 1),1)
graphein-tohtml.xsl: new-static-files/graphein-tohtml.xsl
	$(call pull-static-file,graphein-tohtml.xsl)
endif
ifeq ($(shell [ -f ../graphein-tohtml.xsl ] && echo 1),1)
graphein-tohtml.xsl:  ../graphein-tohtml.xsl
	$(call pull-static-file,graphein-tohtml.xsl)
endif

ifeq ($(shell [ -f new-static-files/main.css ] && echo 1),1)
main.css: new-static-files/main.css
	$(call pull-static-file,main.css)
endif
ifeq ($(shell [ -f ../main.css ] && echo 1),1)
main.css:  ../main.css
	$(call pull-static-file,main.css)
endif

ifeq ($(shell [ -f new-static-files/common.css ] && echo 1),1)
common.css: new-static-files/common.css
	$(call pull-static-file,common.css)
endif
ifeq ($(shell [ -f ../common.css ] && echo 1),1)
common.css:  ../common.css
	$(call pull-static-file,common.css)
endif

ifeq ($(shell [ -f new-static-files/find-location.sh ] && echo 1),1)
find-location.sh: new-static-files/find-location.sh
	$(call pull-static-file,find-location.sh)
	chmod +x find-location.sh
endif
ifeq ($(shell [ -f ../find-location.sh ] && echo 1),1)
find-location.sh:  ../find-location.sh
	$(call pull-static-file,find-location.sh)
	chmod +x find-location.sh
endif

ifeq ($(shell [ -f new-static-files/current-depth-in-hierarchy.sh ] && echo 1),1)
current-depth-in-hierarchy.sh: new-static-files/current-depth-in-hierarchy.sh
	$(call pull-static-file,current-depth-in-hierarchy.sh)
	chmod +x current-depth-in-hierarchy.sh
endif
ifeq ($(shell [ -f ../current-depth-in-hierarchy.sh ] && echo 1),1)
current-depth-in-hierarchy.sh:  ../current-depth-in-hierarchy.sh
	$(call pull-static-file,current-depth-in-hierarchy.sh)
	chmod +x current-depth-in-hierarchy.sh
endif

ifeq ($(shell [ -f new-static-files/current-depth-in-category.sh ] && echo 1),1)
current-depth-in-category.sh: new-static-files/current-depth-in-category.sh
	$(call pull-static-file,current-depth-in-category.sh)
	chmod +x current-depth-in-category.sh
endif
ifeq ($(shell [ -f ../current-depth-in-category.sh ] && echo 1),1)
current-depth-in-category.sh:  ../current-depth-in-category.sh
	$(call pull-static-file,current-depth-in-category.sh)
	chmod +x current-depth-in-category.sh
endif

ifeq ($(shell [ -f new-static-files/path-to-category-top.sh ] && echo 1),1)
path-to-category-top.sh: new-static-files/path-to-category-top.sh
	$(call pull-static-file,path-to-category-top.sh)
	chmod +x path-to-category-top.sh
endif
ifeq ($(shell [ -f ../path-to-category-top.sh ] && echo 1),1)
path-to-category-top.sh:  ../path-to-category-top.sh
	$(call pull-static-file,path-to-category-top.sh)
	chmod +x path-to-category-top.sh
endif

ifeq ($(shell [ -f new-static-files/path-to-category-top.awk ] && echo 1),1)
path-to-category-top.awk: new-static-files/path-to-category-top.awk
	$(call pull-static-file,path-to-category-top.awk)
	chmod +x path-to-category-top.awk
endif
ifeq ($(shell [ -f ../path-to-category-top.awk ] && echo 1),1)
path-to-category-top.awk:  ../path-to-category-top.awk
	$(call pull-static-file,path-to-category-top.awk)
	chmod +x path-to-category-top.awk
endif

ifeq ($(shell [ -f new-static-files/path-to-current.sh ] && echo 1),1)
path-to-current.sh: new-static-files/path-to-current.sh
	$(call pull-static-file,path-to-current.sh)
	chmod +x path-to-current.sh
endif
ifeq ($(shell [ -f ../path-to-current.sh ] && echo 1),1)
path-to-current.sh:  ../path-to-current.sh
	$(call pull-static-file,path-to-current.sh)
	chmod +x path-to-current.sh
endif

ifeq ($(shell [ -f new-static-files/path-to-current.awk ] && echo 1),1)
path-to-current.awk: new-static-files/path-to-current.awk
	$(call pull-static-file,path-to-current.awk)
	chmod +x path-to-current.awk
endif
ifeq ($(shell [ -f ../path-to-current.awk ] && echo 1),1)
path-to-current.awk:  ../path-to-current.awk
	$(call pull-static-file,path-to-current.awk)
	chmod +x path-to-current.awk
endif

ifeq ($(shell [ -f new-static-files/insert-img-dimensions.awk ] && echo 1),1)
insert-img-dimensions.awk: new-static-files/insert-img-dimensions.awk
	$(call pull-static-file,insert-img-dimensions.awk)
endif
ifeq ($(shell [ -f ../insert-img-dimensions.awk ] && echo 1),1)
insert-img-dimensions.awk:  ../insert-img-dimensions.awk
	$(call pull-static-file,insert-img-dimensions.awk)
endif

ifeq ($(shell [ -f new-static-files/xmllint-hack.awk ] && echo 1),1)
xmllint-hack.awk: new-static-files/xmllint-hack.awk
	$(call pull-static-file,xmllint-hack.awk)
endif
ifeq ($(shell [ -f ../xmllint-hack.awk ] && echo 1),1)
xmllint-hack.awk:  ../xmllint-hack.awk
	$(call pull-static-file,xmllint-hack.awk)
endif

ifeq ($(shell [ -f new-static-files/make-subdirectory.sh ] && echo 1),1)
make-subdirectory.sh: new-static-files/make-subdirectory.sh
	$(call pull-static-file,make-subdirectory.sh)
	chmod +x make-subdirectory.sh
endif
ifeq ($(shell [ -f ../make-subdirectory.sh ] && echo 1),1)
make-subdirectory.sh:  ../make-subdirectory.sh
	$(call pull-static-file,make-subdirectory.sh)
	chmod +x make-subdirectory.sh
endif

ifeq ($(shell [ -f new-static-files/gpl ] && echo 1),1)
gpl: new-static-files/gpl
	$(call pull-static-file,gpl)
endif
ifeq ($(shell [ -f ../gpl ] && echo 1),1)
gpl:  ../gpl
	$(call pull-static-file,gpl)
endif

ifeq ($(shell [ -f new-static-files/gfdl ] && echo 1),1)
gfdl: new-static-files/gfdl
	$(call pull-static-file,gfdl)
endif
ifeq ($(shell [ -f ../gfdl ] && echo 1),1)
gfdl:  ../gfdl
	$(call pull-static-file,gfdl)
endif



# If the specified file exists in the new-static-files subdirectory,
#    then check to see if that source is newer than the existing file
#    (which may or may not exist) in the current directory.  
#       If so, cp it over here. (that is, update to most current version)
# else if the specified file does not exist in new-static-files,
#    then check to see if the version here doesn't exist
#    or if the version in the parent directory is newer
#       If so, cp it down here
pull-static-file = \
	if [ -f ./new-static-files/$(1) ]; then \
	   if [ ./new-static-files/$(1) -nt $(1) ]; then \
	      cp -f ./new-static-files/$(1) .; \
	      chmod 444 $(1); \
	   fi; \
	else \
	   if [ ! -f $(1) ] || [ ../$(1) -nt $(1) ]; then \
	      cp -f ../$(1) .; \
	      chmod 444 $(1); \
	   fi; \
	fi


# If ../link-home-sf0.jpg exists, then we are NOT in the home directory
# and should generate a rule to pull link-home-sf?.jpg, if necessary
# otherwise we are in the home directory and they'll be made 
# in normal image scaling.
ifeq ($(shell [ -e ../link-home-sf0.jpg ] && echo 1),1)
link-home-sf0.jpg: ../link-home-sf0.jpg
	cp -f ../link-home-sf0.jpg link-home-sf0.jpg
	cp -f ../link-home-sf1.jpg link-home-sf1.jpg
	cp -f ../link-home-sf2.jpg link-home-sf2.jpg
else
link-home-sf0.jpg: 
endif

ifeq ($(shell [ -e ../new-link-category-sf0.jpg ] && echo 1),1)
link-category-sf0.jpg: ../new-link-category-sf0.jpg
	cp -f ../new-link-category-sf0.jpg link-category-sf0.jpg
	cp -f ../new-link-category-sf1.jpg link-category-sf1.jpg
	cp -f ../new-link-category-sf2.jpg link-category-sf2.jpg
else
ifeq ($(shell [ -e ../link-category-sf0.jpg ] && echo 1),1)
link-category-sf0.jpg: ../link-category-sf0.jpg
	cp -f ../link-category-sf0.jpg link-category-sf0.jpg
	cp -f ../link-category-sf1.jpg link-category-sf1.jpg
	cp -f ../link-category-sf2.jpg link-category-sf2.jpg
else
link-category-sf0.jpg: 
	touch link-category-sf0.jpg
	touch link-category-sf1.jpg
	touch link-category-sf2.jpg
endif
endif


# If ../link-topic-sf0.jpg exists, then we are NOT in the home directory
# and should generate a rule to pull link-topic-sf?.jpg, if necessary
# otherwise we are in the home directory and they shouldn't be made anyway.
# Note that it is necessary to "touch" a fake link-up-sf0.jpg file
# in the home directory.  Unlike the "home" image, for example, it will
# never exist (it isn't scaled from a local master), but if something
# with its name doesn't exist, make will have an unsatisfied dependency.
ifeq ($(shell [ -e ../link-topic-sf0.jpg ] && echo 1),1)
link-up-sf0.jpg: ../link-topic-sf0.jpg
	cp -f ../link-topic-sf0.jpg link-up-sf0.jpg
	cp -f ../link-topic-sf1.jpg link-up-sf1.jpg
	cp -f ../link-topic-sf2.jpg link-up-sf2.jpg
else
link-up-sf0.jpg: 
	touch link-up-sf0.jpg
	touch link-up-sf1.jpg
	touch link-up-sf2.jpg
endif


clean: 
	rm -f *.M.txt
	rm -f *.aux
	rm -f *.fo
	rm -f *.html
	rm -f *.lint
	rm -f *.log
	rm -f *.out
	rm -f *-sf?.jpg
	rm -f private/*-sf?.jpg
	rm -f dependencies.dep
	rm -f images-scaled.dep
	rm -f images-scaled-private.dep
	for subdirectory in $(SUBDIRS); do \
	   $(call if-subdirectory-is-traversable,$$subdirectory); then \
	      cd $$subdirectory; \
	      $(call pull-static-file,makefile); \
	      $(MAKE) clean; \
	      cd .. ; \
	   fi; \
	done

veryclean: clean
	rm -f makefile-mrproper
	rm -f generate-dependencies.sh
	rm -f generate-dependencies.awk
	rm -f find-location.sh
	rm -f graphein-tofo.xsl
	rm -f graphein-tohtml.xsl
	rm -f main.css
	rm -f common.css
	rm -f current-depth-in-hierarchy.sh
	rm -f current-depth-in-category.sh
	rm -f path-to-category-top.sh
	rm -f path-to-category-top.awk
	rm -f path-to-current.sh
	rm -f path-to-current.awk
	rm -f insert-img-dimensions.awk
	rm -f xmllint-hack.awk
	rm -f make-subdirectory.sh
	rm -f gpl gfdl
	for subdirectory in $(SUBDIRS); do \
	   $(call if-subdirectory-is-traversable,$$subdirectory); then \
	      cd $$subdirectory; \
	      $(call pull-static-file,makefile); \
              $(MAKE) veryclean; \
	      cd ..; \
	   fi; \
	done

# This won't work:
# Once you've "made veryclean", the scripts to make dependencies.dep
# are gone, so its include fails.
# Use makfile-mrproper instead.
# mrproper: dependencies.dep veryclean
#	rm -f makefile
#	for subdirectory in $(SUBDIRS); do \
#	   $(call if-subdirectory-is-traversable,$$subdirectory); then \
#	      cd $$subdirectory; \
#	      $(call pull-static-file,makefile); \
#             $(MAKE) mrproper; \
#	      cd ..; \
#	   fi; \
#	done

# Use this target to do global updates of texts 
# (e.g., to change tm to reg tm).  It is the only target which uses the 
# TEIS variable.
# Normally keep this commented out; adapt as necessary.
#globalsed:
#	for teifile in $(TEIS); do \
#	    rm -f /tmp/fubar.tei; \
#	    cp $$teifile /tmp/fubar.tei ; \
#	    sed 's/Circuitous Root</Circuitous Root\xC2\xAE</' /tmp/fubar.tei > $$teifile; \
#	done
#	for subdirectory in $(SUBDIRS); do \
#	   $(call if-subdirectory-is-traversable,$$subdirectory); then \
#	      cd $$subdirectory; \
#	      $(call pull-static-file,makefile); \
#	      $(MAKE) globalsed; \
#	      cd .. ; \
#	   fi; \
#	done

include ./dependencies.dep

