# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=oqsprovider
_pkgname=oqs-provider
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.10.0
pkgrel=1
pkgdesc="OpenSSL 3 provider containing post-quantum algorithms (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://openquantumsafe.org/applications/tls.html#oqs-openssl-provider'
msys2_repository_url="https://github.com/open-quantum-safe/oqs-provider"
license=('spdx:MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-liboqs"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
)
source=(https://github.com/open-quantum-safe/$_pkgname/archive/$pkgver/$_realname-$pkgver.tar.gz
        "0001-fix-test-build.patch"
        "0002-fix-missing-prefix-relocation.patch")
sha256sums=('b6caaa8701678a5360600cff439c0eeda387d698e9cc432ac05d2c253029776c'
            '73c44a80f10336a38f53bf7f08ad8c7ea49c63794c41023a81aba616763cb492'
            '4dd63f1a5aef651819951d085ef89a1e8458498b268b55b3e67632fcc0b0e040')

prepare() {
  cd "${_pkgname}-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-fix-test-build.patch"
  patch -Np1 -i "${srcdir}/0002-fix-missing-prefix-relocation.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -S "${_pkgname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target test
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_pkgname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt
}
