# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libp11
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.4.16
pkgrel=1
pkgdesc="A library implementing a small layer on top of the PKCS11 API (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/OpenSC/libp11"
license=('spdx:LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-openssl")
# optdepends("${MINGW_PACKAGE_PREFIX}-p11-kit: seamless PKCS#11 modules integration")
source=("https://github.com/OpenSC/libp11/releases/download/libp11-${pkgver}/libp11-${pkgver}.tar.gz"{,.asc}
        0001-relocate-pkgconfig-install-paths.patch
        "engine_h_win32.patch"
        "readme.msys2")
sha256sums=('97777640492fa9e5831497e5892e291dfbf39a7b119d9cb6abb3ec8c56d17553'
            'SKIP'
            '2637e24bd085de10d1330a7df71ffa1d4d25265d55ce38f03788a8b641c614cc'
            '199b35e7e6dac997c7cc08096e9d7bc3ba8ab1c222b5d63b4eeaadaafa5ceeca'
            '73d7e42d25a6f109f089066e6e6c483471ab7b4f78e3401f9d3783a2fd151667')
validpgpkeys=('AC915EA30645D9D3D4DAE4FEB1048932DD3AAAA3') # Michał Trojnara <Michal.Trojnara@stunnel.org>

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/engine_h_win32.patch
  patch -p1 -i "${srcdir}"/0001-relocate-pkgconfig-install-paths.patch

  autoreconf -fi
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=;--with-enginesdir=;--with-modulesdir=" \
  ../"${_realname}-${pkgver}"/configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --with-enginesdir="${MINGW_PREFIX}/lib/engines-3" \
    --with-modulesdir="${MINGW_PREFIX}/lib/ossl-modules" \
    --with-pkcs11-module=libp11-kit-0.dll # p11-kit lib/proxy module

  # re with-pkcs11-module: pkg-config returns an absolute path, but we want
  # things to be relocatable so hardcode the .dll name here

  make
}

package() {
  cd "build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/readme.msys2" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/readme.msys2"
}
