# Maintainer: wirx6 <wirx654@gmail.com>

_realname=aom
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.1
pkgrel=5
pkgdesc='AV1 codec library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://aomedia.org/"
license=('BSD-2-Clause')
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("perl"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-yasm"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://storage.googleapis.com/aom-releases/libaom-${pkgver}.tar.gz" # {,.asc}
        "fix-lib-dest.patch"
        "f19edfa.diff"
        "0003-cmake-fix-libname-import-library.patch")
sha256sums=('7cd5e8e469268c37241df93fe61557ea0dc46980ca21b37e786e92a3d0729276'
            '3a202005676f91b2b5ee5d7c10792f1930b204f36a96d7d5b893ef81d375b19d'
            '7dc44a08cb6c70f6a2fcd7c1310ac96da386186a27413b8c145644d3fa5b8054'
            'b95c9afc17fcc0f1726652976b412e74d8108d6b23a67703590e59cfa6428928')
#validpgpkeys=('B002F08B74A148DAA01F7123A48E86DB0B830498')
noextract=("libaom-${pkgver}.tar.gz")

prepare() {
  # Extract into a subdirectory
  (mkdir "${srcdir}/${_realname}" && cd "${srcdir}/${_realname}" && bsdtar -xf "${srcdir}/libaom-${pkgver}.tar.gz")

  cd "${srcdir}/${_realname}"

  patch -Np1 -i "${srcdir}"/fix-lib-dest.patch
  patch -Np1 -i "${srcdir}"/f19edfa.diff
  patch -Np1 -i "${srcdir}"/0003-cmake-fix-libname-import-library.patch
}

build() {
  cd "${srcdir}"/${_realname}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  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=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DENABLE_TESTS=OFF \
      ../${_realname}

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

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

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

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