# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=cppwinrt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.250303.1
_windows_rs_ver=70
pkgrel=1
pkgdesc="C++ language projection for Windows Runtime (WinRT) APIs (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://aka.ms/cppwinrt'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/microsoft/cppwinrt/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/microsoft/windows-rs/archive/${_windows_rs_ver}/windows-rs-${_windows_rs_ver}.tar.gz")
sha256sums=('da75b7047f4615a3db582ed8c75ee327f228b05bb47750621593594d2d0459e1'
            'c637c399d3dbd5d01235dc3fea9055237e4bf6ea7a3cc7b5f8cdda4cd58f68f7')

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 -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DCPPWINRT_BUILD_VERSION="${pkgver}" \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}

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

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

  "${MINGW_PREFIX}"/bin/cmake.exe -DBUILD_TESTING=ON ../${_realname}-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  "${MINGW_PREFIX}"/bin/ctest.exe . || warning "Tests failed"
}

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

  install -Dm755 cppwinrt.exe "${pkgdir}${MINGW_PREFIX}/bin/cppwinrt.exe"

  echo "SPDX-License-Identifier: MIT OR Apache-2.0
SPDX-FileCopyrightText: Copyright (c) Microsoft Corporation

Generated using .winmd files from https://github.com/microsoft/windows-rs
" > license.txt

  ./cppwinrt.exe -license license.txt \
    -input "${srcdir}/windows-rs-${_windows_rs_ver}/crates/libs/bindgen/default" \
    -output "${pkgdir}${MINGW_PREFIX}/include"

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