# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Yuui Tanabe <yuuitanabe@163.com>

_realname=gnupg
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.27
pkgrel=2
pkgdesc="GNU Privacy Guard - a PGP replacement tool (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libksba"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-libassuan"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libusb-compat"
         "${MINGW_PACKAGE_PREFIX}-npth"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-pinentry")
makedepends=("texinfo" "${MINGW_PACKAGE_PREFIX}-gcc")
# REMOVE openldap package before build
license=('GPL')
options=(emptydirs)
url="https://gnupg.org/"
source=(https://gnupg.org/ftp/gcrypt/gnupg/${_realname}-${pkgver}.tar.bz2{,.sig}
        01-mingw32-fix-potomo.mingw.patch
        02-mingw-sysconfigdir-under-prefix.patch
        03-ldap-libraries-check-order.patch
        04-dos-extra-defs.patch)
sha256sums=('34e60009014ea16402069136e0a5f63d9b65f90096244975db5cea74b3d02399'
            'SKIP'
            '592396d968353f991c879488b4b3e55a550873fa74703e08c7d28bb22241d8b9'
            'a936d17a8dc60fb401db07d040e55e03ba382d5e38871d1b62f527b1aa458dc1'
            'b985b01edb3924b62ed99180f0b08a6ae2cdcab0fc81d14cd7169ccb278e756e'
            '82b0cccc99d7303d05fcea4d7295f63b64fd92f7ea346312bfc4a69794237920')
validpgpkeys=('D8692123C4065DEA5E0F3AB5249B39D24F25E3B6'
              '031EC2536E580D8EA286A9F22071B08A33BD3F06'
              '5B80C5754298F0CB55D8ED6ABCEF7E294B092E28'
              '6DAA6E64A76D2840571B4902528897B826403ADA')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/01-mingw32-fix-potomo.mingw.patch
  patch -p1 -i ${srcdir}/02-mingw-sysconfigdir-under-prefix.patch
  patch -p1 -i ${srcdir}/03-ldap-libraries-check-order.patch
  patch -p1 -i ${srcdir}/04-dos-extra-defs.patch

  autoreconf -fiv
  sed 's/development_version=yes/development_version=no/' -i configure
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

# I had to disable doc generation because that would cause a
#a segmentation fault with something related to tex files.
#It would only happen for 32-bit versions.
  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --libexecdir=${MINGW_PREFIX}/bin \
      --sbindir=${MINGW_PREFIX}/bin \
      --sysconfdir=${MINGW_PREFIX}/etc/gnupg \
      --enable-maintainer-mode \
      --disable-symcryptrun \
      --enable-gpgtar \
      --disable-doc \
      --disable-g13
  
  MSYS2_ARG_CONF_EXCL="-DGNUPG_BINDIR=;-DGNUPG_LIBEXECDIR=;-DGNUPG_LIBDIR=;-DGNUPG_DATADIR=;-DGNUPG_SYSCONFDIR=;-DGNUPG_LOCALSTATEDIR=;-DLOCALEDIR=" \
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
# a few tests fail because of not executable errors
  make check || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONF_EXCL="-DGNUPG_BINDIR=;-DGNUPG_LIBEXECDIR=;-DGNUPG_LIBDIR=;-DGNUPG_DATADIR=;-DGNUPG_SYSCONFDIR=;-DGNUPG_LOCALSTATEDIR=;-DLOCALEDIR=" \
  make -j1 DESTDIR=${pkgdir} install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{etc/gnupg,var/cache/gnupg,home}
}
