# Maintainer: Antoine Martin <totaam@xpra.org>

_realname=libyuv
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname})
provides=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
pkgver=1787.r2266.eb6e7bb6
pkgrel=1
pkgdesc="YUV conversion and scaling library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://chromium.googlesource.com/libyuv/libyuv"
license=('BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-libjpeg")
_commit="eb6e7bb63738e29efd82ea3cf2a115238a89fa51"
source=("${_realname}::git+https://chromium.googlesource.com/${_realname}/${_realname}#commit=${_commit}"
        "libyuv.pc.in"
        "libyuv-jpeg.patch"
        "cmake-mingw.patch"
        "pkgconfig-cmake.patch")
options=('strip' 'staticlibs')
sha256sums=('SKIP'
            '86aaa22252e033e62931e7c3fe3a6be4143a8ca532f95c1346b1e6b278c1947f'
            '7ad4df4a33ce7623bbea336ade74b4fbfb6a750973dba359e287b6bfcbfa7f09'
            '3897bfb1be028bacf065906a5032811bca0203527dd0348e8a7d264e43999957'
            'b7fc89b8a8da6b3dd2165b9be3408545b3e6ec6e7cae34a50843c5a24f8313c0')

pkgver() {
  cd "${srcdir}"/${_realname}
  local VERSION=$(cat include/libyuv/version.h | grep "#define LIBYUV_VERSION" | awk '{print $3}')

  printf "%s.r%s.%s" "${VERSION}" "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd "${srcdir}"/${_realname}
  cp "${srcdir}"/libyuv.pc.in "${srcdir}"/${_realname}
  patch -p1 -i "${srcdir}"/libyuv-jpeg.patch
  patch -p1 -i "${srcdir}"/cmake-mingw.patch
  patch -p1 -i "${srcdir}"/pkgconfig-cmake.patch
}

build() {
  [[ -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 \
      ../${_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"
}
