# 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.22
pkgrel=1
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}-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}-ffmpeg"
         "${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}-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}-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
        0100-more-ffmpeg7-fixes.patch
        https://code.videolan.org/videolan/vlc/-/commit/b4fc9059.patch
        # https://code.videolan.org/videolan/vlc/-/merge_requests/7686
        7686.patch
        # https://code.videolan.org/videolan/vlc/-/merge_requests/8012
        8012.patch
        # https://code.videolan.org/videolan/vlc/-/commit/c79659bc
        c79659bc.patch
        https://code.videolan.org/videolan/vlc/-/commit/e09876cc3bf568fce0058a4ead46085415690191.patch
        )
sha256sums=('e2cc1cf0ae0902a09da5a37c249a8a4e4b5ec4dc095443b8e1493c6a7cc138ea'
            '130cdf7655deca92fd28dd177cd99aab24113bd1052700896046b66acbcb1f2d'
            '8f40d57bb087306e5099c5b9067dacca0479c4dc9e45eb902ad04eeb3fb83ef3'
            '072de5cda7772a63d81a067be1b942e1cf59f6b53206e61d0caac9dc18216ad6'
            '416f6c11eb8a47ea3d0f258fa7d471e0eb53b79e4816913d083aacead0113e7a'
            'e72218a3fd6fa63fa07d67c62384da6f7221bd529a45a37d292555bf365dbe16'
            '71eada1e1a93e77f7a6c76423e4d39e9f8f700ca12322c3ffa7ecfaa3debf5ae'
            '194b85e50f2c410ce50d7c7ff23bc915020c9c073a83f64f12c8247d41e1b475'
            'e604ac4849b4f792961b887063f381b87c69445b49f20f24451897c857ce9813'
            '278e6111e467dfb1b6b4228489dc6ab029abfa11df3c77d8cd8021987a11b4ea'
            '2b0f45a93c804e8e6e59a56d0ed82d2bc703a3b7ef166aa98ad311d9b15d4f08'
            '54ddd07473da265ebaff7d928a23bcc639354513d5e0677afe21b6496f934625'
            '4ba10fb9ecfb3b421d41efedb2844d681252d6a0dc4d86f867dd3bff57fade80'
            'a06fd62dd6a1c983477ce531be9182c079b8b230605cf65f99dd95964e313853'
            '5f2728c2912296e8bb466068f5244b5c06f551d46fb8a50014b8bd2fb9f43287'
            'ce4de97e01a92d8d3217915371ab3befa12a54b983ca7d2ed9d1666f937a7441'
            'c3039bb2d24ad79a48cb07371b1114be41dcdbc4b8ea4505b7c3c4ab14246790'
            '2c2cf6ec687881b5950af4b8a09de87430d300ba77ba00bcfa173fbbfaa97aae')

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

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

  # Fix build with ffmpeg-8
  apply_patch_with_msg \
    7686.patch \
    8012.patch \
    b4fc9059.patch \
    c79659bc.patch

  # patch for 3.0.22 Windows regression
  apply_patch_with_msg \
    e09876cc3bf568fce0058a4ead46085415690191.patch

  ./bootstrap
}

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

  # 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
}
