# The Gnutenberg Press - Makefile
# Copyright (C) 2003  Marcello Perathoner
# 
# This program 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.
#

include include.mk

all:	zips catalog web

.PRECIOUS:	intermediate/%.nroff intermediate/%.hdr intermediate/%.pdt \
		intermediate/%.teipre out/zip/%.zip out/zip/%-html.zip out/zip/%-pdf.zip \
		out/zip/%-pdb.zip out/zip/%-txt.zip out/zip/%-tei.zip 

TEIS      := $(wildcard tei/*.tei)

HTMLS     = $(TEIS:tei/%.tei=out/html/%.html)
PDFS      = $(TEIS:tei/%.tei=out/pdf/%.pdf)
TXTS      = $(TEIS:tei/%.tei=out/txt/%.txt)
PDBS      = $(TEIS:tei/%.tei=out/pdb/%.pdb)

TEXS      = $(TEIS:tei/%.tei=intermediate/%.tex)
DVIS      = $(TEIS:tei/%.tei=intermediate/%.dvi) 
NROFFS    = $(TEIS:tei/%.tei=intermediate/%.nroff)
PDTS      = $(TEIS:tei/%.tei=intermediate/%.pdt)
DIFFS     = $(TEIS:tei/%.tei=intermediate/%.diff1) \
	    $(TEIS:tei/%.tei=intermediate/%.diff2)
FORS      = $(TEIS:tei/%.tei=intermediate/%.for)
IMGLS     = $(TEIS:tei/%.tei=intermediate/%.imgl)
HTMLZIPS  = $(TEIS:tei/%.tei=out/zip/%-html.zip)
PDFZIPS   = $(TEIS:tei/%.tei=out/zip/%-pdf.zip)
PDBZIPS   = $(TEIS:tei/%.tei=out/zip/%-pdb.zip)
TXTZIPS   = $(TEIS:tei/%.tei=out/zip/%-txt.zip)
TEIZIPS   = $(TEIS:tei/%.tei=out/zip/%-tei.zip)

WEBPATH   = ~/prj/gutenberg/www.gnutenberg.de/web

vpath %.tei	tei catalog
vpath %.html    out/html
vpath %.pdf     out/pdf
vpath %.pdb     out/pdb
vpath %.txt     out/txt
vpath %.gut     src/gutenberg
vpath %.zip     out/zip

zips:   htmlzip pdfzip pdbzip txtzip teizip

%.all:  out/zip/%.zip out/zip/%-html.zip out/zip/%-pdf.zip \
	out/zip/%-pdb.zip out/zip/%-txt.zip out/zip/%-tei.zip 
	/bin/true

%.upload:  %.all catalog
	scp -p out/zip/$*-*.zip root@gnutenberg.de:config/htdocs/www.gnutenberg.de/download/
	scp -p catalog/html/$*.html catalog/html/catalog.html \
	root@gnutenberg.de:config/webapps/gnutenberg/catalog/
	/bin/true

htmlzip:$(HTMLZIPS)
pdfzip: $(PDFZIPS)
pdbzip: $(PDBZIPS)
txtzip: $(TXTZIPS)
teizip: $(TEIZIPS)

html:	$(HTMLS)
imgls:  $(IMGLS)
tex:    $(TEXS)
dvi:    $(DVIS)
pdf:	$(PDFS)
txt:    $(TXTS)
pdb:    $(PDBS)

for:    $(FORS)
	sort -d -t \| -k 1,1 -k 3 -k 2,2 -u $(FORS) > out/foreign-words
diff:	$(DIFFS)
dist:	
	$(TAR) -czvf out/zip/gnutenberg-press-0.0.2.tgz --exclude='*~' \
	--owner=root --group=root \
	--no-recursion COPYING $(sort \
	out out/html out/pdf out/txt out/pdb out/zip \
	intermediate tei dtd dtd/tei2 nroffdev nroffdev/devpdb patches \
	gut2tei.pl de-gnutenberg-press-1.0-persistent.css \
	Makefile stupid.mk gutenberg.mk \
	$(PREDEPS) $(HTMLDEPS) $(TEXDEPS) $(NROFFDEPS) \
	$(wildcard dtd/pgtei* dtd/tei2/* nroffdev/devpdb/* pgtei-*.xml) \
	patches/txt2pdbdoc-1.4.2.diff)

clean:
	rm intermediate/* out/html/* out/pdf/* out/txt/* out/pdb/*
hyphenated-words: diff
	./wordsplit.pl *.diff1 | grep "[[:alpha:]]\+-[[:alpha:]]\+" \
	| sort -f -u > out/hyphenated-words

# some dependencies

PREDEPS   = teipreprocessor.xsl gut-header.tei gut-footer.tei \
	    dtd/pgtei.ent dtd/pgtei2.ent 
HTMLDEPS  = tei2html.xsl tei2html-common.xsl tei2common.xsl tei2imgl.xsl \
	    dtd/pgtei.ent tei2common.pl tei2html.pl \
	    out/html/de-gnutenberg-press-1.0-persistent.css
TEXDEPS   = tei2tex.xsl tei2tex-common.xsl tei2common.xsl \
	    dtd/pgtei.ent tei2common.pl tei2tex.pl
NROFFDEPS = tei2nroff.xsl tei2nroff-common.xsl tei2common.xsl \
	    dtd/pgtei.ent tei2common.pl tei2nroff.pl

# zipped files ready for uploading

out/zip/%.zip : %.html %.pdf %.pdb %.txt %.tei intermediate/%.imgl
	-rm "$@"
	$(ZIP) -j "$@" $^ "out/html/de-gnutenberg-press-1.0-persistent.css"
	cd tei ; $(ZIP) "../$@" -@ < ../intermediate/$*.imgl
	$(ZIP) -d -j "$@" intermediate/$*.imgl

out/zip/%-html.zip : %.html intermediate/%.imgl
	-rm "$@"
	$(ZIP) -j "$@" "$<" "out/html/de-gnutenberg-press-1.0-persistent.css"
	cd tei ; $(ZIP) "../$@" -@ < ../intermediate/$*.imgl

out/zip/%-pdf.zip : %.pdf
	-rm "$@"
	$(ZIP) -j "$@" "$<"

out/zip/%-pdb.zip : %.pdb
	-rm "$@"
	$(ZIP) -j "$@" "$<"

out/zip/%-txt.zip : %.txt
	-rm "$@"
	$(ZIP) -j "$@" "$<"

out/zip/%-tei.zip : %.tei intermediate/%.imgl
	-rm "$@"
	$(ZIP) -j "$@" "$<"
	cd tei ; $(ZIP) "../$@" -@ < ../intermediate/$*.imgl

# output formats

out/html/%.html : intermediate/%.teipre $(HTMLDEPS) 
	$(XSLT) tei2html.xsl "$<" | ./tei2html.pl > "$@"

out/html/de-gnutenberg-press-1.0-persistent.css : de-gnutenberg-press-1.0-persistent.css 
	cp "$<" "$@"

out/pdf/%.pdf : intermediate/%.tex
	cd intermediate ; $(PDFLATEX) "$*.tex" ; rm "$*.pdf"; \
	$(PDFLATEX) "$*.tex" ; mv "$*.pdf" "../$@"

out/txt/%.txt : intermediate/%.nroff
	$(NROFF) -Tlatin1 "$<" > "$@"

out/pdb/%.pdb : intermediate/%.pdt $(PDBC)
	$(PDBC) "$<" "$@"

%.valid : %.tei
	@$(XMLLINT) "$<"

 # intermediate formats

intermediate/%.teipre : %.tei $(PREDEPS)
	$(XSLT) teipreprocessor.xsl "$<" > "$@"

intermediate/%.tex : intermediate/%.teipre $(TEXDEPS)
	$(XSLT) tei2tex.xsl "$<" | ./tei2tex.pl > "$@"

intermediate/%.nroff : intermediate/%.teipre $(NROFFDEPS)
	$(XSLT) tei2nroff.xsl "$<" | ./tei2nroff.pl > "$@" 

intermediate/%.pdt : intermediate/%.nroff
	$(NROFF) -Fnroffdev -Tpdb -rd2 "$<" > "$@"

intermediate/%.imgl : %.tei tei2imgl.xsl
	$(XSLT) tei2imgl.xsl "$<" > "$@"

# formats for debugging, proofing etc.

intermediate/%.dvi : intermediate/%.tex
	cd intermediate ; latex "$*"

intermediate/%.for : %.tei tei2foreign.xsl
	$(XSLT) tei2foreign.xsl "$<" > "$@"

intermediate/%.diff1 : intermediate/%.nroff
	$(NROFF) -Tlatin1 -rd1 "$<" > "$@"

intermediate/%.diff2 : %.gut gut2diff.pl
	./gut2diff.pl "$<" > "$@"

#####################################################
# the catalog on www.gnutenberg.de

HTMLCATS  = $(TEIS:tei/%.tei=catalog/html/%.html)
CATS      = $(TEIS:tei/%.tei=intermediate/%.cat)
HDRS      = $(TEIS:tei/%.tei=intermediate/%.hdr)

catalog: $(HTMLCATS) out/html/catalog.html out/pdf/catalog.pdf \
	 out/txt/catalog.txt out/pdb/catalog.pdb \
	 catalog/html/catalog.html catalog/catalog.sql

# the human readable catalog

intermediate/%.hdr : %.tei tei2hdr.xsl
	$(XSLT) tei2hdr.xsl "$<" > "$@"

catalog/catalog.tei : $(HDRS) hdr2hdr-tei.xsl \
		      catalog-head.tei catalog-foot.tei
	echo "<TEI.2>" > catalog/all-hdrs.tei
	cat $(HDRS) >> catalog/all-hdrs.tei
	echo "</TEI.2>" >> catalog/all-hdrs.tei
	cat catalog-head.tei > "$@"
	$(XSLT) hdr2hdr-tei.xsl catalog/all-hdrs.tei >> "$@"
	cat catalog-foot.tei >> "$@"

catalog/html/catalog.html : catalog/catalog.tei $(HTMLDEPS)
	$(XSLT) --stringparam elemid cat \
	tei2webextract.xsl "$<" | ./tei2html.pl > "$@"

# the catalog pages for the web

catalog/html/%.html : %.tei $(HTMLDEPS) tei2catalog.xsl
	$(XSLT) tei2catalog.xsl "$<" | ./tei2html.pl > "$@"

# the sql catalog update script

intermediate/%.cat : intermediate/%.hdr tei2cat.xsl
	$(XSLT) tei2cat.xsl "$<" > "$@"

catalog/catalog.sql: $(CATS) cat2sql.pl
	./cat2sql.pl intermediate/*.cat > "$@"

# the html files for the web page extracted from pg-guide.tei

web:	$(WEBPATH)/help/manifest.html \
	$(WEBPATH)/help/faq.html \
	$(WEBPATH)/help/software.html \
	$(WEBPATH)/de-gnutenberg-press-1.0-persistent.css \
	$(WEBPATH)/help/process.png

$(WEBPATH)/help/manifest.html : tei/pg-guide.tei $(HTMLDEPS) tei2webextract.xsl
	$(XSLT) --stringparam elemid manifest  \
	tei2webextract.xsl "$<" | ./tei2html.pl > "$@"

$(WEBPATH)/help/faq.html : tei/pg-guide.tei $(HTMLDEPS) tei2webextract.xsl
	$(XSLT) --stringparam elemid faq  \
	tei2webextract.xsl "$<" | ./tei2html.pl > "$@"

$(WEBPATH)/help/software.html : tei/pg-guide.tei $(HTMLDEPS) tei2webextract.xsl
	$(XSLT) --stringparam elemid software  \
	tei2webextract.xsl "$<" | ./tei2html.pl > "$@"

$(WEBPATH)/de-gnutenberg-press-1.0-persistent.css : de-gnutenberg-press-1.0-persistent.css 
	cp "$<" "$@"

$(WEBPATH)/help/process.png: tei/images/process.png
	cp "$<" "$@"
