# Maintainer: Yang Kun <ikspress@outlook.com>

_realname=wolfssl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.8.2
pkgrel=1
pkgdesc="Lightweight, portable, C-language-based SSL/TLS library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.wolfssl.com/products/wolfssl/'
msys2_repository_url="https://github.com/wolfssl/wolfssl"
license=('spdx:GPL-2.0-or-later')
msys2_references=(
  'anitya: 21631'
  'archlinux: wolfssl'
  'cpe: cpe:2.3:a:wolfssl:wolfssl'
)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/wolfssl/wolfssl/archive/v${pkgver}-stable/${_realname}-${pkgver}-stable.tar.gz"
        "https://github.com/wolfssl/wolfssl/releases/download/v${pkgver}-stable/${_realname}-${pkgver}-stable.tar.gz.asc")
sha256sums=('3ef126e3466e2f8f6ebb62b916a7f8fb26c6709dbdf2b63a167759f2fdb53068'
            'SKIP')
validpgpkeys=('A2A48E7BCB96C5BECB987314EBC80E415CA29677') # wolfSSL <secure@wolfssl.com>

build() {
  local cmake_options=(
    "-DWOLFSSL_CURVE25519=ON"
    "-DWOLFSSL_CURVE448=ON"
    "-DWOLFSSL_ED25519=ON"
    "-DWOLFSSL_ED448=ON"
    "-DWOLFSSL_REPRODUCIBLE_BUILD=ON"
    "-DWOLFSSL_SHA224=yes"
    "-DWOLFSSL_SHA3=yes"
    "-DWOLFSSL_SNI=ON"
  )
  if check_option "debug" "n"; then
    cmake_options+=("-DCMAKE_BUILD_TYPE=Release")
  else
    cmake_options+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Shared Build
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
    -GNinja \
    -B "build-${MSYSTEM}-shared" \
    -S "${_realname}-${pkgver}-stable" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DBUILD_SHARED_LIBS=ON \
    "${cmake_options[@]}"

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

  # Static Build
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
    -GNinja \
    -B "build-${MSYSTEM}-static" \
    -S "${_realname}-${pkgver}-stable" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DBUILD_SHARED_LIBS=OFF \
    "${cmake_options[@]}"

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

check() {
  # Shared Check
  PATH="$PWD/build-${MSYSTEM}-shared":$PATH \
  cmake --build "build-${MSYSTEM}-shared" --target test || true

  # Static Check
  cmake --build "build-${MSYSTEM}-static" --target test || true
}

package() {
  # Static Install
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-static"

  # Shared Install
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-shared"

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}-stable/COPYING" \
    -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
