# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=xavs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.55
pkgrel=3
pkgdesc="XAVS is to implement high quality encoder and decoder of the Audio Video Standard of China (AVS). (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://xavs.sourceforge.io/"
license=('spdx:GPL-2.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-yasm")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
source=("https://github.com/OpenMandrivaAssociation/xavs/raw/master/xavs-${pkgver}.tar.xz"
        "0001-xavs-0.1.55-fix-dynamic-build.patch"
        "0002-xavs-0.1.55-fix-asm.patch"
        "0003-aarch64.patch")
sha256sums=('178bf04ffc5edd9acfa8188acbeeab16e24e4e43f4465729e8619f844e5901f2'
            'dd717ff5096ea2ffd4c4793461269eee089e54e7db1184669d8f00c0fc67f1a4'
            '1559e0dfb21ee6f67dbd996ae0bb0c67987706db3144199202890de37cb7d8b4'
            'c8fb0a7f36a8e199025d8fdec4e9b5e3b203c8df542eb875030fec3950196181')

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

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

  apply_patch_with_msg \
    "0001-xavs-0.1.55-fix-dynamic-build.patch" \
    "0002-xavs-0.1.55-fix-asm.patch" \
    "0003-aarch64.patch"
}

build() {
  [[ -d "build-${MSYSTEM}" ]] && rm -rf "build-${MSYSTEM}"
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --disable-asm \
    --enable-shared

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
}
