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

_realname=nghttp3
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.12.0
pkgrel=1
pkgdesc="HTTP/3 library written in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_repository_url='https://github.com/ngtcp2/nghttp3'
url='https://nghttp2.org/nghttp3/'
license=('spdx:MIT')
checkdepends=("${MINGW_PACKAGE_PREFIX}-cunit")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
source=("https://github.com/ngtcp2/nghttp3/releases/download/v${pkgver}/nghttp3-${pkgver}.tar.xz"{,.asc}
        "001-fix-pc-prefix.patch"
        "002-nghttp3-pkgconfig-add-cflags-private.patch")
sha256sums=('6ca1e523b7edd75c02502f2bcf961125c25577e29405479016589c5da48fc43d'
            'SKIP'
            '08559edf597300d34b258c5dab6d16237c6493976e7b4d45a818e0619d21665d'
            '68c831018eb79bc3b43a8fb6a7d013a617dbe2ba7d7971b0993b3cf27107e614')
validpgpkeys=('F4F3B91474D1EB29889BD0EF7E8403D5D673C366') # Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>

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

  patch -Np1 -i "$srcdir/001-fix-pc-prefix.patch"
  patch -Np1 -i "$srcdir/002-nghttp3-pkgconfig-add-cflags-private.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 "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_SHARED_LIB=ON \
      -DENABLE_STATIC_LIB=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DENABLE_LIB_ONLY=ON \
      ../"${_realname}-${pkgver}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

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