# Makefile for tex-refs
#

PROJECT=tex-refs
VERSION = 0.4.1
TITLE="Ref. for TeX and Friends"

DISTDIR = $(PROJECT)-$(VERSION)

SRC_FILES=$(PROJECT)-xinclude.xml $(PROJECT)-chapter*.xml $(PROJECT).ent 

DIST_FILES=$(SRC_FILES) GFDL README AUTHORS TODO RELEASE-NEWS THANKS  \
           $(PROJECT).css $(PROJECT)*.xsl $(PROJECT)-cover.jpg Makefile

OUT_FILES = $(PROJECT).html html $(PROJECT).css $(PROJECT).txt $(PROJECT).epub

CLEAN_FILES = $(PROJECT).html html $(PROJECT).txt $(PROJECT).epub

TAR = tar
GZIP = --best

# name and options to 'xsltproc'
XSLTPROC=xsltproc
XSLTPROC_OPTS=--nonet #--verbose

CHUNKDIR = html

TIDY=tidy
TIDY_OPTS=-mq --show-warnings false

# XSL file to generate non-chunked HTML
XSLFILE=$(PROJECT).xsl

# XSL file to generate chunked HTML
XSLCHUNKFILE=$(PROJECT)-chunk.xsl

# XSL file with common customizations
XSLCOMMONFILE=$(PROJECT)-common.xsl

DOCBOOK_XSL_DIR=$(HOME)/docbook-xsl
DBTOEPUB_CMD=$(DOCBOOK_XSL_DIR)/epub/bin/dbtoepub

EPUBCHECK_DIR=$(HOME)/work/epubcheck
EPUBCHECK_JAR=$(EPUBCHECK_DIR)/epubcheck-3.0-RC-1.jar


#####

%.txt : %.html
	@# w3m wraps words like '\something' at the '\'
	@# w3m -dump -cols 72 $< > $@
	lynx -nolist -dump -width=72 $< > $@
	@# discard 'Index', it makes no sense in TXT output
	sed -e '/^Index/,$$d' \
          $@ > x.tmp && mv x.tmp $@

#####

# default target
all: $(PROJECT).html chunk-html $(PROJECT).txt $(PROJECT).epub

# make master document
$(PROJECT).xml: $(SRC_FILES) Makefile 
	xmllint --nonet --output $@ --xinclude $(PROJECT)-xinclude.xml

# HTML related targets
$(PROJECT).html: $(PROJECT).xml $(XSLFILE) $(XSLCOMMONFILE)
	$(XSLTPROC) $(XSLTPROC_OPTS) --output $@ $(XSLFILE) $<
	-$(TIDY) $(TIDY_OPTS) $@

chunk-html: $(CHUNKDIR)/index.html

$(CHUNKDIR)/index.html : $(PROJECT).xml $(XSLCHUNKFILE) $(XSLCOMMONFILE)
	test -d $(CHUNKDIR) || mkdir $(CHUNKDIR) || exit 1
	$(RM) $(CHUNKDIR)/*
	$(XSLTPROC) -o $(CHUNKDIR)/ $(XSLCHUNKFILE) $<
	-(cd $(CHUNKDIR) && $(TIDY) $(TIDY_OPTS) *.html )

# EPUB
$(PROJECT).epub: $(PROJECT).xml Makefile
	$(RM) $(PROJECT).epub
	sed -e 's/ spacing="compact"//g' < $(PROJECT).xml > x.xml && cp x.xml     $(PROJECT).xml && rm x.xml
	$(DBTOEPUB_CMD) -s $(PROJECT)-epub.xsl -c $(PROJECT).css $(PROJECT).xml
	$(RM) $(PROJECT).xml

epubcheck:
	java -jar $(EPUBCHECK_JAR) $(PROJECT).epub
	ls -l $(PROJECT).epub

validate: $(PROJECT).xml Makefile
	xmllint --valid --nonet --noout $(PROJECT).xml

# ASCII
$(PROJECT).txt: $(PROJECT).html

# local "enhancements"
ifeq "$(wildcard Makefile.miwie)" "Makefile.miwie"
include Makefile.miwie
endif

targets:
	@echo "Most important targets:"
	@echo 
	@echo "all              generate all available output formats"
	@echo 
	@echo "validate         validate XML using xmllint"
	@echo "epubcheck        check epub file using epubcheck"
	@echo "$(PROJECT).html    generate $(PROJECT).html"
	@echo "chunk-html       generate chunked HTML output"
	@echo "$(PROJECT).txt     generate $(PROJECT).txt"
	@echo "$(PROJECT).epub    generate $(PROJECT).epub"
	@echo 
	@echo "clean            remove generated files"
	@echo "dist             create tarball with source files"


outfiles:
	-rm -fr $(PROJECT)-out-$(VERSION).tar.gz
	-rm -fr $(PROJECT)-out-$(VERSION).tar.bz2
	GZIP=$(GZIP) $(TAR) chozf $(PROJECT)-out-$(VERSION).tar.gz $(OUT_FILES)
	$(TAR) chojf $(PROJECT)-out-$(VERSION).tar.bz2 $(OUT_FILES)
	ls -l $(PROJECT)-out-$(VERSION).tar.gz
	ls -l $(PROJECT)-out-$(VERSION).tar.bz2

install:
	@echo "Nothing to install..."

uninstall: 
	@echo "Nothing to uninstall..."

clean:
	$(RM) -fr $(CLEAN_FILES)

distclean: clean

dist: distdir
	@-chmod -R a+r $(DISTDIR)
	@GZIP=$(GZIP) $(TAR) chozf $(DISTDIR).tar.gz $(DISTDIR)
	@$(TAR) chojf $(DISTDIR).tar.bz2 $(DISTDIR)
	ls -l $(DISTDIR).tar.gz
	ls -l $(DISTDIR).tar.bz2
	@-rm -rf $(DISTDIR)  

distdir: $(DIST_FILES)
	@-rm -rf $(DISTDIR)
	@mkdir $(DISTDIR)
	@-chmod 777 $(DISTDIR)
	@for file in $(DIST_FILES); do \
	  test -f $(DISTDIR)/$$file \
	  || ln ./$$file $(DISTDIR)/$$file 2> /dev/null \
	  || cp -p ./$$file $(DISTDIR)/$$file; \
	done       
