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

_realname=libgpg-error
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.55
pkgrel=1
pkgdesc="Support library for libgcrypt (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gnupg.org"
msys2_repository_url="https://git.gnupg.org/cgi-bin/gitweb.cgi?p=libgpg-error.git"
msys2_references=(
  "cpe: cpe:2.3:a:gnupg:libgpg-error"
)
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-gettext-tools")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs" "${MINGW_PACKAGE_PREFIX}-gettext-runtime")
source=("https://gnupg.org/ftp/gcrypt/libgpg-error/${_realname}-${pkgver}.tar.bz2"{,.sig}
        01-fix-broken-version.patch
        02-fix-symbollist-on.mingw.patch
        05-w32-gen.all.patch
        07-windows-build.patch
        10-gpg-error-static-linking.patch)
#These might be signed by any of these keys https://gnupg.org/signature_key.html
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)
sha256sums=('95b178148863f07d45df0cea67e880a79b9ef71f5d230baddc0071128516ef78'
            'SKIP'
            '143cfc654adf37924c6e9dd4990b91f037ac835d736b42dfa725315afe100fa0'
            '118ae7c56ef6a3f57cbf834d06b17e3f6a9af4d5fa63fb07cdf2fe4a172c92be'
            'eff12bcd98ec928a99f12c6ce3118a71828e0dc0c753fb613266d4d737b1c23a'
            'a245bba8e6a115be451a83a76143c5a9ab86e536c7668c3cd1a9f535ab205b10'
            '9780e3e397b983ef52d5dae22d54c4453a47bddf04eafa2f874c75038e3485eb')

_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-fix-broken-version.patch \
    02-fix-symbollist-on.mingw.patch \
    05-w32-gen.all.patch \
    07-windows-build.patch \
    10-gpg-error-static-linking.patch

  autoreconf -fi
}

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

  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --enable-install-gpg-error-config \
      --enable-shared \
      --enable-static

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

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