# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=p11-kit
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.23.22
pkgrel=1
pkgdesc="Library to work with PKCS#11 modules"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://p11-glue.freedesktop.org/p11-kit.html"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-libtasn1"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             #"${MINGW_PACKAGE_PREFIX}-meson"
             #"${MINGW_PACKAGE_PREFIX}-ninja"
             )
options=('!debug' 'strip' 'staticlibs')
source=(https://github.com/p11-glue/p11-kit/releases/download/${pkgver}/${_realname}-${pkgver}.tar.xz{,.sig}
        0001-allow-gtkdocize.all.patch
        0002-fix-includes.patch
        0004-fix-abspath-check.all.patch
        0006-p11-kit-relocation.patch
        0007-mmap-CreateFile-with-FILE_SHARE_READ.patch
        0008-add-debugging-to-mmap.patch
        0009-add-debugging-to-path.patch
        0010-fix-transport-test.patch
        0011-p11-kit-spawn-external.patch
        0012-no-version-script.patch
        0013-fix-reallocarray-decl.patch
        0014-meson-build.patch)
validpgpkeys=('C0F67099B808FB063E2C81117BFB1108D92765AF'
              '462225C3B46F34879FC8496CD605848ED7E69871')
sha256sums=('8a8f40153dd5a3f8e7c03e641f8db400133fb2a6a9ab2aee1b6d0cb0495ec6b6'
            'SKIP'
            '195b2e8695f701caf545e2a468383ad29457febd9b1ee57de1986de04ad3c31c'
            'aa92f986d3f7dfc119e86f9a8f3987e6ac2562149921820eadbe09218c4df99c'
            'ea4593324db6d2d193733561f0352c6d679d1fb00aed0a2bae0d28aecbe92721'
            'f75e16020b8059c2471514c99886217c391dd8b7ba4a0ce93a615fe68ad7ab9b'
            '2b0fc5c54776aca54e306315338323075e7412d8d800f490a31870008c34f8d1'
            '652e4be637bf77f20d8fde91175286701430e720e4082e8818a50ebddff28460'
            '8c796ade0ef6356c796d2e87b967c7a9d888783c08e740e35d5686f6deb7f009'
            '6c4b94bb8aee1981c07215d2c8449614f1a604a705f05806f6f1e8299b729698'
            'd5e804ba795aefe0a3938bc527b0e6c8ff8c1d5dfa1c0c1728c20a249504af6b'
            '15a2729f39e260f7fe8d55a91a399f2512854da626a69bc215470f902fc178cd'
            '83ecf11aaf9a1c6f617790048e80febd7320edae6ba203958349264170956911'
            'c04a384a6bc7e05098ee67d76cc4aecc9004eed2366b4ce9097428309b1581b6')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  rm -f common/pathtools.c common/pathtools.h > /dev/null 2>&1 || true
  patch -p1 -i ${srcdir}/0001-allow-gtkdocize.all.patch
  patch -p1 -i ${srcdir}/0002-fix-includes.patch
  patch -p1 -i ${srcdir}/0004-fix-abspath-check.all.patch
  patch -p1 -i ${srcdir}/0006-p11-kit-relocation.patch
  patch -p1 -i ${srcdir}/0007-mmap-CreateFile-with-FILE_SHARE_READ.patch
  # patch -p1 -i ${srcdir}/0007-add-debugging-to-mmap.patch
  # patch -p1 -i ${srcdir}/0008-add-debugging-to-path.patch
  patch -p1 -i ${srcdir}/0010-fix-transport-test.patch
  patch -p1 -i ${srcdir}/0011-p11-kit-spawn-external.patch
  patch -p1 -i ${srcdir}/0012-no-version-script.patch
  patch -p1 -i ${srcdir}/0013-fix-reallocarray-decl.patch
  patch -p1 -i ${srcdir}/0014-meson-build.patch

  autoreconf -vfi
  gtkdocize
}

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

  # PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/share/pkgconfig" \
  # ${MINGW_PREFIX}/bin/meson \
    # --prefix=${MINGW_PREFIX} \
    # --buildtype debugoptimized \
    # -Dgtk_doc=true \
    # -Dsystem_config=${MINGW_PREFIX}/etc \
    # -Dman=true \
    # -Dtrust_paths=${MINGW_PREFIX}/etc/pki/ca-trust/source:${MINGW_PREFIX}/share/pki/ca-trust-source \
    # ../${_realname}-${pkgver}

  # ninja

  PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/share/pkgconfig" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var \
    --with-trust-paths="${MINGW_PREFIX}/etc/pki/ca-trust/source:${MINGW_PREFIX}/share/pki/ca-trust-source" \
    --disable-silent-rules \
    "${extra_config[@]}"

  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=;-DP11_SYSTEM_CONFIG_FILE=;-DP11_SYSTEM_CONFIG_MODULES=;-DP11_PACKAGE_CONFIG_MODULES=;-DP11_MODULE_PATH=;-DDATADIR=;-DSYSCONFDIR=;-DPRIVATEDIR=;-DP11_USER_CONFIG_FILE=;-DP11_USER_CONFIG_MODULES=" \
    make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=;-DP11_SYSTEM_CONFIG_FILE=;-DP11_SYSTEM_CONFIG_MODULES=;-DP11_PACKAGE_CONFIG_MODULES=;-DP11_MODULE_PATH=;-DDATADIR=;-DSYSCONFDIR=;-DPRIVATEDIR=;-DP11_USER_CONFIG_FILE=;-DP11_USER_CONFIG_MODULES=" \
    make DESTDIR="${pkgdir}" install
}
