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

_realname=libassuan
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.7
pkgrel=1
pkgdesc="A IPC library used by some GnuPG related software (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx: LGPL-2.1-or-later OR GPL-3.0-or-later')
url="https://gnupg.org/related_software/libassuan"
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libgpg-error")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://gnupg.org/ftp/gcrypt/${_realname}/${_realname}-${pkgver}.tar.bz2"{,.sig}
        0001-fix-def-files.mingw.patch
        0002-enable-silent-rules.mingw.patch
        0004-enable-gnu.mingw.patch
        0005-better-check-for-stpcpy.mingw.patch
        0006-fix-broken-version.patch)
#These might be signed by any of these keys https://gnupg.org/signature_key.html
validpgpkeys=(
  '6DAA6E64A76D2840571B4902528897B826403ADA' # Werner Koch (dist signing 2020)
  'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD' # Niibe Yutaka (GnuPG Release Key)
)
sha256sums=('0103081ffc27838a2e50479153ca105e873d3d65d8a9593282e9c94c7e6afb76'
            'SKIP'
            '7aca3f3fd476ca9488c41a3c74c36f22f84957320066aa26dd47535ec2d65bce'
            'c61974720f41e982cadcfedbb3f9f77aa856bc5a59dcc3d76bbc30ff0d3b2c8a'
            '7dfedfe37e3d9719f5560d311a9036903cc728b65b868a110af27e29cf4ffdee'
            '4ae136681100906ddd67ef72de697410c12e604d0704bad4c3cebcc181120882'
            '21867abdb907285250289399721c8ddfbd16f96cce3fc5dc20a14a39a931bd7d')

_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 \
   0001-fix-def-files.mingw.patch \
   0002-enable-silent-rules.mingw.patch \
   0004-enable-gnu.mingw.patch \
   0005-better-check-for-stpcpy.mingw.patch \
   0006-fix-broken-version.patch

  autoreconf -fi
}

build() {
  if [ ${MINGW_CHOST} = "x86_64-w64-mingw32" ]; then
    CFLAGS+=" -DHAVE_DOSISH_SYSTEM=1"
  fi

  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-shared \
    --enable-static

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check || true
}

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"
}
