# Maintainer: Tobias Powalowski <tpowa@archlinux.org>
# Contributor: Roman Kyrylych <roman@archlinux.org>
# Contributor: Sarah Hay <sarah@archlinux.org>

pkgbase=gpgme
pkgname=(gpgme qgpgme python-gpgme)
pkgver=1.20.0
pkgrel=2
pkgdesc='A C wrapper library for GnuPG'
arch=('x86_64')
url='https://www.gnupg.org/related_software/gpgme/'
license=('LGPL')
makedepends=(
  'gnupg'
  'libgpg-error'
  'python'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
  'qt5-base'
  'swig'
)
validpgpkeys=('6DAA6E64A76D2840571B4902528897B826403ADA'  # Werner Koch (dist signing 2020)
              'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD') # Niibe Yutaka (GnuPG Release Key)
source=("https://www.gnupg.org/ftp/gcrypt/${pkgbase}/${pkgbase}-${pkgver}.tar.bz2"{,.sig})
sha256sums=('25a5785a5da356689001440926b94e967d02e13c49eb7743e35ef0cf22e42750'
            'SKIP')

build() {
  cd ${pkgbase}-${pkgver}

  ./configure \
    --prefix=/usr \
    --disable-fd-passing \
    --disable-static \
    --disable-gpgsm-test
  make

  (
    # use a PEP517 workflow to get a reproducible Python package
    # NOTE: top_builddir is required so that the build takes place against local gpgme, not system gpgme
    cd lang/python/
    top_builddir="$srcdir/$pkgbase-$pkgver" python -m build --wheel --no-isolation
  )
}

check() {
  cd ${pkgbase}-${pkgver}

  # this test fails with gnupg (FS#66572)
  sed -i 's#"t-keylist-secret",##' tests/json/t-json.c

  make check
}

package_gpgme() {
  depends=('libgpg-error' 'gnupg>=2')
  options+=('!emptydirs')
  provides=('libgpgme.so'
            'libgpgmepp.so')

  cd ${pkgbase}-${pkgver}

  make DESTDIR="${pkgdir}" install

  # split qgpgme
  rm -r "${pkgdir}"/usr/include/{qgpgme,QGpgME}/
  rm -r "${pkgdir}"/usr/lib/{cmake/QGpgme/,libqgpgme.*}
  rm -r "${pkgdir}"/usr/lib/python*
}

package_qgpgme() {
  pkgdesc="Qt bindings for GPGme"
  depends=('gpgme' 'qt5-base')

  cd ${pkgbase}-${pkgver}/lang/qt

  make DESTDIR="${pkgdir}" install
}

package_python-gpgme() {
  pkgdesc="Python bindings for GPGme"
  depends=('gpgme' 'python')

  cd ${pkgbase}-${pkgver}/lang/python
  python -m installer --destdir="$pkgdir" dist/*.whl
}
