### Programme ###
# XSLT-Prozessor:
PROCESSOR=xsltproc
PFLAGS=--catalogs --stringparam baseurl $(BASEURL)
BASEURL=""
# XML-Validator
VALIDATOR=xmllint
# VALIDATOR-Flags für Eingabe-XML (meist c3d2web)
VFLAGS_IN=--noout --valid --catalogs
# VALIDATOR-Flags für Ausgabe-XML (xhtml, rss, atom, ...)
VFLAGS_OUT=$(VFLAGS_IN)
SGML_CATALOG_FILES=$(XML_CATALOG)
export SGML_CATALOG_FILES
# Downloadmanager (Alternativ: curl, fetch):
WGET=wget --timestamping
# rsync
RSYNC=rsync
RSYNC_FLAGS=--verbose --recursive --delete --delete-after --delete-excluded --checksum
# Editor
EDITOR?=vim
# Browser
BROWSER?=links

# Verzeichnis, in das exportiert werden soll
DESTDIR?=www-export

### Ressourcen ###
# XSLT-Stylesheets:
STYLE=xsl/xhtml.xsl
DS_STYLE=xsl/datenspuren/xhtml.xsl
# Document-Type-Definition:
DTD:=dtd/c3d2web.dtd
# XML-Katalog (Pfad zu den DTDs) - wird benötigt vom Validator
XML_CATALOG:=dtd/catalog.xml
# Navigationsleiste:
NAVIGATION=content/navigation.xml
# Chaosupdates:
WWW_CCC_DE_UPDATES=chaosupdates.rdf

### Ziele ###
# Dateinamen der zu erzeugenden html-Dateien:
CONTENT=$(PAGES) $(NEWS_PAGES) $(DS_PAGES) $(NEWSFILES) $(SEARCH_TMPL) build/calendar.html

# 'normale' Seiten:
PAGES:=$(patsubst content/pages/%.xml, build/%.html, $(wildcard content/pages/*.xml))
# Seiten zu einzelnen News-Items:
NEWS_PAGES:=$(patsubst content/news/%.xml, build/news/%.html, $(wildcard content/news/*.xml))
# News-Index und Feeds:
NEWSFILES:=build/news.html build/news-rss.xml build/news-atom.xml build/xcal.xml build/ical.ics
# Datenspuren-Seiten:
DS_PAGES:=$(patsubst content/datenspuren/%.xml, build/datenspuren/%.html, $(wildcard content/datenspuren/*.xml)) build/datenspuren/schedule.html
# Suchseiten-Template
SEARCH_TMPL:=build/cgi-bin/search.html

# Quellen
NEWSITEMS:=$(wildcard content/news/*.xml)
CLEAN=$(CONTENT) $(NEWSFILES) $(WWW_CCC_DE_UPDATES) $(DS_PAGES)

NOVALID_IGNORE:=false

define xml_process
	$(VALIDATOR) $(VFLAGS_IN) $<
	$(PROCESSOR) $(PFLAGS) -o $@ $(STYLE) $<
	# TODO: touch erklären!
	$(VALIDATOR) $(VFLAGS_OUT) $@ || $(NOVALID_IGNORE) || \
		{ touch -m -t 197001010000 Makefile $@ ; exit 1 ; }
endef


### Defaults ###

www: $(XML_CATALOG) $(CONTENT)
	$(RSYNC) $(RSYNC_FLAGS) content/static/ content/old-site/ build/ $@

export: www
	$(RSYNC) $(RSYNC_FLAGS) --exclude=".svn/" www/ $(DESTDIR)/

### Regeln für den XSLT-Prozess ###

build/news.html: $(WWW_CCC_DE_UPDATES)
build/news-rss.xml : STYLE=xsl/rss.xsl
build/news-rss.xml : NOVALID_IGNORE:=true
build/news-rss.xml : VFLAGS_OUT=--noout --schema xsd/rss20.xsd
build/news-atom.xml : STYLE=xsl/atom.xsl
build/news-atom.xml : NOVALID_IGNORE:=true
build/news-atom.xml : VFLAGS_OUT=--noout --relaxng xsd/atom.rng
build/xcal.xml : STYLE=xsl/xcal.xsl
build/ical.ics : STYLE=xsl/ical.xsl
build/ical.ics : NOVALID_IGNORE:=true
build/ical.ics : VFLAGS_OUT=--noout

#.INTERMEDIATE: news.xml
news.xml: $(NEWSITEMS)
	sh scripts/gen_news.xml.sh $^ > $@

$(NEWSFILES): news.xml $(NAVIGATION) $(STYLE)
	$(call xml_process)

# Calendar
calendar-summary.xml : STYLE=xsl/calendar-summary.xsl
calendar-summary.xml : NOVALID_IGNORE:=true
calendar-summary.xml : VFLAGS_IN=--noout
calendar-summary.xml: content/events.xml news.xml
	$(call xml_process)

build/calendar.html : VFLAGS_IN=--noout
build/calendar.html: calendar-summary.xml $(STYLE) $(NAVIGATION) $(WWW_CCC_DE_UPDATES)
	$(call xml_process)

# Pages:
build/%.html: content/pages/%.xml $(STYLE) $(NAVIGATION) $(WWW_CCC_DE_UPDATES) news.xml
	$(call xml_process)

# News:
build/news/%.html : BASEURL="../"
build/news/%.html: content/news/%.xml $(STYLE) $(NAVIGATION) $(WWW_CCC_DE_UPDATES)
	$(call xml_process)

# Suche:
build/cgi-bin/search.html : BASEURL="../"
build/cgi-bin/search.html: content/cgi-bin/search.xml $(STYLE) $(NAVIGATION)
	$(call xml_process)

# -- MNE-20060421: Lt. Astro geht Validierung noch nicht mit dem Fahrplan,
#                  daher fuer Datenspuren auskommentiert
build/datenspuren/%html : STYLE=$(DS_STYLE)
build/datenspuren/%html : NOVALID_IGNORE=true
build/datenspuren/%.html: content/datenspuren/%.xml $(DS_STYLE)
	$(call xml_process)

content/datenspuren/schedule.xml: content/static/datenspuren/fahrplan/days.de.html
	cp $< $@

### Die neuesten Chaosupdates holen ###
$(WWW_CCC_DE_UPDATES):
	$(WGET) http://www.ccc.de/updates/$(WWW_CCC_DE_UPDATES) || \
	cp chaosupdates-2006-01-02.rdf $@

### Mr. Propper ###
.PHONY: clean
clean:
	rm -f $(CLEAN) news.xml
