.PHONY:		test check \
		test_shell test_shell_hashed test_shell_format2 \
		test_perl test_perl_hashed test_perl_format2 \
		bugs bugs_shell bugs_shell_hashed bugs_shell_format2 \
		bugs_perl bugs_perl_hashed bugs_perl_format2 \
		slowtest

TEXSOURCES := preproc src/darcs.lhs src/features.tex \
	src/switching.tex src/configuring_darcs.tex src/gpl.tex \
	$(DARCS_FILES) src/building_darcs.tex \
	src/best_practices.tex src/formats.tex
src/darcs.tex: $(TEXSOURCES)
	./preproc darcs.lhs $(PREPROCHTML) > src/darcs.tex
# The following is to prevent both being generated simultaneously, which
# confuses texlive:
src/darcs_print.pdf: src/darcs_print.ps
src/darcs_print.tex: $(TEXSOURCES)
	./preproc darcs.lhs > src/darcs_print.tex
src/patch-theory.tex: $(TEXSOURCES) $(UNIT_FILES)
	./preproc Darcs/Patch/Properties.lhs > src/patch-theory.tex

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

doc/manual/index.html: src/darcs.tex src/gpl.tex doc/darcs.css
	$(MAKEMANUAL)
	cp -f doc/darcs.css doc/manual/darcs.css

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

doc/manual/darcs.ps:	src/darcs_print.ps
	cp src/darcs_print.ps doc/manual/darcs.ps
doc/manual/darcs.pdf:	src/darcs_print.pdf
	cp src/darcs_print.pdf doc/manual/darcs.pdf
doc/manual/patch-theory.pdf: src/patch-theory.pdf
	cp src/patch-theory.pdf doc/manual/

doc/darcs.1:	darcsman
	./darcsman > $@

AUTHORS:	list_authors
	./list_authors > AUTHORS

ChangeLog: make_changelog changelog.in/ChangeLog.old $(wildcard changelog.in/entries/*)
	( \
	  set -e; \
	  echo; \
	  rm -f changelog.in/entries/*~; \
	  ./make_changelog $(wildcard changelog.in/entries/*); \
	  echo; \
	  sed -n '/^darcs (1\.0\.2)$$/,$$p' changelog.in/ChangeLog.old; \
	) > $@.tmp
	mv $@.tmp $@

check test: tests

tests: \
	test_shell test_shell_hashed test_shell_format2 \
	test_perl test_perl_hashed test_perl_format2

bugs:	\
	bugs_shell bugs_shell_hashed bugs_shell_format2 \
	bugs_perl bugs_perl_hashed bugs_perl_format2

TEST_FILTER_FILE := tests_to_run
PERL_HARNESS := perl ../tests/bin/prove --nocolor
SHELL_HARNESS := perl ../tests/shell_harness

# $1 - description
# $2 - setup
# $3 - discard
# $4 - type
# $5 - extension
# $6 - harness
define harness
	@echo Running $4 tests on $1 repositories...
	@chmod -R u+rwx tests-$4-$1.dir || true
	@rm -rf tests-$4-$1.dir && cp -R tests tests-$4-$1.dir
	@cd tests-$4-$1.dir && rm -rf .darcs; mkdir .darcs;\
	 $2;\
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.$5" $(TEST_FILTER_FILE) | grep -v '^#' | $3 | xargs $6; \
	 else ls *.$5 | sort -r | $3 | xargs $6; fi
	@echo Done running tests on $1 repositories...
endef

define bug_harness
	@echo Demonstrating $4 bugs on $1 repositories...
	@rm -rf bugs-$4-$1.dir && cp -R bugs bugs-$4-$1.dir
	@cd bugs-$4-$1.dir && rm -rf .darcs; mkdir .darcs;\
	 $2;\
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then bugs=1 grep "\.$5" $(TEST_FILTER_FILE) | grep -v '^#' | $3 | xargs $6 || true; \
	 else bugs=1 ls *.$5 | sort -r | $3 | xargs $6 || true; fi
	@echo Done demonstrating bugs on $1 repositories...
endef

# $1 - description
# $2 - setup
# $3 - discard
shell_harness=$(call harness,$1,$2,$3,shell,sh,$(SHELL_HARNESS))
perl_harness=$(call harness,$1,$2,$3,perl,pl,$(PERL_HARNESS))
shell_bugs=$(call bug_harness,$1,$2,$3,shell,sh,$(SHELL_HARNESS))
perl_bugs=$(call bug_harness,$1,$2,$3,perl,pl,$(PERL_HARNESS))

test_network: darcs
	@echo Running network tests ...
	@rm -rf tests-network.dir && cp -R tests/network tests-network.dir
	@cd tests-network.dir && rm -rf .darcs; mkdir .darcs;\
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then bugs=1 grep "\.sh" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(SHELL_HARNESS); \
	 else bugs=1 ls *.sh | sort -r | xargs $(SHELL_HARNESS); fi
	@echo Passed network tests.

test_shell:     darcs
	$(call shell_harness,old-fashioned,:,cat)

# for the format2 tests, we skip the hashed_inventory.sh test, since explicitly
# creates the old-fashioned format that's incompatible with darcs-2.
test_shell_format2:     darcs
	$(call shell_harness,darcs-2,\
	 echo ALL --darcs-2 >> .darcs/defaults,\
	 grep -v hashed_inventory.sh)

test_shell_hashed:     darcs
	$(call shell_harness,hashed,\
	 echo ALL --hashed >> .darcs/defaults,cat)

# skip test known to fail on this repo format
test_perl:     darcs
	$(call perl_harness,old-fashioned,:,cat)

test_perl_format2:     darcs
	$(call perl_harness,darcs-2,\
	 echo ALL --darcs-2 >> .darcs/defaults,cat)

test_perl_hashed:     darcs
	$(call perl_harness,hashed,\
	 echo ALL --hashed >> .darcs/defaults,grep -v conflict-doppleganger.pl)

bugs_shell:     darcs
	$(call shell_bugs,old-fashioned,:,cat)

bugs_shell_format2:     darcs
	$(call shell_bugs,format-2,\
	 echo ALL --darcs-2 >> .darcs/defaults,\
	 grep -v hashed_inventory.sh)

bugs_shell_hashed:     darcs
	$(call shell_bugs,hashed,\
	 echo ALL --hashed >> .darcs/defaults,cat)

bugs_perl:     darcs
	$(call perl_bugs,old-fashioned,:,cat)

bugs_perl_format2:     darcs
	$(call perl_bugs,darcs-2,\
	 echo ALL --darcs-2 >> .darcs/defaults,cat)

bugs_perl_hashed:     darcs
	$(call perl_bugs,hashed,\
	 echo ALL --hashed >> .darcs/defaults,cat)

test_unit:     darcs unit
	./unit

ETAGS = hasktags -e
CTAGS = hasktags -c
# This follows what autoconf does.
# Fixme: Generate correct list of .c files and depend on that.
.PHONY: CTAGS ctags tags
tags: TAGS
TAGS: $(DARCS_FILES)
	$(ETAGS) $(DARCS_FILES) && etags -a src/*.c
ctags: CTAGS
CTAGS: $(DARCS_FILES)
	$(CTAGS) $(DARCS_FILES) && ctags -a src/*.c
	mv tags tags.unsorted
	sort tags.unsorted > tags

darcs: dist/build/darcs/darcs
	ln -s $(<) .
