# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=svt-jpeg-xs
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.9.0
pkgrel=1
pkgdesc='An implementation of the JPEG XS (ISO/IEC 21122) codec (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://github.com/OpenVisualCloud/SVT-JPEG-XS'
msys2_repository_url="https://github.com/OpenVisualCloud/SVT-JPEG-XS"
license=('spdx:BSD-2-Clause-Patent')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-yasm")
source=("https://github.com/OpenVisualCloud/SVT-JPEG-XS/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-pkgconfig.patch"
        "0002-fix-cmake-install-dest.patch")
sha256sums=('ff5dfb3b98348a39049da4fd3062d3391cbadec4c4b61825a9f3435ca671effa'
            '968d5aa2262beafe7a7d2f28247e1d7cf6426d27ccd3a84ecf306691c2ccb14d'
            'ca0db02edac5508fc9c42e9dfe8974af08eb4fb816a3ff829e35a157185f5cc0')

prepare() {
  cd "SVT-JPEG-XS-${pkgver}"
  patch -p1 -i "${srcdir}/0001-fix-pkgconfig.patch"
  patch -p1 -i "${srcdir}/0002-fix-cmake-install-dest.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 -B build -S "SVT-JPEG-XS-${pkgver}" \
    -G 'Ninja' \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DCMAKE_POLICY_VERSION_MINIMUM='3.5.0' \
    -DNATIVE=OFF \
    -Wno-dev

  cmake --build build
}

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

  install -Dm644 "SVT-JPEG-XS-${pkgver}/LICENSE.md" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
