# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=gpgme
pkgname=('libgpgme' 'libgpgme-devel' 'libgpgme-python')
pkgver=1.24.3
pkgrel=1
pkgdesc="A C wrapper library for GnuPG"
arch=('i686' 'x86_64')
url="https://gnupg.org/related_software/gpgme/"
msys2_repository_url="https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gpgme.git"
msys2_references=(
  "cpe: cpe:/a:gnu:gpgme"
)
license=('LGPL')
depends=('libassuan' 'libgpg-error' 'gnupg')
makedepends=(
  'libassuan-devel'
  'libgpg-error-devel'
  'python-devel'
  'python-setuptools'
  'swig'
  'autotools'
  'gcc'
  'python-build'
  'python-installer'
)
source=(https://gnupg.org/ftp/gcrypt/${pkgbase}/${pkgbase}-${pkgver}.tar.bz2{,.sig}
        0001-fix-broken-version.patch)
sha256sums=('bfc17f5bd1b178c8649fdd918956d277080f33df006a2dc40acdecdce68c50dd'
            'SKIP'
            'ff9eec1d2c1803a3f1f77cb8073c7a328859e5b26e8b0327581357110038d4cd')
#These might be signed by any of these keys https://gnupg.org/signature_key.html
validpgpkeys=('5B80C5754298F0CB55D8ED6ABCEF7E294B092E28'
              '6DAA6E64A76D2840571B4902528897B826403ADA'
              'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD')

prepare() {
  cd ${srcdir}/${pkgbase}-${pkgver}

  # otherwise it appends "unknown" to the version which isn't a valid python version
  patch -p1 -i "${srcdir}/0001-fix-broken-version.patch"

  autoreconf -ivf
}

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

  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --disable-gpgsm-test \
    --disable-gpgconf-test \
    --disable-g13-test \
    --enable-shared \
    --enable-static \
    --enable-languages="cpp python cl" \
    --enable-fixed-path="/usr/bin:/bin:/usr/sbin:/usr/local/bin"

  make no_undefined=-no-undefined

  (
    # use a PEP517 workflow to get a reproducible Python package
    cd lang/python/
    top_builddir="${srcdir}/${pkgbase}-${pkgver}" \
      python -m build --wheel --no-isolation
  )

  make DESTDIR=${srcdir}/dest install
}

check() {
  cd ${srcdir}/${pkgbase}-${pkgver}
  make check || true
}

package_libgpgme() {
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/{bin,share}
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  #cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/info ${pkgdir}/usr/share/
}

package_libgpgme-devel() {
  pkgdesc="libgpgme headers and libraries"
  groups=('development')
  depends=("libgpgme=${pkgver}" 'libassuan-devel' 'libgpg-error-devel')

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib/cmake ${pkgdir}/usr/lib/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/common-lisp ${pkgdir}/usr/share/
}

package_libgpgme-python() {
  pkgdesc="libgpgme bindings for Python"
  groups=('development')
  depends=("libgpgme=${pkgver}" "python")
  provides=("libgpgme-python3")
  conflicts=("libgpgme-python3")
  replaces=("libgpgme-python3")

  cd ${srcdir}/${pkgbase}-${pkgver}

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