# Maintainer: Marcin Sielski <marcin.sielski@motorolasolutions.com>

_realname=openh264
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.6.0
pkgrel=1
pkgdesc="Library for encoding/decoding H264/AVC video streams (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.openh264.org/"
msys2_repository_url="https://github.com/cisco/openh264"
msys2_references=(
  "cpe: cpe:/a:cisco:openh264"
)
license=('spdx:BSD-2-Clause')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
   $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-nasm")
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
source=(
  "https://github.com/cisco/openh264/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
  "0001-openh264-install-executables.patch"
)
sha256sums=('558544ad358283a7ab2930d69a9ceddf913f4a51ee9bf1bfb9e377322af81a69'
            '0b1246577b257560b6fae782dd93eabeedcb8d9c87c20d6d7732b79ba14c38d6')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-openh264-install-executables.patch"
}

build() {
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/meson.exe setup \
      --prefix="${MINGW_PREFIX}" \
      --default-library=static \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      -Dtests=disabled \
      build-${MSYSTEM}-static \
      ${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson.exe compile -C build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/meson.exe setup \
      --prefix="${MINGW_PREFIX}" \
      --default-library=shared \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      -Dtests=disabled \
      build-${MSYSTEM}-shared \
      ${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson.exe compile -C build-${MSYSTEM}-shared
}

package() {
  ${MINGW_PREFIX}/bin/meson.exe install -C build-${MSYSTEM}-static --destdir "${pkgdir}"
  ${MINGW_PREFIX}/bin/meson.exe install -C build-${MSYSTEM}-shared --destdir "${pkgdir}"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
