# Template file for 'notmuch'
pkgname=notmuch
version=0.31.3
revision=1
hostmakedepends="perl pkg-config python3-Sphinx python3-devel texinfo"
makedepends="bash-completion gmime3-devel talloc-devel xapian-core-devel
 python3-cffi python3-setuptools gnupg2"
depends="gmime3>=3.2.7"
checkdepends="python3-pytest mdocml dtach gdb tar xz"
short_desc="Thread-based email index, search, and tagging"
maintainer="Jan S. <jan.schreib@gmail.com>"
license="GPL-3.0-or-later"
homepage="https://notmuchmail.org"
distfiles="https://notmuchmail.org/releases/notmuch-${version}.tar.xz
 https://notmuchmail.org/releases/test-databases/database-v1.tar.xz"
checksum="484041aed08f88f3a528a5b82489b6cda4090764228813bca73678da3a753aca
 4299e051b10e1fa7b33ea2862790a09ebfe96859681804e5251e130f800e69d2"
skip_extraction="database-v1.tar.xz"

# Not a real conflicts, but this package is broken with gpgme 1.13.1-
# gpgme is optional dependency
conflicts="gpgme<1.14.0_1"
subpackages="libnotmuch libnotmuch-devel notmuch-mutt notmuch-python3"
patch_args=-Np1

if [ ! "$CROSS_BUILD" ]; then
	hostmakedepends+=" desktop-file-utils"
	makedepends+=" ruby-devel emacs"
	subpackages+=" notmuch-ruby notmuch-emacs"
	_args="--emacslispdir=/usr/share/emacs/site-lisp/notmuch
		--emacsetcdir=/usr/share/emacs/site-lisp/notmuch"
else
	_args="--without-emacs"
fi

do_configure() {
	./configure --prefix=/usr \
		--bashcompletiondir=/usr/share/bash-completion/completions \
		--zshcompletiondir=/usr/share/zsh/site-functions ${_args}
}

do_build() {
	make ${makejobs}
	make -C contrib/notmuch-mutt
	cd bindings/python
	python3 setup.py build --build-base=build
}

do_check() {
	mkdir -p fake-bin
	ln -sf /usr/bin/gpg2 fake-bin/gpg
	PATH=$PWD/fake-bin:$PATH
	cp $XBPS_SRCDISTDIR/notmuch-$version/database-v1.tar.xz \
		test/test-databases
	case "$XBPS_TARGET_LIBC" in
	musl)
		# Seem like gpgconf's problem,
		# mutt on musl also have problems with smime
		export NOTMUCH_SKIP_TESTS="smime.3 smime.5" ;;
	*)
		# This test run under gdb is problematic
		export NOTMUCH_SKIP_TESTS="count.14" ;;
	esac
	make test
}

do_install() {
	make DESTDIR=${DESTDIR} install
	pushd bindings/python
	python3 setup.py build --build-base=build \
		install --prefix=/usr --root=${DESTDIR}
	popd
	if [ ! "$CROSS_BUILD" ]; then
		make DESTDIR=${DESTDIR} -C bindings/ruby install
	fi
}

post_install() {
	rm -f ${DESTDIR}/usr/share/applications/mimeinfo.cache
}

libnotmuch_package() {
	short_desc+=" - runtime library"
	pkg_install() {
		vmove usr/lib/*.so.*
	}
}

libnotmuch-devel_package() {
	short_desc+=" - development files"
	depends="libnotmuch-${version}_${revision}"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/*.so
	}
}

notmuch-emacs_package() {
	short_desc+=" - Emacs interface"
	depends="${sourcepkg}-${version}_${revision} virtual?emacs"
	pkg_install() {
		vmove usr/bin/notmuch-emacs-mua
		vmove usr/share/applications
		vmove usr/share/emacs/site-lisp
		vmove usr/share/info/notmuch-emacs*
		vmove usr/share/man/man1/notmuch-emacs*
	}
}

notmuch-mutt_package() {
	short_desc+=" - Mutt interface"
	depends="${sourcepkg}-${version}_${revision} perl-Mail-Box perl-MailTools
	 perl-String-ShellQuote perl-Term-ReadLine-Gnu perl-File-Which"
	pkg_install() {
		vbin contrib/notmuch-mutt/notmuch-mutt
		vman contrib/notmuch-mutt/notmuch-mutt.1
	}
}

notmuch-python3_package() {
	depends="libnotmuch-${version}_${revision}"
	short_desc+=" - Python 3 bindings"
	pkg_install() {
		vmove ${py3_sitelib}
	}
}

notmuch-ruby_package() {
	depends="libnotmuch-${version}_${revision} ruby"
	short_desc+=" - Ruby bindings"
	pkg_install() {
		vmove usr/lib/ruby
	}
}
