# Maintainer: Martell Malone <martellmalone@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=vlc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=3.0.21
pkgrel=20
pkgdesc="A multi-platform MPEG, VCD/DVD, and DivX player (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.videolan.org/vlc/"
msys2_repository_url="https://code.videolan.org/videolan/vlc"
msys2_references=(
  "cpe: cpe:/a:videolan:vlc"
  "cpe: cpe:/a:videolan:vlc_media_player"
)
install=vlc-${MSYSTEM}.install
license=('spdx:LGPL-2.1-or-later OR spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-a52dec"
         "${MINGW_PACKAGE_PREFIX}-abseil-cpp"
         "${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-aribb24"
         "${MINGW_PACKAGE_PREFIX}-chromaprint"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-faad2"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg7.1"
         "${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-fluidsynth"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gsm"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbluray"
         "${MINGW_PACKAGE_PREFIX}-libcaca"
         "${MINGW_PACKAGE_PREFIX}-libcddb"
         "${MINGW_PACKAGE_PREFIX}-libcdio"
         "${MINGW_PACKAGE_PREFIX}-libdca"
         "${MINGW_PACKAGE_PREFIX}-libdsm"
         "${MINGW_PACKAGE_PREFIX}-libdvdcss"
         "${MINGW_PACKAGE_PREFIX}-libdvdnav"
         "${MINGW_PACKAGE_PREFIX}-libdvbpsi"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-libgme"
         "${MINGW_PACKAGE_PREFIX}-libgoom2"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libmad"
         "${MINGW_PACKAGE_PREFIX}-libmatroska"
         "${MINGW_PACKAGE_PREFIX}-libmfx"
         "${MINGW_PACKAGE_PREFIX}-libmicrodns"
         "${MINGW_PACKAGE_PREFIX}-libmodplug"
         "${MINGW_PACKAGE_PREFIX}-libmpcdec"
         "${MINGW_PACKAGE_PREFIX}-libmpeg2"
         "${MINGW_PACKAGE_PREFIX}-libmysofa"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libnfs"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libnotify"
         "${MINGW_PACKAGE_PREFIX}-libproxy"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libsamplerate"
         "${MINGW_PACKAGE_PREFIX}-libsecret"
         "${MINGW_PACKAGE_PREFIX}-libshout"
         "${MINGW_PACKAGE_PREFIX}-libsoxr"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvncserver"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lua51"
         "${MINGW_PACKAGE_PREFIX}-minizip"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-portaudio"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-pupnp"
         "${MINGW_PACKAGE_PREFIX}-schroedinger"
         "${MINGW_PACKAGE_PREFIX}-speex"
         "${MINGW_PACKAGE_PREFIX}-srt"
         "${MINGW_PACKAGE_PREFIX}-taglib"
         "${MINGW_PACKAGE_PREFIX}-twolame"
         "${MINGW_PACKAGE_PREFIX}-vcdimager"
         "${MINGW_PACKAGE_PREFIX}-libx264"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-SDL_image"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-glew"
             "${MINGW_PACKAGE_PREFIX}-cyrus-sasl")
source=(https://get.videolan.org/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.xz
        0001-Use-libdir-for-plugins-on-msys2.patch
        0002-MinGW-w64-lfind-s-_NumOfElements-is-an-unsigned-int.patch
        0003-MinGW-w64-don-t-pass-static-to-pkg-config-if-SYS-min.patch
        0004-Revert-Win32-prefer-the-static-libraries-when-creati.patch
        0005-no-static-linking.patch
        0006-Linking-libqt_plugin-with-winmm.patch
        0007-Mingw-load-libraries-not-only-from-system32.patch
        0008-include-missing-headers.patch
        0019-qt5-headers.patch
        0025-iovec-redefined.patch
        0026-fix-libsynchronization-check.patch
        0030-taglib-arch-fixes.patch
        0031-Taglib-Use-ID3v2Tag-instead-of-tag-for-RIFF-WAV-File.patch
        5574.patch
        0100-more-ffmpeg7-fixes.patch
        https://code.videolan.org/videolan/vlc/-/commit/720c55a08c1f4fe18114040bf9bc7fde32eb12c2.patch)
sha256sums=('24dbbe1d7dfaeea0994d5def0bbde200177347136dbfe573f5b6a4cee25afbb0'
            '130cdf7655deca92fd28dd177cd99aab24113bd1052700896046b66acbcb1f2d'
            '8f40d57bb087306e5099c5b9067dacca0479c4dc9e45eb902ad04eeb3fb83ef3'
            '072de5cda7772a63d81a067be1b942e1cf59f6b53206e61d0caac9dc18216ad6'
            '416f6c11eb8a47ea3d0f258fa7d471e0eb53b79e4816913d083aacead0113e7a'
            'bf020049257ea377e26d8bd3f1714612fd029d9f8237665934e1d511598cead3'
            '71eada1e1a93e77f7a6c76423e4d39e9f8f700ca12322c3ffa7ecfaa3debf5ae'
            '194b85e50f2c410ce50d7c7ff23bc915020c9c073a83f64f12c8247d41e1b475'
            'e604ac4849b4f792961b887063f381b87c69445b49f20f24451897c857ce9813'
            '278e6111e467dfb1b6b4228489dc6ab029abfa11df3c77d8cd8021987a11b4ea'
            '2b0f45a93c804e8e6e59a56d0ed82d2bc703a3b7ef166aa98ad311d9b15d4f08'
            '54ddd07473da265ebaff7d928a23bcc639354513d5e0677afe21b6496f934625'
            '8830b5ee3a3ea25c2931b49a7cb913517e710cbe6c2b51d61d14df31344eb694'
            '1d430b646fec3b516aa53c7f3ce3b0873baf707cf814ddee843f1794d76b102a'
            '5442798322e60b66eda69dab25a2179a43bee8a6d87f61bb7c5e96072d945ad7'
            '4ba10fb9ecfb3b421d41efedb2844d681252d6a0dc4d86f867dd3bff57fade80'
            '46bbb54dfdd0ce81627d09889244fc7917ebc9ddf5be3d5fed2c80be3f3db787')

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-Use-libdir-for-plugins-on-msys2.patch \
    0002-MinGW-w64-lfind-s-_NumOfElements-is-an-unsigned-int.patch \
    0003-MinGW-w64-don-t-pass-static-to-pkg-config-if-SYS-min.patch \
    0004-Revert-Win32-prefer-the-static-libraries-when-creati.patch \
    0005-no-static-linking.patch \
    0006-Linking-libqt_plugin-with-winmm.patch \
    0007-Mingw-load-libraries-not-only-from-system32.patch \
    0008-include-missing-headers.patch \
    0019-qt5-headers.patch \
    0025-iovec-redefined.patch \
    0026-fix-libsynchronization-check.patch

  # taglib 2 fixes
  # Backport of https://gitlab.archlinux.org/archlinux/packaging/packages/vlc/-/blob/40b8a8a1fc24f594a0b828fbde521c644964efaf/taglib-2.patch
  # Backport of https://code.videolan.org/videolan/vlc/-/commit/ec29dfca1e59530dd412d779e0b045079b72ffb6
  apply_patch_with_msg \
    0030-taglib-arch-fixes.patch \
    0031-Taglib-Use-ID3v2Tag-instead-of-tag-for-RIFF-WAV-File.patch

  # https://code.videolan.org/videolan/vlc/-/merge_requests/5574
  # https://code.videolan.org/videolan/vlc/-/merge_requests/5574#note_449557
  apply_patch_with_msg \
    5574.patch \
    0100-more-ffmpeg7-fixes.patch

  # https://code.videolan.org/videolan/vlc/-/commit/720c55a08c1f4fe18114040bf9bc7fde32eb12c2
  apply_patch_with_msg \
    720c55a08c1f4fe18114040bf9bc7fde32eb12c2.patch

  ./bootstrap
}

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

  # Build against ffmpeg 7.1
  export PKG_CONFIG_PATH="${MINGW_PREFIX}/lib/ffmpeg7.1/pkgconfig:${PKG_CONFIG_PATH}"
  # some things ignore the pkgconfig paths, so also force them via CLFAGS/LDFLAGS
  CFLAGS+=" $(pkgconf --cflags libavcodec)"
  LDFLAGS+=" $(pkgconf --libs-only-L libavcodec)"

  # Hack getting private QtGui directory
  local _qtver=$(qmake -query QT_VERSION)
  local _qtguiinc=$(pkg-config Qt5Gui --variable=includedir)
  local _privdir=${_qtguiinc}/QtGui/${_qtver}/QtGui
  CPPFLAGS+=" -I${_privdir}"
  # Too many warnings
  CFLAGS+=" -Wno-incompatible-pointer-types -w"
  CXXFLAGS+=" -w"

  # Avoid ATOMIC_VAR_INIT removal with C23
  CFLAGS+=" -std=gnu17"

  BUILDCC="${CC}" \
  "${srcdir}"/${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --with-binary-version="msys2" \
    --enable-qt \
    --enable-dav1d \
    --disable-ncurses \
    --disable-dbus \
    --disable-telx \
    --enable-nls \
    --disable-lua \
    --disable-libplacebo \
    --disable-gst-decode

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR="${pkgdir}" install

  for res in 16 32 48 128; do
    install -D -m644 "${srcdir}/${_realname}-${pkgver}/share/icons/${res}x${res}/vlc.png" \
                     "${pkgdir}/${MINGW_PREFIX}/share/icons/hicolor/${res}x${res}/apps/vlc.png"
  done
}
