### XXX we eventually want building of the manual to be part of Setup.lhs

# hardcode some bits
RUBBER=rubber

PREPROC=./dist/build/darcs/darcs --preprocess-manual
TEXSOURCES = src/darcs.tex $(wildcard src/*.tex) $(filter %.lhs,$(DARCS_FILES))
doc/manual/darcs.tex: $(TEXSOURCES)
	$(PREPROC) darcs.tex $(PREPROCHTML) >$@
doc/manual/darcs_print.tex: $(TEXSOURCES)
	$(PREPROC) darcs.tex >$@
doc/manual/patch-theory.tex: $(TEXSOURCES) $(UNIT_FILES)
	$(PREPROC) Darcs/Patch/Properties.lhs >$@

%.pdf: %.tex
	cd $(<D) && $(RUBBER) --pdf $(<F)
%.ps: %.tex
	cd $(<D) && $(RUBBER) --ps $(<F)

ps pdf: %: doc/manual/darcs.% doc/manual/patch-theory.%
html: doc/manual/index.html
website: ps pdf html doc/manual/bigpage.html

### TODO use latex2html since bigpage seems to needslatex2html anyway;
###      we can restore hevea/tex4ht support when we move this to Setup.lhs
doc/manual/index.html: doc/manual/darcs.tex src/gpl.tex doc/darcs.css
	latex2html -split +1 -dir doc/manual doc/manual/darcs.tex
	cp -f doc/darcs.css doc/manual/darcs.css

doc/manual/bigpage.html: doc/manual/darcs.tex src/gpl.tex doc/darcs.css
	ln -sf darcs.tex doc/manual/bigpage.tex
	latex2html -split 0 -external_file darcs -prefix big \
		-no_auto_link -dir doc/manual doc/manual/bigpage.tex
	cp -f doc/darcs.css doc/manual/bigpage.css

doc/manual/darcs.ps: doc/manual/darcs_print.ps
	cp $< $@
doc/manual/darcs.pdf: doc/manual/darcs_print.pdf
	cp $< $@

# Good for tags.
DARCS_FILES = $(wildcard src/[A-Z]*.hs src/*/[A-Z]*.hs src/*/*/[A-Z]*.hs) \
	      $(wildcard src/[A-Z]*.lhs src/*/[A-Z]*.lhs src/*/*/[A-Z]*.lhs)

# Always generate both tags and TAGS when the user asks for either,
# because lispy doesn't like using the shift key.
tags TAGS: $(DARCS_FILES) src/*.c
	hasktags -b $(filter %.lhs %.hs,$^)
	ctags -a $(filter %.c,$^)
# The test part tells make not to care if etags isn't installed, while
# still caring about etags crashing.  This is a Good Thing because you
# probably won't have etags installed unless you're an Emacs user.
	etags -a $(filter %.c,$^) || test $$? -eq 126 -o $$? -eq 127

clean: distclean
	rm -f TAGS tags
	rm -f doc/manual/bigpage.tex
	rm -f doc/manual/*.html doc/manual/darcs*.??? doc/manual/darcs.lg
	rm -f doc/manual/darcs.xref c_context.c doc/darcs_print.ps

.PHONY: ps pdf html clean website
