#
# Build darcs
#
ifeq (,$(findstring clean,$(MAKECMDGOALS)))
ifneq (config,$(MAKECMDGOALS))
include autoconf.mk
endif
endif

ifneq (,$(RTSFLAGS))
GHCFLAGS += +RTS $(RTSFLAGS) -RTS
endif

GHCFLAGS += -Wall -Werror -I. -I./src -i./src
GHCFLAGS_src__Darcs__Commands__Record.o = $(filter-out -auto-all,$(GHCFLAGS))
GHCFLAGS_src__Darcs__SelectChanges.o = $(filter-out -auto-all,$(GHCFLAGS))
GHCFLAGS_src__SHA1.o = $(GHCFLAGS) -no-auto-all -funfolding-use-threshold20
GHCFLAGS_src__Context.o = $(filter-out -cpp,$(GHCFLAGS))
ifeq ($(HAVE_CURSES),True)
GHCFLAGS_src__Darcs__External.o = $(GHCFLAGS)
ifeq ($(HAVE_TERMIO_H),True)
# This termio bit is a grim hack for Solaris 10 (at least).
# Without it, SGTTY isn't declared.  Something in the ghc C headers
# which get included before term.h in the generated C is clobbering
# it.  (If you edit that C and put the curses.h and term.h first, the
# error goes away.)
GHCFLAGS_src__Darcs__External.o += '-\#include <termio.h>'
endif
GHCFLAGS_src__Darcs__External.o += '-\#include <curses.h>' '-\#include <term.h>'
endif

SRC_DIRS:=. ./src ./src/Darcs\
		  ./src/Darcs/Commands\
		  ./src/Darcs/Repository\
		  ./src/Darcs/Patch\
		  ./src/win32\
		  ./src/win32/System\
		  ./src/win32/System/Posix\

MODULES_AUTOCONF :=\
	Autoconf.lhs \
	Context.hs \
        Workaround.hs \

MODULES_GENERAL :=\
	ThisVersion.lhs \
	CommandLine.lhs \
	Curl.hs DateMatcher.lhs \
	Exec.lhs FastPackedString.hs FileName.lhs FileSystem.hs IsoDate.lhs \
	Lcs.lhs Map.hs	\
	Printer.lhs	\
	RegChars.lhs \
	SHA1.lhs Stringalike.hs UTF8.lhs	\

MODULES_DARCS :=\
	ArgumentDefaults.lhs \
	Arguments.lhs Bug.lhs \
	CheckFileSystem.lhs \
	ColourPrinter.lhs 	\
	Commands.lhs CommandsAux.lhs Compat.hs	\
	Diff.lhs Email.hs FilePathUtils.hs \
	FilePathMonad.lhs	\
	External.hs	\
	Flags.lhs Global.lhs 	\
	Hopefully.lhs HopefullyPrivate.lhs \
	IO.lhs Lock.lhs Patch.lhs	\
	Match.lhs \
	Population.lhs PopulationData.lhs	\
	PrintPatch.lhs \
	RemoteApply.lhs	\
	Repository.lhs Resolution.lhs SignalHandler.lhs Test.lhs URL.hs \
	Sealed.lhs \
	SlurpDirectory.lhs \
	SelectChanges.lhs TheCommands.lhs	\
	Utils.lhs		\

MODULES_PATCH:=\
	Apply.lhs Bundle.lhs	\
	Commute.lhs Core.lhs	\
	Check.lhs Choices.lhs Conflicted.lhs ConflictedSet.lhs Conflictor.lhs \
	Depends.lhs Info.lhs Match.lhs MatchData.lhs	\
	Merge.lhs \
	Ordered.lhs \
	Patchy.lhs Permutations.lhs \
	Prim.lhs \
	Read.lhs ReadMonads.hs Set.lhs Show.lhs 	\
	TouchesFiles.lhs Viewing.lhs	\

MODULES_REPOSITORY:=\
	ApplyPatches.lhs \
	Checkpoint.lhs \
	HashedRepo.lhs \
	Format.lhs Internal.lhs \
	Motd.lhs Prefs.lhs		\
	Pristine.lhs \
	DarcsRepo.lhs	\

MODULES_COMMANDS:=\
	Add.lhs AmendRecord.lhs Annotate.lhs Apply.lhs		\
	Changes.lhs Check.lhs Diff.lhs Dist.lhs	\
	Get.lhs	Help.lhs Init.lhs MarkConflicts.lhs Mv.lhs Optimize.lhs 		\
	Pull.lhs Push.lhs Put.lhs	\
	Record.lhs Remove.lhs Repair.lhs Replace.lhs	\
	Revert.lhs Rollback.lhs	\
	Send.lhs SetPref.lhs \
	Show.lhs ShowContents.lhs ShowFiles.lhs ShowTags.lhs	\
	Tag.lhs TrackDown.lhs	\
	Unrecord.lhs Unrevert.lhs WhatsNew.lhs \

DARCS_FILES := $(DARCS_FILES_DEPS)\
	$(patsubst %,src/%,$(MODULES_AUTOCONF))\
	$(patsubst %,src/%,$(MODULES_GENERAL))\
	$(patsubst %,src/Darcs/%,$(MODULES_DARCS))\
	$(patsubst %,src/Darcs/Patch/%,$(MODULES_PATCH))\
	$(patsubst %,src/Darcs/Repository/%,$(MODULES_REPOSITORY))\
	$(patsubst %,src/Darcs/Commands/%,$(MODULES_COMMANDS))\
	$(GIT_SRCS)	\

UNIT_FILES := $(DARCS_FILES) src/Darcs/Patch/Test.lhs src/Darcs/Patch/QuickCheck.lhs src/Darcs/Patch/Unit.lhs

C_OBJS := $(patsubst %,src/%,c_compat.o maybe_relink.o atomic_create.o fpstring.o umask.o) \
          $(GIT_C_OBJS)

ifeq ($(SYS), windows)
GHCFLAGS += -isrc/win32 -Isrc/win32 -DWIN32
DARCS_FILES += src/win32/System/Posix.hs src/win32/System/Posix/Files.hs \
               src/win32/System/Posix/IO.hs src/win32/CtrlC.hs
C_OBJS += src/win32/CtrlC_stub.o src/win32/CtrlC.o
UNIT_FILES += src/win32/System/Posix.hs src/win32/System/Posix/Files.hs \
              src/win32/System/Posix/IO.hs
endif

ifeq ($(HAVE_CURSES),True)
GHCFLAGS += -DHAVE_CURSES
endif

ifeq ($(HAVE_MAPI),True)
C_OBJS += src/win32/send_email.o
CPPFLAGS += -Isrc/win32
GHCFLAGS += -DHAVE_MAPI
endif

ifeq ($(HAVE_LIBCURL),True)
C_OBJS += src/hscurl.o
GHCFLAGS += -DHAVE_CURL
endif

DARCS_OBJS	:= $(patsubst %.hs,%.o,$(patsubst %.lhs,%.o,$(DARCS_FILES)))
UNIT_OBJS	:= $(patsubst %.hs,%.o,$(patsubst %.lhs,%.o,$(UNIT_FILES)))


.PHONY:		all clean distclean maintainer-clean dist deb test check install \
		test_shell test_shell_hashed test_shell_both test_shell_format2 \
		test_perl test_perl_hashed test_perl_both test_perl_format2 \
		installserver installdocs installbin windowsinstaller \
		website slowtest

config: configure autoconf.mk

installbin:	all
	test -d $(DESTDIR)$(bindir) || $(INSTALL) -d $(DESTDIR)$(bindir)
	$(INSTALL) darcs $(DESTDIR)$(bindir)

	test -d $(DESTDIR)$(sysconfdir)/bash_completion.d || \
		$(INSTALL) -d $(DESTDIR)$(sysconfdir)/bash_completion.d
	$(INSTALL_DATA) tools/darcs_completion $(DESTDIR)$(sysconfdir)/bash_completion.d/darcs

	test -d $(DESTDIR)$(mandir)/man1 || \
		$(INSTALL) -d $(DESTDIR)$(mandir)/man1
	$(INSTALL_DATA) doc/darcs.1 $(DESTDIR)$(mandir)/man1/

installdocs:	doc/manual/darcs.ps doc/manual/index.html
	test -d $(DESTDIR)$(docdir)/manual || \
		$(INSTALL) -d $(DESTDIR)$(docdir)/manual
	$(INSTALL_DATA) doc/manual/* $(DESTDIR)$(docdir)/manual

	test -d $(DESTDIR)$(docdir)/examples || \
		$(INSTALL) -d $(DESTDIR)$(docdir)/examples
	$(INSTALL_DATA) tools/zsh_completion_new $(DESTDIR)$(docdir)/examples/
	$(INSTALL_DATA) tools/zsh_completion_old $(DESTDIR)$(docdir)/examples/

installserver:	tools/cgi/darcs.cgi
	test -d $(DESTDIR)$(libexecdir)/cgi-bin || \
		$(INSTALL) -d $(DESTDIR)$(libexecdir)/cgi-bin

	test -d $(DESTDIR)$(sysconfdir)/darcs || \
		$(INSTALL) -d $(DESTDIR)$(sysconfdir)/darcs
	test -e $(DESTDIR)$(sysconfdir)/darcs/cgi.conf || \
	    $(INSTALL_DATA) tools/cgi/cgi.conf $(DESTDIR)$(sysconfdir)/darcs/cgi.conf
	$(INSTALL) tools/cgi/darcs.cgi $(DESTDIR)$(libexecdir)/cgi-bin/darcs.cgi
	test -d $(DESTDIR)$(datadir)/darcs || \
		$(INSTALL) -d $(DESTDIR)$(datadir)/darcs
	test -d $(DESTDIR)$(datadir)/darcs/xslt || \
		$(INSTALL) -d $(DESTDIR)$(datadir)/darcs/xslt
	$(INSTALL_DATA) tools/cgi/xslt/*.xslt $(DESTDIR)$(datadir)/darcs/xslt/
	$(INSTALL_DATA) tools/cgi/xslt/styles.css $(DESTDIR)$(datadir)/darcs/xslt/styles.css

# Debian policy doesn't allow symlinks as configuration files.
#	test -e $(DESTDIR)$(sysconfdir)/darcs/styles.css || \
#	    ln -s $(datadir)/darcs/xslt/styles.css \
#		$(DESTDIR)$(sysconfdir)/darcs/styles.css
	$(INSTALL) -m 644 tools/cgi/xslt/styles.css $(DESTDIR)$(sysconfdir)/darcs/styles.css

PREDIST_COPY_FILES := \
	ChangeLog \
	AUTHORS \
	release/STATE \
	src/Context.hs

dist: darcs $(PREDIST_COPY_FILES)
	REPO_PATH="$$PWD" ./darcs dist --dist-name darcs-$(DARCS_VERSION)
	ln -sf darcs-$(DARCS_VERSION).tar.gz darcs-latest.tar.gz

predist_copy:
	for F in $(PREDIST_COPY_FILES); do \
	    cp -v "$$REPO_PATH/$$F" .; \
	done

predist: predist_copy doc/manual/darcs.ps doc/darcs.1 doc/manual/index.html distclean
	rm -fr release/debian

deb:		darcs
	rm -rf /tmp/darcs-deb_temp && \
	./darcs dist --dist-name darcs-deb_temp && \
	mv darcs-deb_temp.tar.gz /tmp && \
	cd /tmp && tar zxf darcs-deb_temp.tar.gz && \
	cd darcs-deb_temp && debuild && \
	rm -rf /tmp/darcs-deb_temp

windowsinstaller:	darcs doc/manual/index.html release/darcs.nsi
	echo "!define VERSION $(DARCS_VERSION)" > version.nsi
	"$(MAKENSIS)" version.nsi release/darcs.nsi
	rm -f version.nsi

darcs:		$(DARCS_OBJS) src/darcs.lhs $(C_OBJS) src/rts.o
	@echo Linking $@ ...
	@rm -f Main.o Main.hi
	@$(GHC) $(GHCFLAGS) $(OPTLLDFLAGS) -o $@ $^

ghci: $(DARCS_FILES) $(C_OBJS)
	ghci $(filter-out -Wall,$(GHCFLAGS)) $(OPTLLDFLAGS) src/darcs.lhs $(C_OBJS)
.PHONY: ghci

darcsman:		src/darcsman.hs $(C_OBJS) $(DARCS_OBJS)
list_authors:	src/list_authors.hs $(C_OBJS) $(DARCS_OBJS)
make_changelog:	src/make_changelog.hs $(C_OBJS) $(DARCS_OBJS)
unit:			src/unit.lhs $(UNIT_OBJS) src/rts.o $(C_OBJS)
preproc:		src/preproc.hs $(C_OBJS) $(DARCS_OBJS)

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/darcs.tex: $(TEXSOURCES)
	./preproc darcs.lhs $(PREPROCHTML) > src/darcs.tex
src/darcs_print.tex: $(TEXSOURCES)
	./preproc darcs.lhs > src/darcs_print.tex

website: darcs-stable.tar.gz doc/manual/darcs.ps 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/index.html:	doc/index.html.in config.status
	sed "s/VERSION/$(DARCS_VERSION)/g" $< > $@

doc/manual/darcs.ps:	src/darcs_print.ps
	cp src/darcs_print.ps doc/manual/darcs.ps

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; \
	  ./make_changelog $(wildcard changelog.in/entries/*); \
	  echo; \
	  sed -n '/^darcs (1\.0\.2)$$/,$$p' changelog.in/ChangeLog.old; \
	) > $@.tmp
	mv $@.tmp $@

test check:    unit test_unit \
		test_shell test_shell_hashed test_shell_both test_shell_format2 \
		test_perl test_perl_hashed test_perl_both test_perl_format2

TEST_FILTER_FILE := tests_to_run
PERL_HARNESS := perl perl_harness
SHELL_HARNESS := perl shell_harness

test_shell:     darcs
	@echo Running sh tests on old-fashioned reositories...
	@rm -rf test_shell.dir && cp -a tests test_shell.dir
	@cd test_shell.dir && rm -rf .darcs; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.sh" $(TEST_FILTER_FILE) | grep -v '^#' | exit 1 | xargs $(SHELL_HARNESS); \
	 else ls *.sh | sort -r | xargs $(SHELL_HARNESS); fi
	@echo Done running tests on old-fashioned repositories...

test_shell_format2:     darcs
	@echo Running tests on darcs-2 repositories...
	@rm -rf test_shell_format2.dir && cp -a tests test_shell_format2.dir
	@cd test_shell_format2.dir && \
	 rm -rf .darcs; \
	 mkdir .darcs; \
	 echo ALL --format-2 >> .darcs/defaults; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.sh" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(SHELL_HARNESS); \
	 else ls *.sh | sort -r | xargs $(SHELL_HARNESS); fi
	@echo Done running tests on darcs-2 repositories...

test_shell_hashed:     darcs
	@echo Running tests on hashed-inventory repositories...
	@rm -rf test_shell_hashed.dir && cp -a tests test_shell_hashed.dir
	@cd test_shell_hashed.dir && \
	 rm -rf .darcs; \
	 mkdir .darcs; \
	 echo ALL --hashed-inventory >> .darcs/defaults; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.sh" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(SHELL_HARNESS); \
	 else ls *.sh | sort | xargs $(SHELL_HARNESS); fi
	@echo Done running tests on hashed-inventory repositories...

test_shell_both:     darcs
	@echo Running tests on repositories supporting both formats...
	@rm -rf test_shell_both.dir && cp -a tests test_shell_both.dir
	@cd test_shell_both.dir && \
	 rm -rf .darcs; \
	 mkdir .darcs; \
	 echo ALL --hashed-inventory >> .darcs/defaults; \
	 echo ALL --old-fashioned-inventory >> .darcs/defaults; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.sh" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(SHELL_HARNESS); \
	 else ls *.sh | sort | xargs $(SHELL_HARNESS); fi
	@echo Done running tests on repositories supporting both formats...

test_perl:     darcs
	@echo Running sh tests on old-fashioned reositories...
	@rm -rf test_perl.dir && cp -a tests test_perl.dir
	@cd test_perl.dir && rm -rf .darcs; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.pl" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(PERL_HARNESS); \
	 else ls *.pl | sort -r | xargs $(PERL_HARNESS); fi
	@echo Done running tests on old-fashioned repositories...

test_perl_format2:     darcs
	@echo Running tests on darcs-2 repositories...
	@rm -rf test_perl_format2.dir && cp -a tests test_perl_format2.dir
	@cd test_perl_format2.dir && \
	 rm -rf .darcs; \
	 mkdir .darcs; \
	 echo ALL --format-2 >> .darcs/defaults; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.pl" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(PERL_HARNESS); \
	 else ls *.pl | sort -r | xargs $(PERL_HARNESS); fi
	@echo Done running tests on darcs-2 repositories...

test_perl_hashed:     darcs
	@echo Running tests on hashed-inventory repositories...
	@rm -rf test_perl_hashed.dir && cp -a tests test_perl_hashed.dir
	@cd test_perl_hashed.dir && \
	 rm -rf .darcs; \
	 mkdir .darcs; \
	 echo ALL --hashed-inventory >> .darcs/defaults; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.pl" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(PERL_HARNESS); \
	 else ls *.pl | sort | xargs $(PERL_HARNESS); fi
	@echo Done running tests on hashed-inventory repositories...

test_perl_both:     darcs
	@echo Running tests on repositories supporting both formats...
	@rm -rf test_perl_both.dir && cp -a tests test_perl_both.dir
	@cd test_perl_both.dir && \
	 rm -rf .darcs; \
	 mkdir .darcs; \
	 echo ALL --hashed-inventory >> .darcs/defaults; \
	 echo ALL --old-fashioned-inventory >> .darcs/defaults; \
	 if [ -r $(TEST_FILTER_FILE) ];\
	 then grep "\.pl" $(TEST_FILTER_FILE) | grep -v '^#' | xargs $(PERL_HARNESS); \
	 else ls *.pl | sort | xargs $(PERL_HARNESS); fi
	@echo Done running tests on repositories supporting both formats...

test_unit:     darcs unit
	./unit

clean:
	rm -f $(foreach dir,$(SRC_DIRS),$(dir)/*.o $(dir)/*.hi)
	rm -f unit diff darcs darcs_cgi
	rm -f stringify
	rm -f src/darcs.tex darcs.dvi darcs.aux
	rm -f src/darcs_print.tex darcs_print.log darcs_print.aux darcs_print.toc
	rm -f preproc darcsman
	rm -rf tests/temp* darcs.log darcs.toc
	rm -f conftest* .depend.bak
	rm -f src/win32/System/Posix/IO.hs src/win32/System/Posix/Files.hs
	rm -f version.nsi
	rm -f list_authors make_changelog
	#only remove src/Context.hs if we have a darcs inventory to rebuild it from
	if test -f _darcs/inventory; then rm -f src/Context.hs; fi
	rm -f TAGS tags

distclean:	clean
	rm -rf config.status config.log autoconf.mk config.cache autom4te.cache
	rm -f doc/manual/darcs.ps doc/manual/darcs.pdf src/Autoconf.lhs Workaround.hs darcs.idv
	rm -f src/ThisVersion.lhs tools/cgi/README tools/cgi/darcs.cgi tools/cgi/cgi.conf
	rm -f config.command

maintainer-clean: distclean
	@echo 'This command is intended for maintainers to use; it'
	@echo 'deletes files that may need special tools to rebuild.'
	rm -f configure doc/darcs.1 .depend AUTHORS ChangeLog
	rm -f src/Context.hs
	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
	rm -rf tests/test_output

configure:		configure.ac aclocal.m4
	autoconf

autoconf.mk src/Autoconf.lhs tools/cgi/darcs.cgi : \
   config.status autoconf.mk.in src/Autoconf.lhs.in tools/cgi/darcs.cgi.in
	./config.status

config.status:	configure
	@if test -x config.status; then					\
	  ./config.status --recheck;					\
	else								\
	  echo >&2 "Please run ./configure to setup the build system.";	\
	  exit 1;							\
	fi

.depend:	$(DARCS_FILES) $(UNIT_FILES)
	@echo Rebuild dependencies ...
	@$(GHC) $(GHCFLAGS) -M -optdep-f -optdep.depend $^

.depend:	autoconf.mk

ifeq (,$(findstring clean,$(MAKECMDGOALS)))
ifneq (config,$(MAKECMDGOALS))
include .depend
endif
endif

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
