# Maintainer: taozuhong <taozuhong@gmail.com>

_realname=libfido2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.16.0
pkgrel=1
pkgdesc="Library functionality for FIDO 2.0, including communication with a device over USB (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://developers.yubico.com/libfido2/"
license=('BSD-2-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
depends=("${MINGW_PACKAGE_PREFIX}-libcbor"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-cc-libs")
source=("https://developers.yubico.com/libfido2/Releases/libfido2-${pkgver}.tar.gz"{,.sig}
        "001-fixed-link-cmd.patch"
        "002-no-version-script.patch"
        "003-fix-diagnostic-build.patch")
sha256sums=('8c2b6fb279b5b42e9ac92ade71832e485852647b53607c43baaafbbcecea04e4'
            'SKIP'
            '3134757a26e80eb1917036d4402551fa517e4e0e678c54dcf6c166b714de2bf2'
            'c4592b59f3714803df029b8ae4ced3d1474e90d591a36fc9a84bb1e90888230e'
            '91fd17023f143f74af73aabb3452a4a1616fc0f43bf4e466dadfe515bd82e330')
# https://developers.yubico.com/Software_Projects/Software_Signing.html
validpgpkeys=(
  '1D7308B0055F5AEF36944A8F27A9C24D9588EA0F' # Aveen Ismail <aveen.ismail@yubico.com>
  '7FBB6186957496D58C751AC20E777DD85755AA4A' # Konstantinos Georgantas <kostas@yubico.com>
  '78D997D53E9C0A2A205392ED14A19784723C9988' # Ludvig Michaelsson <ludvig.michaelsson@yubico.com>
)

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-fixed-link-cmd.patch
  cp src/export.msvc src/export.def 
  patch -p1 -i ${srcdir}/002-no-version-script.patch
  patch -p1 -i ${srcdir}/003-fix-diagnostic-build.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

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

  # XXX: avoid mingw-w64 pulling in winpthreads for clock_gettime
  export CFLAGS+=" -DIN_WINPTHREAD"

  if [[ ${MSYSTEM} == CLANG* ]]; then
    export CFLAGS+="  -Wno-error=tautological-constant-out-of-range-compare"
  fi

  CFLAGS+=" -Wno-int-conversion -Wno-error=unused-parameter -Wno-strict-prototypes" \
  MSYS2_ARG_CONV_EXCL=- \
  cmake \
      -GNinja \
      ${_extra_config[@]} \
      -DCMAKE_INSTALL_PREFIX=$MINGW_PREFIX \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DBUILD_TESTS=OFF \
      -DBUILD_TOOLS=OFF \
      -DBUILD_EXAMPLES=OFF \
      -DBUILD_STATIC_LIBS=ON \
      -DBUILD_SHARED_LIBS=ON \
      -DUSE_WINHELLO=ON \
      "../${_realname}-${pkgver}"

  cmake --build .
}

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

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