# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: Dirk Stolle

_realname=directxtex
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
# Version from CMakeLists.txt
pkgver=2.0.8
_tag=jul2025
pkgrel=1
pkgdesc="DirectXTex texture processing library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://go.microsoft.com/fwlink/?LinkId=248926'
msys2_repository_url="https://github.com/microsoft/DirectXTex"
msys2_changelog_url='https://github.com/microsoft/DirectXTex/blob/main/CHANGELOG.md'
msys2_references=(
  'aur: mingw-w64-directxtex'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-directx-headers"
         "${MINGW_PACKAGE_PREFIX}-directxmath"
         "${MINGW_PACKAGE_PREFIX}-openexr")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-efxc2")
source=("https://github.com/microsoft/DirectXTex/archive/$_tag/${_realname}-${_tag}.tar.gz"
        '001-missing-compression-enumeration.patch')
sha256sums=('25356ef10e0a2ba20fe344c337a6db6c7220a19971fc266831f7a505d88ece5d'
            '8ee961d7340eeccaddf53b1848d177ec370ada5e27fd9ff4f17995b2c3a76f9d')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
    cd "${srcdir}"/DirectXTex-${_tag}

    # Currently, the wincodec.h header in the headers-git package is missing the
    # WICHeifCompressionOption enumeration (<https://learn.microsoft.com/en-us/windows/win32/api/wincodec/ne-wincodec-wicheifcompressionoption>)
    # and thus the WICHeifCompressionNone value, so we always have to use the
    # literal value instead.
    apply_patch_with_msg \
      001-missing-compression-enumeration.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

  if [ "${CARCH}" == "x86_64" ]; then
    extra_config+=("-DDIRECTX_ARCH=x64")
  fi
  if [ "${CARCH}" == "aarch64" ]; then
    extra_config+=("-DDIRECTX_ARCH=arm64")
  fi

  #DBUILD_DX11 requires fxc.exe but that's not available.
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_DX11=ON \
      -DBUILD_DX12=ON \
      -DBUILD_TOOLS=ON \
      -DENABLE_OPENEXR_SUPPORT=ON \
      "${extra_config[@]}" \
      ../${_realname}-${_tag}

  "${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}"/${_realname}-${_tag}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
