# -*- makefile -*-

# Here's the (hopefully simple) versioning scheme.
#
# Releases of notmuch have a two-digit version (0.1, 0.2, etc.). We
# increment the second digit for each release and increment the first
# digit when we reach particularly major milestones of usability.
#
# Between releases, (such as when compiling notmuch from the git
# repository), we let git append identification of the actual commit.
PACKAGE=notmuch

IS_GIT=$(shell if [ -d .git ] ; then echo yes ; else echo no; fi)

ifeq ($(IS_GIT),yes)
DATE:=$(shell git log --date=short -1 --pretty=format:%cd)
else
DATE:=$(shell date +%F)
endif

VERSION:=$(shell cat ${srcdir}/version)
ifeq ($(filter release release-message pre-release update-versions,$(MAKECMDGOALS)),)
ifeq ($(IS_GIT),yes)
VERSION:=$(shell git describe --match '[0-9.]*'|sed -e s/_/~/ -e s/-/+/ -e s/-/~/)
endif
endif

UPSTREAM_TAG=$(subst ~,_,$(VERSION))
DEB_TAG=debian/$(UPSTREAM_TAG)-1

RELEASE_HOST=notmuchmail.org
RELEASE_DIR=/srv/notmuchmail.org/www/releases
RELEASE_URL=http://notmuchmail.org/releases
TAR_FILE=$(PACKAGE)-$(VERSION).tar.gz
DEB_TAR_FILE=$(PACKAGE)_$(VERSION).orig.tar.gz
SHA1_FILE=$(TAR_FILE).sha1
GPG_FILE=$(SHA1_FILE).asc

PV_FILE=bindings/python/notmuch/version.py

# Smash together user's values with our extra values
FINAL_CFLAGS = -DNOTMUCH_VERSION=$(VERSION) $(CFLAGS) $(WARN_CFLAGS) $(CONFIGURE_CFLAGS) $(extra_cflags)
FINAL_CXXFLAGS = $(CXXFLAGS) $(WARN_CXXFLAGS) $(CONFIGURE_CXXFLAGS) $(extra_cflags) $(extra_cxxflags)
FINAL_NOTMUCH_LDFLAGS = $(LDFLAGS) -Lutil -lutil -Llib -lnotmuch $(AS_NEEDED_LDFLAGS) $(GMIME_LDFLAGS) $(TALLOC_LDFLAGS)
FINAL_NOTMUCH_LINKER = CC
ifneq ($(LINKER_RESOLVES_LIBRARY_DEPENDENCIES),1)
FINAL_NOTMUCH_LDFLAGS += $(CONFIGURE_LDFLAGS)
FINAL_NOTMUCH_LINKER = CXX
endif
ifeq ($(LIBDIR_IN_LDCONFIG),0)
FINAL_NOTMUCH_LDFLAGS += $(RPATH_LDFLAGS)
endif
FINAL_LIBNOTMUCH_LDFLAGS = $(LDFLAGS) $(AS_NEEDED_LDFLAGS) $(CONFIGURE_LDFLAGS)

.PHONY: all
all: notmuch notmuch-shared
ifeq ($(MAKECMDGOALS),)
ifeq ($(shell cat .first-build-message 2>/dev/null),)
	@NOTMUCH_FIRST_BUILD=1 $(MAKE) --no-print-directory all
	@echo ""
	@echo "Compilation of notmuch is now complete. You can install notmuch with:"
	@echo ""
	@echo "	make install"
	@echo ""
	@echo "Note that depending on the prefix to which you are installing"
	@echo "you may need root permission (such as \"sudo make install\")."
	@echo "See \"./configure --help\" for help on setting an alternate prefix."
	@echo Printed > .first-build-message
endif
endif

$(TAR_FILE):
	if git tag -v $(VERSION) >/dev/null 2>&1; then \
           ref=$(VERSION); \
        else \
           ref="HEAD" ; \
	   echo "Warning: No signed tag for $(VERSION)"; \
	fi ; \
	git archive --format=tar --prefix=$(PACKAGE)-$(VERSION)/ $$ref > $(TAR_FILE).tmp
	echo $(VERSION) > version.tmp
	tar --append -f $(TAR_FILE).tmp --transform s_^_$(PACKAGE)-$(VERSION)/_  --transform 's_.tmp$$__' version.tmp
	rm version.tmp
	gzip < $(TAR_FILE).tmp > $(TAR_FILE)
	@echo "Source is ready for release in $(TAR_FILE)"

$(SHA1_FILE): $(TAR_FILE)
	sha1sum $^ > $@

$(GPG_FILE): $(SHA1_FILE)
	@echo "Please enter your GPG password to sign the checksum."
	gpg --armor --sign $^ 

.PHONY: dist
dist: $(TAR_FILE)

.PHONY: update-versions

update-versions: update-man-versions
	sed -i "s/^__VERSION__[[:blank:]]*=.*$$/__VERSION__ = \'${VERSION}\'/" $(PV_FILE)

# We invoke make recursively only to force ordering of our phony
# targets in the case of parallel invocation of make (-j).
#
# We carefully ensure that our VERSION variable is passed down to any
# sub-ordinate make invocations (which won't otherwise know that they
# are part of the release and need to take the version from the
# version file).
.PHONY: release
release: verify-source-tree-and-version
	$(MAKE) VERSION=$(VERSION) verify-newer
	$(MAKE) VERSION=$(VERSION) clean
	$(MAKE) VERSION=$(VERSION) test
	git tag -s -m "$(PACKAGE) $(VERSION) release" $(UPSTREAM_TAG)
	$(MAKE) VERSION=$(VERSION) $(GPG_FILE)
	ln -sf $(TAR_FILE) $(DEB_TAR_FILE)
	pristine-tar commit $(DEB_TAR_FILE) $(UPSTREAM_TAG)
	git tag -s -m "$(PACKAGE) Debian $(VERSION)-1 upload (same as $(VERSION))" $(DEB_TAG)
	mkdir -p releases
	mv $(TAR_FILE) $(SHA1_FILE) $(GPG_FILE) releases
	$(MAKE) VERSION=$(VERSION) release-message > $(PACKAGE)-$(VERSION).announce
ifeq ($(REALLY_UPLOAD),yes)
	git push origin $(VERSION)
	cd releases && scp $(TAR_FILE) $(SHA1_FILE) $(GPG_FILE) $(RELEASE_HOST):$(RELEASE_DIR)
	ssh $(RELEASE_HOST) "rm -f $(RELEASE_DIR)/LATEST-$(PACKAGE)-* ; ln -s $(TAR_FILE) $(RELEASE_DIR)/LATEST-$(TAR_FILE)"
endif
	@echo "Please send a release announcement using $(PACKAGE)-$(VERSION).announce as a template."

.PHONY: pre-release
pre-release:
	$(MAKE) VERSION=$(VERSION) clean
	$(MAKE) VERSION=$(VERSION) test
	git tag -s -m "$(PACKAGE) $(VERSION) release" $(UPSTREAM_TAG)
	git tag -s -m "$(PACKAGE) Debian $(VERSION)-1 upload (same as $(VERSION))" $(DEB_TAG)
	$(MAKE) VERSION=$(VERSION) $(TAR_FILE)
	ln -sf $(TAR_FILE) $(DEB_TAR_FILE)
	pristine-tar commit $(DEB_TAR_FILE) $(UPSTREAM_TAG)
	mkdir -p releases
	mv $(TAR_FILE) $(DEB_TAR_FILE) releases

.PHONY: debian-snapshot
debian-snapshot:
	make VERSION=$(VERSION) clean
	TMPFILE=$$(mktemp /tmp/notmuch.XXXXXX);		\
	  cp debian/changelog $${TMPFILE};		\
	  EDITOR=/bin/true dch -b -v $(VERSION)+1	\
	    -D UNRELEASED 'test build, not for upload';	\
	  echo '3.0 (native)' > debian/source/format; 	\
	  debuild -us -uc;				\
	  mv -f $${TMPFILE} debian/changelog;		\
	  echo '3.0 (quilt)' > debian/source/format

.PHONY: release-message
release-message:
	@echo "To: notmuch@notmuchmail.org"
	@echo "Subject: $(PACKAGE) release $(VERSION) now available"
	@echo ""
	@echo "Where to obtain notmuch $(VERSION)"
	@echo "==========================="
	@echo "  $(RELEASE_URL)/$(TAR_FILE)"
	@echo ""
	@echo "Which can be verified with:"
	@echo ""
	@echo "  $(RELEASE_URL)/$(SHA1_FILE)"
	@echo -n "  "
	@cat releases/$(SHA1_FILE)
	@echo ""
	@echo "  $(RELEASE_URL)/$(GPG_FILE)"
	@echo "  (signed by `getent passwd "$$USER" | cut -d: -f 5 | cut -d, -f 1`)"
	@echo ""
	@echo "What's new in notmuch $(VERSION)"
	@echo "========================="
	@sed -ne '/^[Nn]otmuch $(VERSION)/{n;n;b NEWS}; d; :NEWS /^===/q; {p;n;b NEWS}' < NEWS | head -n -2
	@echo ""
	@echo "What is notmuch"
	@echo "==============="
	@echo "Notmuch is a system for indexing, searching, reading, and tagging"
	@echo "large collections of email messages in maildir or mh format. It uses"
	@echo "the Xapian library to provide fast, full-text search with a convenient"
	@echo "search syntax."
	@echo ""
	@echo "For more about notmuch, see http://notmuchmail.org"

# This is a chain of dependencies rather than a simple list simply to
# avoid the messages getting interleaved in the case of a parallel
# make invocation.
.PHONY: verify-source-tree-and-version
verify-source-tree-and-version: verify-no-dirty-code

.PHONY: verify-no-dirty-code
verify-no-dirty-code: verify-version-debian verify-version-python verify-version-manpage
ifeq ($(IS_GIT),yes)
	@printf "Checking that source tree is clean..."
ifneq ($(shell git ls-files -m),)
	@echo "No"
	@echo "The following files have been modified since the most recent git commit:"
	@echo ""
	@git ls-files -m
	@echo ""
	@echo "The release will be made from the committed state, but perhaps you meant"
	@echo "to commit this code first? Please clean this up to make it more clear."
	@false
else
	@echo "Good"
endif
endif

.PHONY: verify-version-debian
verify-version-debian: verify-version-components
	@echo -n "Checking that Debian package version is $(VERSION)-1..."
	@[ "$(VERSION)-1" = $$(sed '1{ s/).*//; s/.*(//; q; }' debian/changelog) ] || \
		(echo "No." && \
		 echo "Please edit version and debian/changelog to have consistent versions." && false)
	@echo "Good."

.PHONY: verify-version-python
verify-version-python: verify-version-components
	@echo -n "Checking that python bindings version is $(VERSION)..."
	@[ "$(VERSION)" = $$(python -c "execfile('$(PV_FILE)'); print __VERSION__") ] || \
		(echo "No." && \
		 echo "Please edit version and $(PV_FILE) to have consistent versions." && false)
	@echo "Good."

.PHONY: verify-version-components
verify-version-components:
	@echo -n "Checking that $(VERSION) consists only of digits and periods..."
	@echo $(VERSION) | grep -q -x '^[0-9.]*$$' || \
		(echo "No." && \
	         echo "Please follow the instructions in RELEASING to choose a version" && false)
	@echo "Good."

.PHONY: verify-newer
verify-newer:
	@echo -n "Checking that no $(VERSION) release already exists..."
	@wget -q -O /dev/null $(RELEASE_URL)/$(TAR_FILE) ; \
	case $$? in \
          8) echo "Good." ;; \
          0) echo "Ouch."; \
	     echo "Found: $(RELEASE_URL)/$(TAR_FILE)"; \
	     echo "Refusing to replace an existing release."; \
	     echo "Don't forget to update \"version\" as described in RELEASING before release." ; \
	     false ;; \
	  *) echo "An unexpected error occured"; \
	     false;; esac

# The user has not set any verbosity, default to quiet mode and inform the
# user how to enable verbose compiles.
ifeq ($(V),)
quiet_DOC := "Use \"$(MAKE) V=1\" to see the verbose compile lines.\n"
quiet = @printf $(quiet_DOC)$(eval quiet_DOC:=)"$1 $@\n"; $($(shell echo $1 | sed -e s'/ .*//'))
endif
# The user has explicitly enabled quiet compilation.
ifeq ($(V),0)
quiet = @printf "$1 $@\n"; $($(shell echo $1 | sed -e s'/ .*//'))
endif
# Otherwise, print the full command line.
quiet ?= $($(shell echo $1 | sed -e s'/ .*//'))

%.o: %.cc $(global_deps)
	$(call quiet,CXX $(CXXFLAGS)) -c $(FINAL_CXXFLAGS) $< -o $@

%.o: %.c $(global_deps)
	$(call quiet,CC $(CFLAGS)) -c $(FINAL_CFLAGS) $< -o $@

.deps/%.d: %.c $(global_deps)
	@set -e; rm -f $@; mkdir -p $$(dirname $@) ; \
	$(CC) -M $(CPPFLAGS) $(FINAL_CFLAGS) $< > $@.$$$$ 2>/dev/null ; \
	sed 's,'$$(basename $*)'\.o[ :]*,$*.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

.deps/%.d: %.cc $(global_deps)
	@set -e; rm -f $@; mkdir -p $$(dirname $@) ; \
	$(CXX) -M $(CPPFLAGS) $(FINAL_CXXFLAGS) $< > $@.$$$$ 2>/dev/null ; \
	sed 's,'$$(basename $*)'\.o[ :]*,$*.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

.PHONY : clean
clean:
	rm -f $(CLEAN); rm -rf .deps

# We don't (yet) have any distributed files not in the upstream repository.
# So distclean is currently identical to clean.
.PHONY: distclean
distclean: clean

notmuch_client_srcs =		\
	command-line-arguments.c\
	debugger.c		\
	gmime-filter-reply.c	\
	gmime-filter-headers.c	\
	hooks.c			\
	notmuch.c		\
	notmuch-config.c	\
	notmuch-count.c		\
	notmuch-dump.c		\
	notmuch-new.c		\
	notmuch-reply.c		\
	notmuch-restore.c	\
	notmuch-search.c	\
	notmuch-setup.c		\
	notmuch-show.c		\
	notmuch-tag.c		\
	notmuch-time.c		\
	query-string.c		\
	show-message.c		\
	mime-node.c		\
	json.c

notmuch_client_modules = $(notmuch_client_srcs:.c=.o)

notmuch: $(notmuch_client_modules) lib/libnotmuch.a util/libutil.a
	$(call quiet,CXX $(CFLAGS)) $^ $(FINAL_LIBNOTMUCH_LDFLAGS) -o $@

notmuch-shared: $(notmuch_client_modules) lib/$(LINKER_NAME)
	$(call quiet,$(FINAL_NOTMUCH_LINKER) $(CFLAGS)) $(notmuch_client_modules) $(FINAL_NOTMUCH_LDFLAGS) -o $@

.PHONY: install
install: all install-man
	mkdir -p "$(DESTDIR)$(prefix)/bin/"
	install notmuch-shared "$(DESTDIR)$(prefix)/bin/notmuch"
ifeq ($(MAKECMDGOALS), install)
	@echo ""
	@echo "Notmuch is now installed to $(DESTDIR)$(prefix)"
	@echo ""
	@echo "New users should simply run \"notmuch\" to be guided"
	@echo "through the process of configuring notmuch and creating"
	@echo "a database of existing email messages. The \"notmuch\""
	@echo "command will also offer some sample search commands."
ifeq ($(WITH_EMACS), 1)
	@echo ""
	@echo "Beyond the command-line interface, notmuch also offers"
	@echo "a full-featured interface for reading and writing mail"
	@echo "within emacs. To use this, each user should add the"
	@echo "following line to the ~/.emacs file:"
	@echo ""
	@echo "	(require 'notmuch)"
	@echo ""
	@echo "And then run emacs as \"emacs -f notmuch\" or invoke"
	@echo "the command \"M-x notmuch\" from within emacs."
endif
endif

.PHONY: install-desktop
install-desktop:
	mkdir -p "$(DESTDIR)$(desktop_dir)"
	desktop-file-install --mode 0644 --dir "$(DESTDIR)$(desktop_dir)" notmuch.desktop

SRCS  := $(SRCS) $(notmuch_client_srcs)
CLEAN := $(CLEAN) notmuch notmuch-shared $(notmuch_client_modules) notmuch.elc

DEPS := $(SRCS:%.c=.deps/%.d)
DEPS := $(DEPS:%.cc=.deps/%.d)
-include $(DEPS)