# Contributor: Edward E. <develinthedetail@gmail.com>

_realname=libheif
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.20.2
pkgrel=6
pkgdesc="HEIF image decoder/encoder library and tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/strukturag/libheif"
msys2_references=(
  "cpe: cpe:/a:struktur:libheif"
)
license=('spdx:LGPL-3.0 AND MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-kvazaar"
         "${MINGW_PACKAGE_PREFIX}-libde265"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libsharpyuv"
         "${MINGW_PACKAGE_PREFIX}-openh264"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         $([[ ${CARCH} == i686 ]] || echo \
            "${MINGW_PACKAGE_PREFIX}-openjph" \
            "${MINGW_PACKAGE_PREFIX}-rav1e" \
            "${MINGW_PACKAGE_PREFIX}-svt-av1" )
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://github.com/strukturag/libheif/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('68ac9084243004e0ef3633f184eeae85d615fe7e4444373a0a21cebccae9d12a')

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} == i686 ]]; then
    extra_config+=("-DWITH_OPENJPH_ENCODER=OFF" "-DWITH_RAV1E=OFF" "-DWITH_SvtEnc=OFF")
  else
    extra_config+=("-DWITH_OPENJPH_ENCODER=ON" "-DWITH_RAV1E=ON" "-DWITH_SvtEnc=ON")
  fi

  # Keep "-DX265_API_IMPORTS" flag due to https://github.com/strukturag/libheif/issues/357
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      -DENABLE_PLUGIN_LOADING=OFF \
      -DWITH_HEADER_COMPRESSION=ON \
      -DWITH_KVAZAAR=ON \
      -DWITH_DAV1D=ON \
      -DWITH_JPEG_DECODER=ON \
      -DWITH_JPEG_ENCODER=ON \
      -DWITH_OpenJPEG_DECODER=ON \
      -DWITH_OpenJPEG_ENCODER=ON \
      -DX265_CFLAGS="-DX265_API_IMPORTS" \
      ../${_realname}-${pkgver}

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

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

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

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