# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Marcin Sielski <marcin.sielski@motorolasolutions.com>

_realname=gst-plugins-bad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-gst-plugins-bad-libs" "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad")
pkgver=1.18.4
pkgrel=8
pkgdesc="GStreamer Multimedia Framework Bad Plugins (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://gstreamer.freedesktop.org/"
license=("LGPL")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
  "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-shaderc"
  "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
  "${MINGW_PACKAGE_PREFIX}-gtk-doc"
  "${MINGW_PACKAGE_PREFIX}-aom"
  "${MINGW_PACKAGE_PREFIX}-bzip2"
  "${MINGW_PACKAGE_PREFIX}-cairo"
  "${MINGW_PACKAGE_PREFIX}-chromaprint"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-faad2"
  "${MINGW_PACKAGE_PREFIX}-faac"
  "${MINGW_PACKAGE_PREFIX}-fdk-aac"
  "${MINGW_PACKAGE_PREFIX}-fluidsynth"
  "${MINGW_PACKAGE_PREFIX}-gsm"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-lcms2"
  "${MINGW_PACKAGE_PREFIX}-libass"
  "${MINGW_PACKAGE_PREFIX}-libbs2b"
  "${MINGW_PACKAGE_PREFIX}-libdca"
  "${MINGW_PACKAGE_PREFIX}-libdvdnav"
  "${MINGW_PACKAGE_PREFIX}-libdvdread"
  "${MINGW_PACKAGE_PREFIX}-libexif"
  "${MINGW_PACKAGE_PREFIX}-libgme"
  "${MINGW_PACKAGE_PREFIX}-libjpeg"
  "${MINGW_PACKAGE_PREFIX}-libmodplug"
  "${MINGW_PACKAGE_PREFIX}-libmpeg2"
  "${MINGW_PACKAGE_PREFIX}-libnice"
  "${MINGW_PACKAGE_PREFIX}-librsvg"
  "${MINGW_PACKAGE_PREFIX}-libsndfile"
  "${MINGW_PACKAGE_PREFIX}-libsrtp"
  "${MINGW_PACKAGE_PREFIX}-libssh2"
  "${MINGW_PACKAGE_PREFIX}-libwebp"
  "${MINGW_PACKAGE_PREFIX}-libxml2"
  "${MINGW_PACKAGE_PREFIX}-libmicrodns"
  "${MINGW_PACKAGE_PREFIX}-libmpcdec"
  "${MINGW_PACKAGE_PREFIX}-nettle"
  "${MINGW_PACKAGE_PREFIX}-openal"
  "${MINGW_PACKAGE_PREFIX}-opencv"
  "${MINGW_PACKAGE_PREFIX}-openexr"
  "${MINGW_PACKAGE_PREFIX}-openh264"
  "${MINGW_PACKAGE_PREFIX}-openjpeg2"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-opus"
  "${MINGW_PACKAGE_PREFIX}-orc"
  "${MINGW_PACKAGE_PREFIX}-pango"
  "${MINGW_PACKAGE_PREFIX}-rtmpdump-git"
  "${MINGW_PACKAGE_PREFIX}-soundtouch"
  "${MINGW_PACKAGE_PREFIX}-spandsp"
  "${MINGW_PACKAGE_PREFIX}-srt"
  "${MINGW_PACKAGE_PREFIX}-vo-amrwbenc"
  "${MINGW_PACKAGE_PREFIX}-webrtc-audio-processing"
  "${MINGW_PACKAGE_PREFIX}-x265"
  "${MINGW_PACKAGE_PREFIX}-zbar"
)

source=(${url}/src/${_realname}/${_realname}-${pkgver}.tar.xz
        "0001-meson-fix-vulkan-detection.patch"
        "0002-fix-vulkan-mkenum.patch"
        "0003-enable-mediafoundation-plugin.patch")
sha256sums=('74e806bc5595b18c70e9ca93571e27e79dfb808e5d2e7967afa952b52e99c85f'
            'e038864a83bafc0e16f143f16a86215dd52621703c8272c580f43ab56d30110f'
            '5ca55fdfc4c5d10d2319f83dddac8130c2ffab67b1248d2233cdcb188d5a0fd3'
            '5c2c0f49c1afcee2e406078322cdac1be16bc832a92583152f9bc6aedaf526e6')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-meson-fix-vulkan-detection.patch"
  patch -Np1 -i "${srcdir}/0002-fix-vulkan-mkenum.patch"

  # https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1882
  patch -Np1 -i "${srcdir}/0003-enable-mediafoundation-plugin.patch"
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    --wrap-mode=nofallback \
    -Dauto_features=enabled \
    -Dpackage-origin='https://www.msys2.org' \
    -Dtests=disabled \
    -Ddoc=disabled \
    -Dexamples=disabled \
    -Dx11=disabled \
    -Dwayland=disabled \
    -Dbluez=disabled \
    -Ddvb=disabled \
    -Dfbdev=disabled \
    -Dipcpipeline=disabled \
    -Dkms=disabled \
    -Dmsdk=disabled \
    -Dopensles=disabled \
    -Dshm=disabled \
    -Dtinyalsa=disabled \
    -Duvch264=disabled \
    -Ddc1394=disabled \
    -Ddirectfb=disabled \
    -Dflite=disabled \
    -Dkate=disabled \
    -Dladspa=disabled \
    -Dlibde265=disabled \
    -Dlibmms=disabled \
    -Dlv2=disabled \
    -Dmpeg2enc=disabled \
    -Dmplex=disabled \
    -Dneon=disabled \
    -Dofa=disabled \
    -Dopenmpt=disabled \
    -Dopenni2=disabled \
    -Dsbc=disabled \
    -Dsctp=disabled \
    -Dteletext=disabled \
    -Dvoaacenc=disabled \
    -Dwildmidi=disabled \
    -Dwpe=disabled \
    -Diqa=disabled \
    -Dmagicleap=disabled \
    -Dv4l2codecs=disabled \
    -Dva=disabled \
    -Dwasapi2=disabled \
    -Davtp=disabled \
    -Dsvthevcenc=disabled \
    -Dzxing=disabled \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/ninja.exe
}

package_gst-plugins-bad-libs() {
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
    "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
  )
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/ninja.exe install

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/gstreamer*/*.a

  # things moved to the plugins package later
  mkdir -p _plugins/lib
  mkdir -p _plugins/bin
  mv "${pkgdir}${MINGW_PREFIX}/lib/gstreamer-1.0" _plugins/lib
  mv "${pkgdir}${MINGW_PREFIX}/bin/libgstopencv-1.0-0.dll"  _plugins/bin
}

package_gst-plugins-bad() {
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad-libs=$pkgver"
    "${MINGW_PACKAGE_PREFIX}-aom"
    "${MINGW_PACKAGE_PREFIX}-bzip2"
    "${MINGW_PACKAGE_PREFIX}-cairo"
    "${MINGW_PACKAGE_PREFIX}-chromaprint"
    "${MINGW_PACKAGE_PREFIX}-curl"
    "${MINGW_PACKAGE_PREFIX}-faad2"
    "${MINGW_PACKAGE_PREFIX}-faac"
    "${MINGW_PACKAGE_PREFIX}-fdk-aac"
    "${MINGW_PACKAGE_PREFIX}-fluidsynth"
    "${MINGW_PACKAGE_PREFIX}-gsm"
    "${MINGW_PACKAGE_PREFIX}-gtk3"
    "${MINGW_PACKAGE_PREFIX}-lcms2"
    "${MINGW_PACKAGE_PREFIX}-libass"
    "${MINGW_PACKAGE_PREFIX}-libbs2b"
    "${MINGW_PACKAGE_PREFIX}-libdca"
    "${MINGW_PACKAGE_PREFIX}-libdvdnav"
    "${MINGW_PACKAGE_PREFIX}-libdvdread"
    "${MINGW_PACKAGE_PREFIX}-libexif"
    "${MINGW_PACKAGE_PREFIX}-libgme"
    "${MINGW_PACKAGE_PREFIX}-libjpeg"
    "${MINGW_PACKAGE_PREFIX}-libmodplug"
    "${MINGW_PACKAGE_PREFIX}-libmpeg2"
    "${MINGW_PACKAGE_PREFIX}-libnice"
    "${MINGW_PACKAGE_PREFIX}-librsvg"
    "${MINGW_PACKAGE_PREFIX}-libsndfile"
    "${MINGW_PACKAGE_PREFIX}-libsrtp"
    "${MINGW_PACKAGE_PREFIX}-libssh2"
    "${MINGW_PACKAGE_PREFIX}-libwebp"
    "${MINGW_PACKAGE_PREFIX}-libxml2"
    "${MINGW_PACKAGE_PREFIX}-libmicrodns"
    "${MINGW_PACKAGE_PREFIX}-libmpcdec"
    "${MINGW_PACKAGE_PREFIX}-nettle"
    "${MINGW_PACKAGE_PREFIX}-openal"
    "${MINGW_PACKAGE_PREFIX}-opencv"
    "${MINGW_PACKAGE_PREFIX}-openexr"
    "${MINGW_PACKAGE_PREFIX}-openh264"
    "${MINGW_PACKAGE_PREFIX}-openjpeg2"
    "${MINGW_PACKAGE_PREFIX}-openssl"
    "${MINGW_PACKAGE_PREFIX}-opus"
    "${MINGW_PACKAGE_PREFIX}-orc"
    "${MINGW_PACKAGE_PREFIX}-pango"
    "${MINGW_PACKAGE_PREFIX}-rtmpdump-git"
    "${MINGW_PACKAGE_PREFIX}-soundtouch"
    "${MINGW_PACKAGE_PREFIX}-spandsp"
    "${MINGW_PACKAGE_PREFIX}-srt"
    "${MINGW_PACKAGE_PREFIX}-vo-amrwbenc"
    "${MINGW_PACKAGE_PREFIX}-webrtc-audio-processing"
    "${MINGW_PACKAGE_PREFIX}-x265"
    "${MINGW_PACKAGE_PREFIX}-zbar"
  )
  cd "${srcdir}/build-${MINGW_CHOST}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  mv _plugins/* "${pkgdir}${MINGW_PREFIX}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;