# 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"
         "${MINGW_PACKAGE_PREFIX}-gst-plugin-opencv")
pkgver=1.26.7
pkgrel=1
pkgdesc="GStreamer Multimedia Framework Bad Plugins (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gstreamer.freedesktop.org/"
msys2_references=(
  "cpe: cpe:/a:freedesktop:gst-plugins-bad"
)
license=("spdx:LGPL-2.1-or-later")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
  "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
  "${MINGW_PACKAGE_PREFIX}-shaderc"
  "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
  "${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}-directxmath"
  "${MINGW_PACKAGE_PREFIX}-faad2"
  "${MINGW_PACKAGE_PREFIX}-faac"
  "${MINGW_PACKAGE_PREFIX}-fdk-aac"
  "${MINGW_PACKAGE_PREFIX}-fluidsynth"
  "${MINGW_PACKAGE_PREFIX}-fxc2"
  "${MINGW_PACKAGE_PREFIX}-gsm"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-imath"
  "${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}-liblc3"
  "${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"
  "${MINGW_PACKAGE_PREFIX}-soundtouch"
  "${MINGW_PACKAGE_PREFIX}-spandsp"
  "${MINGW_PACKAGE_PREFIX}-srt"
  "${MINGW_PACKAGE_PREFIX}-svt-av1"
  "${MINGW_PACKAGE_PREFIX}-vo-amrwbenc"
  "${MINGW_PACKAGE_PREFIX}-webrtc-audio-processing-1"
  "${MINGW_PACKAGE_PREFIX}-x265"
  "${MINGW_PACKAGE_PREFIX}-zbar"
  "${MINGW_PACKAGE_PREFIX}-directx-headers"
)
source=("${url}/src/${_realname}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "0001-meson-fix-vulkan.patch"
        "0002-fix-vulkan-mkenum.patch"
        "0004-check-d3d11-header.patch")
sha256sums=('a0ab17dddd4c029ecd7a423c30badd5a3c7599ea42707016d1d57545f5723ccf'
            'SKIP'
            'f49da7b178541bf56c79ebb1014c3903c1d5edfec683b59bb5621f31a697bec3'
            '5ca55fdfc4c5d10d2319f83dddac8130c2ffab67b1248d2233cdcb188d5a0fd3'
            '547248d0ea092dee103c452f5d12fc0302237244b3184d00bcb3afdac2476470')
validpgpkeys=('D637032E45B8C6585B9456565D2EEE6F6F349D7C') # Tim Müller <tim@gstreamer-foundation.org>

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-meson-fix-vulkan.patch \
    0002-fix-vulkan-mkenum.patch

  apply_patch_with_msg \
    0004-check-d3d11-header.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  if [[ ${CC} == gcc ]]; then
    CXXFLAGS+=" -fext-numeric-literals"
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --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 \
    -Dladspa=disabled \
    -Dlibde265=disabled \
    -Dlv2=disabled \
    -Dmpeg2enc=disabled \
    -Dmplex=disabled \
    -Dneon=disabled \
    -Dopenmpt=disabled \
    -Dopenni2=disabled \
    -Dsbc=disabled \
    -Dsctp=enabled \
    -Dteletext=disabled \
    -Dvoaacenc=disabled \
    -Dwildmidi=disabled \
    -Dwpe=disabled \
    -Diqa=disabled \
    -Dmagicleap=disabled \
    -Dv4l2codecs=disabled \
    -Dva=disabled \
    -Dwasapi2=disabled \
    -Davtp=disabled \
    -Dsvthevcenc=disabled \
    -Dzxing=disabled \
    -Dasio=disabled \
    -Dgpl=enabled \
    -Dgs=disabled \
    -Disac=disabled \
    -Dldac=disabled \
    -Donnx=disabled \
    -Dopenaptx=disabled \
    -Dqroverlay=disabled \
    -Ddirectshow=disabled \
    -Dwic=disabled \
    -Dgtk3=disabled \
    -Dmediafoundation=disabled \
    -Dunixfd=disabled \
    -Daja=disabled \
    -Dd3d11-wgc=disabled \
    -Duvcgadget=disabled \
    -Dlc3=enabled \
    -Dqt6d3d11=disabled \
    -Dsvtav1=enabled \
    -Dopencv=enabled \
    -Dwebrtcdsp=enabled \
    -Dandroidmedia=disabled \
    -Dwebview2=disabled \
    -Dd3d12-wgc=disabled \
    -Dlcevcdecoder=disabled \
    -Dlcevcencoder=disabled \
    -Dnvcomp=disabled \
    -Dsvtjpegxs=disabled \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

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

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

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

  # for gst-plugin-opencv
  mkdir -p plugin-opencv/bin/
  mkdir -p plugin-opencv/include/gstreamer-1.0/gst/
  mkdir -p plugin-opencv/lib/gstreamer-1.0/
  mv "${pkgdir}${MINGW_PREFIX}/bin/libgstopencv-1.0-0.dll" plugin-opencv/bin/
  mv "${pkgdir}${MINGW_PREFIX}/include/gstreamer-1.0/gst/opencv" plugin-opencv/include/gstreamer-1.0/gst/
  mv "${pkgdir}${MINGW_PREFIX}/lib/libgstopencv-1.0.dll.a" plugin-opencv/lib/
  mv "${pkgdir}${MINGW_PREFIX}/lib/gstreamer-1.0/libgstopencv.dll" plugin-opencv/lib/gstreamer-1.0/

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

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}-imath"
    "${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}-liblc3"
    "${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}-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"
    "${MINGW_PACKAGE_PREFIX}-soundtouch"
    "${MINGW_PACKAGE_PREFIX}-spandsp"
    "${MINGW_PACKAGE_PREFIX}-srt"
    "${MINGW_PACKAGE_PREFIX}-svt-av1"
    "${MINGW_PACKAGE_PREFIX}-vo-amrwbenc"
    "${MINGW_PACKAGE_PREFIX}-webrtc-audio-processing-1"
    "${MINGW_PACKAGE_PREFIX}-x265"
    "${MINGW_PACKAGE_PREFIX}-zbar"
  )
  cd "${srcdir}/build-${MSYSTEM}"

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

package_gst-plugin-opencv() {
  pkgdesc+=" (opencv plugin)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
    "${MINGW_PACKAGE_PREFIX}-opencv"
  )

  cd "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  mv plugin-opencv/* "${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;
