# Maintainer: Diego Sogari <diego.sogari@falker.com.br>
# Contributor: Dirk Stolle

_realname=uriparser
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.9
pkgrel=1
pkgdesc="RFC 3986 URI parsing and processing libary (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://uriparser.github.io/"
msys2_changelog_url='https://github.com/uriparser/uriparser/blob/master/ChangeLog'
msys2_references=(
  "archlinux: uriparser"
  "cpe: cpe:/a:uriparser_project:uriparser"
  "gentoo: dev-libs/uriparser"
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-gtest")
source=("https://github.com/${_realname}/${_realname}/releases/download/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.xz")
sha256sums=('85645df1bb220a3867c42f8adeba4f6a4b2fdc1563e91ad7d9f92d14d3dd377d')

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

  # Shared
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DURIPARSER_BUILD_TESTS=OFF \
      -DURIPARSER_BUILD_DOCS=OFF \
      ../${_realname}-${pkgver}

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DURIPARSER_BUILD_TOOLS=OFF \
      -DURIPARSER_BUILD_TESTS=OFF \
      -DURIPARSER_BUILD_DOCS=OFF \
      ../${_realname}-${pkgver}

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

check() {
  cd "${srcdir}/build-${MSYSTEM}-shared"
  ${MINGW_PREFIX}/bin/cmake ../${_realname}-${pkgver} -DURIPARSER_BUILD_TESTS=ON
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest || true
}

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

  # Static
  cd "${srcdir}/build-${MSYSTEM}-static"
  cp liburiparser.a "${pkgdir}"${MINGW_PREFIX}/lib/

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.BSD-3-Clause" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
