# Maintainer: Eli Schwartz <eschwartz@archlinux.org>
# Maintainer: Jonas Witschel <diabonas@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.2.4
pkgrel=1
pkgdesc="OpenPGP message encryption and authentication for Thunderbird"
arch=('any')
url="https://www.enigmail.net/"
license=('MPL' 'GPL3')
makedepends=('zip' 'python' 'perl')
replaces=('thunderbird-enigmail')
source=("https://www.enigmail.net/download/source/enigmail-${pkgver}.tar.gz"{,.asc}
        "0001-genxpi-make-XPI-files-reproducible.patch")
sha512sums=('bf23f52ed61002b05e2e432a0abd1f576fe6eb97d94ab2241e17be86138cee153d668747f5759b967d9a4977be3dc901ee78ddb168347bdbbfed9229d27f786d'
            'SKIP'
            '78457902b66792e75f284e5e5a73e5431b71908da79c94f1b9e45faf0f4cefcbe4e3c355d226e7e0a682dd65e913a3a68ef3dc4d6223f9584962fb7c13f70eb4')
b2sums=('e6d5eab4cba2cd66669ddf38f8152511606c48ce995be331551a7b403ecbd06967c22702a06e06aa89219b5c905e835ddb4d146c0fc2cb11e1a647e46854dd1f'
        'SKIP'
        'f0fe6d5b0637002a16f066fc25b3c69113d88651163b3eab4a5da179842d5ddf4e5185dbc45491a5d4800b5cf00a5a89e355768263a710bd467ff070608de900')
validpgpkeys=('4F9F89F5505AC1D1A260631CDB1187B9DD5F693B') # Patrick Brunschwig <patrick@enigmail.net>

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

    # Make timestamps in the generated XPI file respect SOURCE_DATE_EPOCH
    # (https://gitlab.com/enigmail/enigmail/-/merge_requests/45 and
    #  https://gitlab.com/enigmail/enigmail/-/merge_requests/46)
    patch -p1 -i ../0001-genxpi-make-XPI-files-reproducible.patch
}

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

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

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

    if ! _extension_id="$(sed -n '/.*<em:id>\(.*\)<\/em:id>.*/{s//\1/p;q}' build-tb/dist/install.rdf 2>/dev/null)" ||
            [[ -z $_extension_id ]]; then
        _extension_id="$(sed -n 's/.*"id": "\(.*\)".*/\1/p' build-tb/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-tb/dist/install.rdf 2>/dev/null; then
        install -dm755 "${_extension_dest}"
        cp -R build-tb/dist/* "${_extension_dest}"
        chmod -R ugo+rX "${_extension_dest}"
    else
        install -Dm644 build-tb/enigmail-${pkgver}.xpi "${_extension_dest}.xpi"
    fi
}
