# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Stefan Wagner <stw@bit-strickerei.de>
# Maintainer: Stefan Wagner <stw@bit-strickerei.de>
pkgname=notmuch
pkgver=0.25.2
pkgrel=0
pkgdesc="E-Mail index, search and tagging"
url="https://notmuchmail.org/"
arch="all"
license="GPL-3.0"
makedepends="gzip xapian-core-dev gmime-dev talloc-dev py-sphinx
	py-requests python2-dev python3-dev bash-completion"
checkdepends="sed coreutils diffutils bash tar mdocml grep"
subpackages="
	py2-$pkgname:py2
	py3-$pkgname:py3
	$pkgname-dev
	$pkgname-doc
	$pkgname-libs
	$pkgname-emacs:emacs
	$pkgname-vim:vim:noarch
	$pkgname-zsh-completion:zshcomp:noarch
	$pkgname-bash-completion:bashcomp:noarch"
source="https://notmuchmail.org/releases/$pkgname-$pkgver.tar.gz
	fix-tests.patch
	fix-tests-for-python3.patch"
builddir="$srcdir/$pkgname-$pkgver"

build() {
	cd "$builddir"

	PYTHON=python3 ./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--bashcompletiondir=/usr/share/bash-completion/completions \
		--zshcompletiondir=/usr/share/zsh/site-functions
	make PREFIX=/usr DESTDIR="$pkgdir"

	cd "$builddir"/bindings/python
	python2 setup.py build
	python3 setup.py build
}

package() {
	make PREFIX=/usr DESTDIR="$pkgdir" \
		-C "$builddir" install
}

check() {
	cd "$builddir"/test
	make test-binaries

	local test=
	for test in T*.sh; do
		name="$(basename "$test")"
		case "${name%%.*}" in
		# Requires dtach ↦ doesn't work on the builders
		*emacs*|T355-smime|T350-crypto) continue ;;

		# XXX: Doesn't pass on the builders for some reason
		T140-excludes) continue ;;

		# FIXME: These should pass but currently don't
		T050-new|T150-tagging) continue ;;
		esac

		./${test}
	done
}

vim() {
	depends="vim notmuch"
	pkgdesc="Vim plugins for $pkgname"

	make -C "$builddir/vim" DESTDIR="$subpkgdir" \
		prefix="/usr/share/vim/vimfiles" install
}

emacs() {
	depends="emacs notmuch"
	pkgdesc="Emacs plugins for $pkgname"

	mkdir -p "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/share/emacs/ "$subpkgdir"/usr/share/emacs/

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/notmuch-emacs-mua "$subpkgdir"/usr/bin/
}

bashcomp() {
	depends=""
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	mkdir -p "$subpkgdir"/usr/share/bash-completion/completions/
	mv "$pkgdir"/usr/share/bash-completion/completions/$pkgname \
		"$subpkgdir"/usr/share/bash-completion/completions/
}

zshcomp() {
	depends=""
	pkgdesc="Zsh completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	mkdir -p "$subpkgdir"/usr/share/zsh/site-functions/
	mv "$pkgdir"/usr/share/zsh/site-functions/_${pkgname} \
		"$subpkgdir"/usr/share/zsh/site-functions/
}

_py() {
	local python="$1"
	pkgdesc="$pkgdesc (for $python)"
	depends="$depends $python"
	install_if="$pkgname=$pkgver-r$pkgrel $python"

	cd "$builddir"/bindings/python
	$python setup.py install --prefix=/usr --root="$subpkgdir"
}

py2() {
	_py python2
}

py3() {
	_py python3
}

sha512sums="5ee7bded69cbcc980e23f1e1562bf0cc9294c20bddcc3e442030c4f29373ecbbaef79144ad38470bfc97dac87f8dfb3bc7aee2d7eb974f6998f933369aa46006  notmuch-0.25.2.tar.gz
430e8f02b194b0582dfd7c66a2791a0071824e1d215993a6daf89c8d2a1ee7f73a6202136b1d890fc8920a4dc309ae976da8a8b8fc8b90e489d3c04f1592876c  fix-tests.patch
a7dd888c5d08835be75986e67afcbc95c7a65c187fab5b0eb969bb36e9bbbc2d647789f106f709a38906eed51e3684a3241678ace1fc8c4b5e1cdbdd06fb6bbb  fix-tests-for-python3.patch"
