# 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.4.8
pkgrel=1
pkgdesc="GNU Privacy Guard - a PGP replacement tool (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gnupg.org/"
msys2_references=(
  'archlinux: gnupg'
  'cpe: cpe:/a:gnupg:gnupg'
)
msys2_repository_url='https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git'
license=('spdx:GPL-3.0-or-later')
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}-npth"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-pinentry")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "texinfo")
# REMOVE openldap package before build
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
        05-gnupg-include-winber.patch)
sha256sums=('b58c80d79b04d3243ff49c1c3fc6b5f83138eb3784689563bcdd060595318616'
            'SKIP'
            'ab05acce27ade6510e913c7c901855a71843a6ee3c3713f3dc881e49a8d55e91'
            '28b8eeeb9c078b9a1ff153a8187ad54e194bd1b23017ea60cfdcb02c4000fc43'
            'b985b01edb3924b62ed99180f0b08a6ae2cdcab0fc81d14cd7169ccb278e756e'
            '5c0c6be11d2c8e49272a09a368c52a8b8954b89344984506a0156c9eeda24d34'
            'b482d26640659160c62688d1954ca459392eba65fef4c8eada7dc5c2d43652e4')
validpgpkeys=(
  '5B80C5754298F0CB55D8ED6ABCEF7E294B092E28' # Andre Heinecke (Release Signing Key)
  '6DAA6E64A76D2840571B4902528897B826403ADA' # Werner Koch (dist signing 2020)
  'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD' # Niibe Yutaka (GnuPG Release Key)
  '02F38DFF731FF97CB039A1DA549E695E905BA208' # GnuPG.com (Release Signing Key 2021)
)

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  _apply_patch_with_msg \
    01-mingw32-fix-potomo.mingw.patch \
    02-mingw-sysconfigdir-under-prefix.patch \
    03-ldap-libraries-check-order.patch \
    04-dos-extra-defs.patch \
    05-gnupg-include-winber.patch

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

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

# 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.

# Workaround for gcc 14 compiler error
  CFLAGS+=" -Wno-error=incompatible-pointer-types -Wno-int-conversion" \
  ../${_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 \
      --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-${MSYSTEM}"
  # a few tests fail because of not executable errors
  make check || warning "Tests failed"
}

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

  install -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING* "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"

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