# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Christian Hesse <mail@eworm.de>
# Maintainer: Robin Candau <antiz@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Eivind Uggedal <eivind@uggedal.com>

pkgname=mpv
epoch=1
pkgver=0.38.0
pkgrel=8
pkgdesc='a free, open source, and cross-platform media player'
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=('GPL-2.0-or-later AND LGPL-2.1-or-later')
url='https://mpv.io/'
depends=('alsa-lib' 'desktop-file-utils' 'ffmpeg' 'glibc' 'hicolor-icon-theme'
         'jack' 'lcms2' 'libarchive' 'libass' 'libbluray' 'libcdio'
         'libcdio-paranoia' 'libdrm' 'libdvdnav' 'libdvdread' 'libegl' 'libgl'
         'libglvnd' 'libjpeg-turbo' 'libplacebo' 'libpulse' 'libsixel' 'libva'
         'libvdpau' 'libx11' 'libxext' 'libxkbcommon' 'libxpresent' 'libxrandr'
         'libxss' 'libxv' 'mesa' 'mujs' 'libpipewire' 'rubberband'
         'openal' 'uchardet' 'vapoursynth' 'vulkan-icd-loader' 'wayland' 'zlib')
depends_powerpc=('luajit')
depends_powerpc64=('lua')
depends_powerpc64le=('luajit')
depends_riscv64=('luajit')
depends_x86_64=('luajit')
makedepends=('git' 'meson' 'python-docutils' 'ladspa' 'wayland-protocols'
             'ffnvcodec-headers' 'vulkan-headers')
optdepends=('yt-dlp: for video-sharing websites playback')
provides=('libmpv.so')
options=('!emptydirs')
validpgpkeys=('145077D82501AA20152CACCE8D769208D5E31419') # sfan5 <sfan5@live.de>
source=("git+https://github.com/mpv-player/mpv.git#tag=v${pkgver}?signed"
        "dynamically_generate_desktop_file_protocols.patch")
sha256sums=('b37b91d0ae4518594b22338ebe979f13bdc56b6fa26d589e5c3f7b96a31bab3d'
            '88acf97cbc8e0fe745f09bd0bd7f65e0437adcb549dadf3588fd0724d01298e9')

prepare() {
  cd "${pkgname}"
  # Patch to dynamically generate protocols in the mpv.desktop file
  # Taken from https://github.com/mpv-player/mpv/pull/14145
  # Fixes:
  # - https://github.com/mpv-player/mpv/issues/8731
  # - https://github.com/mpv-player/mpv/issues/14124
  # - https://gitlab.archlinux.org/archlinux/packaging/packages/mpv/-/issues/10
  patch -Np1 < "${srcdir}/dynamically_generate_desktop_file_protocols.patch"
}

build() {
  local _meson_options=(
    --auto-features auto

    -Dlibmpv=true
    -Dgl-x11=enabled
    -Dcaca=disabled
    -Dcdda=enabled
    -Ddvbin=enabled
    -Ddvdnav=enabled
    -Dlibarchive=enabled
    -Dopenal=enabled
  )

  arch-meson "${pkgname}" build "${_meson_options[@]}"

  meson compile -C build
}

check() {
  meson test -C build
}

package() {
  depends+=('libasound.so' 'libavcodec.so' 'libavdevice.so' 'libavfilter.so'
            'libavformat.so' 'libavutil.so' 'libswresample.so' 'libswscale.so'
            'libjack.so' 'liblcms2.so' 'libarchive.so' 'libass.so' 'libbluray.so'
            'libjpeg.so' 'libplacebo.so' 'libpulse.so' 'libva.so' 'libva-drm.so'
            'libva-wayland.so' 'libva-x11.so' 'libxkbcommon.so' 'librubberband.so')

  meson install -C build --destdir "${pkgdir}"

  # delete private entries only required for static linking 
  sed -i -e '/Requires.private/d' -e '/Libs.private/d' "${pkgdir}"/usr/lib/pkgconfig/mpv.pc

  install -m0644 "${pkgname}"/DOCS/{encoding.rst,tech-overview.txt} \
    "${pkgdir}"/usr/share/doc/mpv

  install -m0644 "${pkgname}"/TOOLS/{umpv,mpv_identify.sh,stats-conv.py,idet.sh,lua/*} \
    -D -t "${pkgdir}"/usr/share/mpv/scripts
}
