# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=cmakerc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.1
pkgrel=3
pkgdesc="A Standalone CMake-Based C++ Resource Compiler (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/vector-of-bool/cmrc'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cmake")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${url}/archive/${pkgver}/cmrc-${pkgver}.tar.gz"
        '0001-add-install-rules.patch'
        "0002-increase-cmake-min-req.patch::${url}/commit/91c9522ee59654e2f4d9701947a4600a4436d076.patch")
sha256sums=('edad5faaa0bea1df124b5e8cb00bf0adbd2faeccecd3b5c146796cbcb8b5b71b'
            '18dad7f3572cbc30e0a76134b30ec676c7a867ec860b13205f205c000cb169d4'
            'dc43cd8fe3b4727eff7a1c19414459aa0658e7db8fb59fea49842e41333fd9fd')

prepare() {
  cd "${srcdir}/cmrc-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-add-install-rules.patch"
  # https://github.com/vector-of-bool/cmrc/pull/48
  patch -Np1 -i "${srcdir}/0002-increase-cmake-min-req.patch"
}

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

  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=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -DBUILD_TESTS=ON \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      ../cmrc-${pkgver}

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

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