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

_realname=ngtcp2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.19.0
pkgrel=1
pkgdesc="An effort to implement IETF QUIC protocol (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_repository_url='https://github.com/ngtcp2/ngtcp2'
url='https://nghttp2.org/ngtcp2/'
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
license=('spdx:MIT')
source=("https://github.com/ngtcp2/ngtcp2/releases/download/v${pkgver}/ngtcp2-${pkgver}.tar.xz"{,.asc}
        "001-fix-pc-prefix.patch"
        "002-ngtcp2-pkgconfig-add-cflags-private.patch")
sha256sums=('f11f7da5065f2298f8b5f079a11f1a6f72389271b8dedd893c8eb26aba94bce9'
            'SKIP'
            'cdbb6fad1afc1eac3f3bca72cd83e49684439c02b0b74fd8841b9d1523ab86de'
            '11d30ddfcf358e4a10f416737776fd268395643c84a90f43f798492b42dd4680')
validpgpkeys=(
'F4F3B91474D1EB29889BD0EF7E8403D5D673C366' # Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
'516B622918D15C478AB1EA3A5339A2BE82E07DEC' # Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
)

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

  patch -Np1 -i "$srcdir/001-fix-pc-prefix.patch"
  patch -Np1 -i "$srcdir/002-ngtcp2-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-shared-${MSYSTEM}" && cd "${srcdir}/build-shared-${MSYSTEM}"

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

  cmake --build .

  mkdir -p "${srcdir}/build-static-${MSYSTEM}" && cd "${srcdir}/build-static-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_SHARED_LIB=OFF \
      -DENABLE_STATIC_LIB=ON \
      -DENABLE_GNUTLS=ON \
      -DENABLE_OPENSSL=ON \
      ../${_realname}-${pkgver}

  cmake --build .
}

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

  cd "${srcdir}/build-shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .

  # Remove on next update
  cp "${pkgdir}${MINGW_PREFIX}"/bin/libngtcp2_crypto_ossl-0.dll "${pkgdir}${MINGW_PREFIX}"/bin/libngtcp2_crypto_ossl.dll

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