# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libavif
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.0
pkgrel=5
pkgdesc="Library for encoding and decoding .avif files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/AOMediaCodec/libavif"
msys2_references=(
  'anitya: 178015'
  "cpe: cpe:/a:aomedia:libavif"
)
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         $([[ ${CARCH} == i686 ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-rav1e" \
           "${MINGW_PACKAGE_PREFIX}-svt-av1" )
         "${MINGW_PACKAGE_PREFIX}-libyuv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsharpyuv"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "git")
checkdepends=("${MINGW_PACKAGE_PREFIX}-gtest")
source=("https://github.com/AOMediaCodec/libavif/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha512sums=('a411579c851b7c46ddbd93c3549295e0901d822c817f5378b2c75b6a4f16eba1dffdf611bd1789dedeba776e701690a7437f9caeb9eb6dc382fc64935949dbf4')

prepare() {
  cd "${_realname}-${pkgver}"
}

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

  if [[ ${CARCH} == i686 ]]; then
    extra_config+=("-DAVIF_CODEC_RAV1E=OFF" "-DAVIF_CODEC_SVT=OFF")
  else
    extra_config+=("-DAVIF_CODEC_RAV1E=SYSTEM" "-DAVIF_CODEC_SVT=SYSTEM")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DBUILD_SHARED_LIBS=OFF \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DAVIF_CODEC_AOM=SYSTEM \
    -DAVIF_CODEC_DAV1D=SYSTEM \
    -DAVIF_LIBSHARPYUV=SYSTEM \
    -DAVIF_LIBXML2=SYSTEM \
    -S "${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}-static"

  ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${extra_config[@]}" \
    -DAVIF_BUILD_APPS=ON \
    -DAVIF_CODEC_AOM=SYSTEM \
    -DAVIF_CODEC_DAV1D=SYSTEM \
    -DAVIF_BUILD_GDK_PIXBUF=ON \
    -DAVIF_LIBSHARPYUV=SYSTEM \
    -DAVIF_LIBXML2=SYSTEM \
    -S "${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}-shared"

  ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-shared"
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}-shared
  ${MINGW_PREFIX}/bin/cmake -DAVIF_BUILD_TESTS=ON ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest ./ || true
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-${MSYSTEM}-shared"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/README.md \
    "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/README.md
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
