# Maintainer: yumetodo <yume-wikijp@live.jp>

_realname=skyr-url
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.13.0
pkgrel=3
pkgdesc="A C++ library that implements the WhatWG URL specification"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/cpp-netlib/url'
license=('BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-tl-expected")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-catch"
             "${MINGW_PACKAGE_PREFIX}-fmt"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-range-v3")
options=('strip' '!debug' 'staticlibs')
source=("url-v${pkgver}.tar.gz::https://github.com/cpp-netlib/url/archive/v${pkgver}.tar.gz")
sha256sums=('0c9e594f09a9abda5815b07b548ac520b2fc1044fc74b589fc4c5bd18ea4330c')

prepare() {
  cd "url-${pkgver}"
  # patch catch.hpp path to catch2/catch.hpp
  find -type f -exec sed -i 's/<catch.hpp>/<catch2\/catch.hpp>/g' {} \;
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

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

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_config+=("-Dskyr_ENABLE_FILESYSTEM_FUNCTIONS=OFF")
  fi

  # Tests takes too much time
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -Dskyr_BUILD_TESTS=OFF \
    "../url-${pkgver}"

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

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

  install -Dm644 "${srcdir}/url-${pkgver}/LICENSE_1_0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE_1_0.txt"
}
