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

_realname=rav1e
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.4.1
pkgrel=1
pkgdesc='An AV1 encoder focused on speed and safety (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url=https://github.com/xiph/rav1e/
license=(BSD)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-c")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/xiph/rav1e/archive/v${pkgver}.tar.gz")
sha256sums=('b0be59435a40e03b973ecc551ca7e632e03190b5a20f944818afa3c2ecf4852d')

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

  cargo fetch \
    --locked
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo build \
    --release \
    --frozen \
    --offline

  cargo capi build \
    --release \
    --frozen \
    --library-type=cdylib
}

check() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo test \
    --release \
    --frozen
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo install \
    --frozen \
    --offline \
    --no-track \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

  MSYS2_ARG_CONV_EXCL="--prefix=;" \
  cargo capi install \
    --release \
    --frozen \
    --library-type=cdylib \
    --prefix="${MINGW_PREFIX}" \
    --destdir="${pkgdir}"

  # Workaround for import lib
  mv ${pkgdir}${MINGW_PREFIX}/lib/{,lib}rav1e.dll.a
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.def

  install -Dm644 LICENSE PATENTS -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
