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

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

GHCFLAGS += -Wall -Werror -package util -I.
GHCFLAGS_SHA1.o = $(GHCFLAGS) -no-auto-all -funfolding-use-threshold=20

COMMON_FILES := AntiMemo.lhs Autoconf.lhs \
	CheckFileSystem.lhs ColourPrinter.lhs Curl.hs	\
	Pristine.lhs DarcsArguments.lhs DarcsFlags.lhs DarcsUtils.lhs	\
	CommandLine.lhs \
	DateMatcher.lhs Depends.lhs Diff.lhs Exec.lhs External.hs	\
	FastPackedString.hs FileName.lhs IsoDate.lhs Lcs.lhs Lock.lhs	\
	Match.lhs Motd.lhs Patch.lhs PatchApply.lhs PatchBundle.lhs	\
	PatchCheck.lhs PatchChoices.lhs PatchCommute.lhs PatchCore.lhs	\
	PatchInfo.lhs PatchMatch.lhs PatchMatchData.lhs			\
	PatchRead.lhs PatchShow.lhs PatchViewing.lhs	\
	Population.lhs PopulationData.lhs	\
	PrintPatch.lhs Printer.lhs RegChars.lhs RepoPrefs.lhs		\
	RepoTypes.lhs Repository.lhs Resolution.lhs SHA1.lhs		\
	SignalHandler.lhs SlurpDirectory.lhs Test.lhs UTF8.lhs		\
	Workaround.hs Zlib.lhs FileSystem.hs AtExit.lhs

DARCS_FILES := $(COMMON_FILES) Add.lhs AmendRecord.lhs	\
	Annotate.lhs Apply.lhs		\
	ArgumentDefaults.lhs Changes.lhs Check.lhs Context.lhs		\
	DarcsCommands.lhs DarcsURL.hs DiffCommand.lhs Dist.lhs		\
	Email.hs Get.lhs Init.lhs Mv.lhs Optimize.lhs Pull.lhs		\
	Push.lhs Record.lhs Remove.lhs Repair.lhs Replace.lhs		\
	Resolve.lhs Revert.lhs Rollback.lhs	\
	SelectChanges.lhs Send.lhs SetPref.lhs Tag.lhs TheCommands.lhs	\
	TouchesFiles.lhs TrackDown.lhs Unrecord.lhs Unrevert.lhs	\
	WhatsNew.lhs

UNIT_FILES := $(DARCS_FILES) PatchTest.lhs

CREATEREPO_FILES := DarcsUtils.lhs Workaround.hs \
		    Autoconf.lhs Printer.lhs FastPackedString.hs \
		    fpstring.o rts.o

C_OBJS := compat.o fpstring.o zlib_helper.o c_context.o

ifeq ($(SYS), windows)
GHCFLAGS += -iwin32 -Iwin32 -threaded -DOVERRIDE_RENAMEFILE -DWIN32
DARCS_FILES += win32/System/Posix.hs win32/CtrlC.hs
C_OBJS += win32/CtrlC_stub.o win32/CtrlC.o
UNIT_FILES += win32/System/Posix.hs
CREATEREPO_FILES += win32/System/Posix.hs
endif

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

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

ifeq ($(HAVE_LIBCURL),True)
C_OBJS += 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)))
CREATEREPO_OBJS := $(patsubst %.hs,%.o,$(patsubst %.lhs,%.o,$(CREATEREPO_FILES)))

.PHONY:		all clean distclean realclean dist deb test check install \
		installserver installdocs installbin windowsinstaller \
		website

config: configure autoconf.mk

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

	$(INSTALL) -d $(DESTDIR)/$(sysconfdir)/bash_completion.d/
	$(INSTALL_DATA) darcs_completion $(DESTDIR)/$(sysconfdir)/bash_completion.d/darcs

	$(INSTALL) -d $(DESTDIR)/$(mandir)/man1
	$(INSTALL_DATA) darcs.1 $(DESTDIR)/$(mandir)/man1/

installdocs:	darcs.ps manual/index.html
	$(INSTALL) -d $(DESTDIR)/$(datadir)/doc/darcs/manual
	$(INSTALL_DATA) darcs.ps $(DESTDIR)/$(datadir)/doc/darcs
	$(INSTALL_DATA) manual/* $(DESTDIR)/$(datadir)/doc/darcs/manual

	$(INSTALL) -d $(DESTDIR)/$(datadir)/doc/darcs/examples
	$(INSTALL_DATA) zsh_completion_new $(DESTDIR)/$(datadir)/doc/darcs/examples/
	$(INSTALL_DATA) zsh_completion_old $(DESTDIR)/$(datadir)/doc/darcs/examples/

installserver:	darcs-createrepo cgi/darcs.cgi
	$(INSTALL) -d $(DESTDIR)/$(libexecdir)/cgi-bin

	$(INSTALL) -d $(DESTDIR)/$(sbindir)
	$(INSTALL) darcs-createrepo $(DESTDIR)/$(sbindir)

	$(INSTALL) -d $(DESTDIR)/$(sysconfdir)/darcs
	test -e $(DESTDIR)/$(sysconfdir)/darcs/cgi.conf || \
	    $(INSTALL_DATA) cgi/cgi.conf $(DESTDIR)/$(sysconfdir)/darcs/cgi.conf
	$(INSTALL) cgi/darcs.cgi $(DESTDIR)/$(libexecdir)/cgi-bin/darcs.cgi
	$(INSTALL) -d $(DESTDIR)/$(datadir)/darcs
	$(INSTALL) -d $(DESTDIR)/$(datadir)/darcs/xslt
	$(INSTALL_DATA) cgi/xslt/*.xslt $(DESTDIR)/$(datadir)/darcs/xslt/
	$(INSTALL_DATA) 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 cgi/xslt/styles.css $(DESTDIR)/$(sysconfdir)/darcs/styles.css

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


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 manual/index.html darcs.nsi
	echo "!define VERSION $(DARCS_VERSION)" > version.nsi
	"$(MAKENSIS)" version.nsi darcs.nsi
	rm -f version.nsi

darcs:		$(DARCS_OBJS) darcs.lhs $(C_OBJS) 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) darcs.lhs $(C_OBJS)
.PHONY: ghci

darcsman:		darcsman.hs $(C_OBJS) $(DARCS_OBJS)
list_authors:	list_authors.hs $(C_OBJS) $(DARCS_OBJS)
make_changelog:	make_changelog.hs $(C_OBJS) $(DARCS_OBJS)
darcs-createrepo:	darcs-createrepo.lhs $(CREATEREPO_OBJS)
unit:			unit.lhs $(UNIT_OBJS) rts.o $(C_OBJS)
preproc:		GHCFLAGS += -package text
preproc:		preproc.hs $(C_OBJS) $(DARCS_OBJS)

TEXSOURCES := preproc darcs.lhs features.tex \
	switching.tex configuring_darcs.tex gpl.tex \
	darcs-createrepo.lhs $(DARCS_FILES) building_darcs.tex \
	best_practices.tex
darcs.tex: $(TEXSOURCES)
	./preproc darcs.lhs $(PREPROCHTML) > darcs.tex
darcs_print.tex: $(TEXSOURCES)
	./preproc darcs.lhs > darcs_print.tex

website: darcs-stable.tar.gz darcs.ps manual/index.html manual/bigpage.html darcs \
	index.html

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

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

manual/AntiMemo.html: AntiMemo.lhs
	latex2html -split 0 -no_auto_link -dir manual AntiMemo.lhs
	cp -f darcs.css manual/AntiMemo.css

index.html:	index.html.in config.status
	sed "s/VERSION/$(DARCS_VERSION)/g" $< > $@

darcs.ps:	darcs_print.ps
	cp darcs_print.ps darcs.ps

darcs.1:	darcsman
	./darcsman > darcs.1

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:    test_coding_standards test_unit test_perl test_shell

test_perl:     darcs
	@cd tests && perl perl_harness *.pl


test_unit:     darcs unit
	./unit

test_shell:    darcs
	@cd tests && perl shell_harness *.sh

test_coding_standards: test_coding_standards_haskell_without_tabs

test_coding_standards_haskell_without_tabs:
	@echo Checking that there are no TABs in Haskell sources
	@if grep '	' $(DARCS_FILES); then false; else true; fi

clean:
	rm -f *.o *.hi unit diff darcs darcs_cgi
	rm -f Context.hs stringify
	rm -f darcs.tex darcs.dvi darcs.aux
	rm -f darcs_print.tex darcs_print.log darcs_print.aux darcs_print.toc
	rm -f darcs-createrepo preproc darcsman
	rm -rf tests/temp* darcs.log darcs.toc
	rm -f conftest* .depend.bak
	rm -f win32/*.o win32/*.hi
	rm -f win32/System/*.o win32/System/*.hi
	rm -f version.nsi
	rm -f list_authors

distclean:	clean
	rm -rf config.status config.log autoconf.mk config.cache autom4te.cache
	rm -f darcs.ps darcs.pdf Autoconf.lhs Workaround.hs darcs.idv

realclean: distclean
	rm -f configure darcs.1 .depend AUTHORS ChangeLog c_context.c

configure:		configure.ac aclocal.m4
	autoconf

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

config.status:	configure
	if test -f config.command; then sh ./config.command; \
	else sh ./configure; fi

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

.depend:	autoconf.mk

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

