# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=scnlib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.0.1
pkgrel=1
pkgdesc="scanf for modern C++. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.scnlib.dev'
msys2_repository_url="https://github.com/eliaskosunen/scnlib"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-fast_float")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-gtest")
source=("https://github.com/eliaskosunen/scnlib/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-Fix-incompatibility-with-newer-fast_float.patch::https://github.com/eliaskosunen/scnlib/commit/144a590f.patch")
sha256sums=('ece17b26840894cc57a7127138fe4540929adcb297524dec02c490c233ff46a7'
            '55a3d94e2241c5268679ae2fe7fe10ee4a00754143d80d72da9d1286dcab5392')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/001-Fix-incompatibility-with-newer-fast_float.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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DSCN_USE_EXTERNAL_FAST_FLOAT=ON \
      -DSCN_REGEX_BACKEND="std" \
      -DSCN_INSTALL=ON \
      -DSCN_PEDANTIC=OFF \
      -DSCN_WERROR=OFF \
      -DSCN_BENCHMARKS=OFF \
      -DSCN_BENCHMARKS_BUILDTIME=OFF \
      -DSCN_BENCHMARKS_BINARYSIZE=OFF \
      -DSCN_DOCS=OFF \
      -DSCN_EXAMPLES=OFF \
      -DSCN_TESTS=OFF \
      -DBUILD_TESTING=OFF \
      -DSCN_FUZZING=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake \
    -DBUILD_TESTING=ON \
    -DSCN_TESTS=ON \
    -DSCN_USE_EXTERNAL_GTEST=ON \
    -S "${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}"
  cmake --build "build-${MSYSTEM}"

  PATH="${srcdir}"/build-${MSYSTEM}:$PATH \
  ctest --output-on-failure --test-dir "build-${MSYSTEM}" || true
}

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

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