# Maintainer: Eli Schwartz <eschwartz@archlinux.org>
# Contributor: Daniel Landau <daniel.landau@iki.fi>
# Contributor: Einhard Leichtfuß <alguien@respiranto.de>
# Contributor: Xyne
# Contributor: David Manouchehri <d@32t.ca>
# Contributor: Alexander Fehr <pizzapunk gmail com>
# Contributor: Thomas Jost <schnouki schnouki net>
# Contributor: Hinrich Harms <arch hinrich de>

pkgname=thunderbird-extension-enigmail
pkgver=2.1.2
pkgrel=1
pkgdesc="OpenPGP message encryption and authentication for Thunderbird"
arch=('any')
url="https://www.enigmail.net/"
license=('MPL' 'GPL3')
makedepends=('zip' 'python2' 'perl')
replaces=('thunderbird-enigmail')
source=("https://www.enigmail.net/download/source/enigmail-${pkgver}.tar.gz"{,.asc}
        "0001-preferences-disable-pEpAutoDownload-by-default.patch")
sha512sums=('c76dcdb0561ee6e94ab19c2ed8cddac71297ffc9ad14ac3d37cd8966cc4111cbc74d1b01ccaa044acef699de27a2cc7ba1ade7a31db8ab930095722ffc62be93'
            'SKIP'
            '4307c7b6bce7febba7a8186467c3bc93ec3428e1cd75cddad659e4d4a71832f1c8d46e4c811da38ff1dc38b1961a82bd84e7c90b7c63ce080be49ce69571c016')
b2sums=('335c7569e2c46dde645c8992cd7017dc57a0174e109ddada2719cb60de010c8ca3691c1d31e8435986b459936bd1602153e462fb5f93d7ffd1244ea867d8d3bc'
        'SKIP'
        'feb16236a5e5f19a7b7b3259d204732e8a9a5fee78726c5bae67d7a998c10edc89d2131acaa44c6d524ab346670b8b969e0ffd5b8f3fafe0b5b7ae90a1984764')
validpgpkeys=('4F9F89F5505AC1D1A260631CDB1187B9DD5F693B') # Patrick Brunschwig <patrick@enigmail.net>

prepare() {
    cd "${srcdir}"/enigmail

    # Using vendor settings via /usr/lib/thunderbird/defaults/preferences/enigmail.js
    # does not seem to work.
    patch -p1 -i ../0001-preferences-disable-pEpAutoDownload-by-default.patch
}

build() {
    cd "${srcdir}"/enigmail

    ./configure
    make -j1 # fails with -j greater than 1
}

package() {
    depends=('thunderbird>=68' 'gnupg')
    cd "${srcdir}"/enigmail

    if ! _extension_id="$(sed -n '/.*<em:id>\(.*\)<\/em:id>.*/{s//\1/p;q}' build/dist/install.rdf 2>/dev/null)" ||
            [[ -z $_extension_id ]]; then
        _extension_id="$(sed -n 's/.*"id": "\(.*\)".*/\1/p' build/dist/manifest.json)"
    fi
    _extension_dest="${pkgdir}/usr/lib/thunderbird/extensions/${_extension_id}"
    # Should this extension be unpacked or not?
    if grep -q '<em:unpack>true</em:unpack>' build/dist/install.rdf 2>/dev/null; then
        install -dm755 "${_extension_dest}"
        cp -R build/dist/* "${_extension_dest}"
        chmod -R ugo+rX "${_extension_dest}"
    else
        install -Dm644 build/enigmail-${pkgver}.xpi "${_extension_dest}.xpi"
    fi
}
