# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               active_variants 1.1
PortGroup               conflicts_build 1.0
PortGroup               github 1.0
PortGroup               legacysupport 1.1
PortGroup               openssl 1.0

# This port is not really stable, however at the moment it works again via X11.
# Confirmed on 10.6.8 ppc and 10.15 x86_64.

name                    VLC-X11
github.setup            videolan vlc 3.0.23-2
set git_revision        7912887
revision                3
categories              multimedia
license                 GPL-2 LGPL-2.1
maintainers             {@barracuda156 gmail.com:vital.had} openmaintainer

description             VLC media player with X11 backend
long_description        {*}${description}, aimed primarily at supporting legacy \
                        macOS, including PowerPC systems. It may also be used \
                        on modern macOS versions, as long as GUI is unneeded.
homepage                https://www.videolan.org
checksums               rmd160  f4180d9ed287befebb53a2334ed4e4868dd642ec \
                        sha256  1a108be0511a0847c95a5ccccce263413db88839636b9cbfd05a173e115f05ae \
                        size    32816141
github.tarball_from     archive

# Upstream lost the file which is required by the build…
post-extract {
    copy ${filespath}/revision.txt ${worksrcpath}/src/
}

# The purpose of this port is to be as compatible as possible and work on every macOS
# version, including PowerPC systems. Priority is given to legacy systems,
# since on the current ones VLC3 can be built with GUI and all modules.
# We do not want a dozen versions here with complex conditional fallback code.
# Some choices below may not be the first best for newer macOS versions.
# If this does not fit your aims, please rather install a standard VLC3
# either from MacPorts or the developer.

# FIXME: what is labelled bonjour in VLC3 has no relation to bonjour in VLC2,
# where it was an Avahi module. VLC3 has some ObjC sourcery instead,
# which unsurprisingly fails to compile. libsecuretransport did not build
# already in VLC 2.2.8. However, it may be possible to fix both.

# Macros are modified to be actually useful: HAVE_OSX becomes a configurable option
# instead of a coerced one; HAVE_DARWIN is used as a macOS baseline.
# That is, everything macOS-specific which can be supported from 10.5+
# should go under HAVE_DARWIN. Anything which requires modern Xcode
# or newer SDK and does not have a fallback implemented should go inside HAVE_OSX,
# and we conveniently disable all that breaking stuff via --disable-osx arg.
# Notice, this is not the same as --disable-macosx* of the upstream;
# despite a suggestive wording, the latter fails to actually disable macOS code.
patchfiles-append       0001-configure-allow-disabling-opengl.patch \
                        0002-configure-allow-xcb-on-darwin.patch \
                        0003-Avoid-building-tests.patch \
                        0004-picture_pool-fix-a-bug-in-for-loop.patch \
                        0005-demux-add-missing-cinttypes.patch \
                        0006-Fix-environ-define.patch \
                        0007-Sort-out-HAVE_OSX-and-HAVE_DARWIN-macros-to-work.patch

if {[string match *gcc* ${configure.compiler}]} {
    patchfiles-append   0008-configure-do-not-err-out-on-implicit-function-decl.patch \
                        0009-Trash-some-breaking-flags.patch
}

if {${configure.cxx_stdlib} ne "libc++"} {
    patchfiles-append   0010-configure-drop-hardcoding-of-C-runtime.patch
}

platform darwin {
    if {${os.major} < 19} {
        patchfiles-append \
                        0011-disc_helper-drop-unsupported-check.patch
    }

    # FIXME: this patch is a fallback to an older code.
    # It may not be robust, and perhaps can be improved.
    if {${os.major} < 14} {
        patchfiles-append \
                        0012-libauhal-fix-the-build.patch \
                        0013-Restore-working-code-for-text_renderer.patch
    }
}

# Fix install paths:
post-patch {
    reinplace "s|@rpath|${prefix}\/lib|g" \
                        ${worksrcpath}/lib/Makefile.am \
                        ${worksrcpath}/src/Makefile.am
    reinplace "s|=10.8|=${macosx_deployment_target}|" \
                        ${worksrcpath}/modules/codec/Makefile.am
    reinplace "s|have_egl=\"yes\"|have_egl=\"no\"|" ${worksrcpath}/configure.ac
    reinplace "s|@revision@|${git_revision}|" ${worksrcpath}/src/revision.txt
}

use_autoreconf          yes

set ffmpeg_v            7

depends_build-append    path:bin/pkg-config:pkgconfig \
                        port:xorg-xorgproto

configure.pkg_config_path-prepend \
                        ${prefix}/libexec/ffmpeg${ffmpeg_v}/lib/pkgconfig

# This is ugly, admittedly:
# Undefined symbols for architecture x86_64: "_av_channel_layout_default"
conflicts_build         ffmpeg

depends_lib-append      port:a52dec \
                        port:aalib \
                        port:aom \
                        port:avahi \
                        port:bzip2 \
                        path:lib/pkgconfig/cairo.pc:cairo \
                        port:dav1d \
                        port:dbus \
                        port:faad2 \
                        port:ffmpeg${ffmpeg_v} \
                        port:flac \
                        port:fluidsynth \
                        path:lib/pkgconfig/fontconfig.pc:fontconfig \
                        path:lib/pkgconfig/freetype2.pc:freetype \
                        port:fribidi \
                        port:gettext-runtime \
                        path:lib/pkgconfig/glib-2.0.pc:glib2 \
                        port:gmp \
                        path:lib/pkgconfig/gnutls.pc:gnutls \
                        port:gstreamer1-gst-plugins-base \
                        path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                        port:lame \
                        port:libarchive \
                        path:lib/pkgconfig/libass.pc:libass \
                        port:libbluray \
                        path:lib/pkgconfig/caca.pc:libcaca \
                        port:libcddb \
                        port:libcdio \
                        port:libdc1394 \
                        port:libdca \
                        port:libdvbpsi \
                        port:libdvdnav \
                        port:libdvdread \
                        port:libebml \
                        port:libgcrypt \
                        port:libgme \
                        port:libgpg-error \
                        port:libiconv \
                        port:libid3tag \
                        port:libidn2 \
                        path:include/turbojpeg.h:libjpeg-turbo \
                        port:libmad \
                        port:libmatroska \
                        port:libmodplug \
                        port:libmpeg2 \
                        port:libmtp \
                        port:libogg \
                        port:libopus \
                        port:libpng \
                        port:libproxy \
                        path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                        port:libsamplerate \
                        port:libsecret \
                        port:libshout2 \
                        port:libsndfile \
                        port:libssh2 \
                        port:libtheora \
                        port:libupnp \
                        path:lib/pkgconfig/libusb-1.0.pc:libusb \
                        port:libvorbis \
                        path:lib/pkgconfig/vpx.pc:libvpx \
                        port:libxml2 \
                        port:live555 \
                        port:minizip \
                        port:mpg123 \
                        port:musepack \
                        port:ncurses \
                        port:nettle \
                        port:openjpeg \
                        port:orc \
                        port:p11-kit \
                        port:readline \
                        port:schroedinger \
                        port:speex \
                        port:speexdsp \
                        port:taglib \
                        port:tiff \
                        port:twolame \
                        port:vcdimager \
                        port:x264 \
                        port:x265 \
                        port:xz \
                        port:zlib \
                        port:zvbi

# X11 (the only working variant at the moment)
if {![variant_isset cocoa]} {
    depends_lib-append  port:mesa \
                        port:xorg-libX11 \
                        port:xorg-libXau \
                        port:xorg-libxcb \
                        port:xorg-libXdmcp \
                        port:xorg-libXext \
                        port:xorg-libXinerama \
                        port:xorg-libXrandr \
                        port:xorg-libXv \
                        port:xorg-libXxf86vm \
                        port:xorg-xcb-util \
                        port:xorg-xcb-util-keysyms \
                        port:xrender \
                        port:XviD

    require_active_variants ffmpeg${ffmpeg_v} x11
}

depends_run-append      port:libaacs \
                        port:libbdplus

universal_variant       no

compiler.c_standard         2011
compiler.cxx_standard       2014
compiler.blacklist-append   {clang < 800}

# VLC has a zoo of options, and they change over time.
# If something is found to be redundant or, on the contrary,
# lacking, please help to improve the port.
# It is desirable not to make any potentially breaking changes
# here without proper testing on systems of concern,
# which include PowerPC and legacy Intel.

# TODO: VLC 3.x needs an old version of libplacebo.
# libnotify and samba could be supported if desired.

# Misc options
configure.args-append   --disable-debug \
                        --disable-kwallet \
                        --disable-lua \
                        --disable-microdns \
                        --disable-notify \
                        --disable-osx \
                        --disable-osx-notifications \
                        --disable-sout \
                        --disable-sparkle \
                        --disable-update-check \
                        --enable-avahi \
                        --enable-dbus \
                        --enable-gnutls \
                        --enable-libgcrypt \
                        --enable-libxml2 \
                        --enable-rpath \
                        --enable-secret \
                        --enable-taglib \
                        --enable-upnp

# Input Plugins
configure.args-append   --disable-freerdp \
                        --disable-macosx-avfoundation \
                        --disable-opencv \
                        --disable-smbclient \
                        --enable-archive \
                        --enable-bluray \
                        --enable-dvdnav \
                        --enable-dvdread \
                        --enable-libcddb \
                        --enable-live555 \
                        --enable-realrtsp \
                        --enable-sftp \
                        --enable-vcd

# Mux/Demux Plugins
configure.args-append   --enable-dvbpsi \
                        --enable-gme \
                        --enable-matroska \
                        --enable-mod \
                        --enable-mpc \
                        --enable-ogg \
                        --enable-shout

# Codec Plugins
configure.args-append   --enable-a52 \
                        --enable-aom \
                        --enable-avcodec \
                        --enable-avformat \
                        --enable-dav1d \
                        --enable-dca \
                        --enable-faad \
                        --enable-flac \
                        --enable-fluidsynth \
                        --enable-gst-decode \
                        --enable-jpeg \
                        --enable-libass \
                        --enable-libmpeg2 \
                        --enable-live555 \
                        --enable-mad \
                        --enable-mpg123 \
                        --enable-opus \
                        --enable-png \
                        --enable-postproc \
                        --enable-schroedinger \
                        --enable-speex \
                        --enable-swscale \
                        --enable-theora \
                        --enable-twolame \
                        --enable-vorbis \
                        --enable-vpx \
                        --enable-x264 \
                        --enable-x265 \
                        --enable-zvbi

# TODO: see if libplacebo can be usable.
configure.args-append   --disable-evas \
                        --disable-gles2 \
                        --disable-kva \
                        --disable-libplacebo \
                        --disable-mmal \
                        --disable-sdl-image \
                        --disable-wayland \
                        --enable-aa \
                        --enable-caca \
                        --enable-fontconfig \
                        --enable-freetype \
                        --enable-fribidi \
                        --enable-harfbuzz \
                        --enable-svg \
                        --enable-svgdec \
                        --enable-xcb \
                        --enable-xvideo \
                        --with-x

# Audio Plugins
# FIXME: PulseAudio disabled for now due to being defunct:
# https://trac.macports.org/ticket/69539
# TOD0: is it gonna work with jack 1.x? (jack 2.x is broken due to a gcc bug.)
configure.args-append   --disable-alsa \
                        --disable-chromaprint \
                        --disable-chromecast \
                        --disable-jack \
                        --disable-sndio \
                        --disable-pulse \
                        --enable-samplerate \
                        --enable-soxr

# Interface Plugins
# FIXME: either fix minimal-macosx or perhaps forward-port working cocoa gui from 2.1.x.
configure.args-append   --disable-macosx \
                        --disable-minimal-macosx \
                        --disable-qt \
                        --disable-skins2 \
                        --disable-vnc \
                        --enable-ncurses

# Visualisations and Video Filter Plugins
configure.args-append   --disable-goom \
                        --disable-projectm \
                        --disable-vsxu

# live555 is installed to a weird location.
# Explicit linking to OpenSSL is needed for its module to build.
configure.env-append    "CXXCPP=${configure.cxx} -E" \
                        "LIVE555_CFLAGS=-I${prefix}/lib/live/BasicUsageEnvironment/include \
                                -I${prefix}/lib/live/groupsock/include \
                                -I${prefix}/lib/live/liveMedia/include \
                                -I${prefix}/lib/live/UsageEnvironment/include" \
                        "LIVE555_LIBS=-L${prefix}/lib/live/BasicUsageEnvironment -lBasicUsageEnvironment \
                                -L${prefix}/lib/live/groupsock -lgroupsock \
                                -L${prefix}/lib/live/liveMedia -lliveMedia \
                                -L${prefix}/lib/live/UsageEnvironment -lUsageEnvironment \
                                -L[openssl::install_area]/lib -lssl -lcrypto" \
                        "MATROSKA_CFLAGS=-I${prefix}/include/ebml" \
                        "MATROSKA_LIBS=-lmatroska -lebml"

build.args              V=1

variant opengl description "Enable OpenGL" {
    patchfiles-delete       0001-configure-allow-disabling-opengl.patch
    configure.args-delete   --disable-gl
}

# FIXME: this builds but does not work. Do not use it!
variant cocoa description "Enable minimal Cocoa GUI" {
    known_fail              yes

    patchfiles-append       1001-video_output-macosx.m.patch \
                            1002-gui-minimal_macosx-intf.m.patch \
                            1003-gui-minimal_macosx-misc.m.patch \
                            1004-gui-minimal_macosx-VLCMinimalVoutWindow.m.patch

    configure.args-replace  --disable-minimal-macosx --enable-minimal-macosx

    configure.args-delete   --enable-xcb --enable-xvideo --with-x
}

# This may need to be disabled on some configurations.
default_variants-append +opengl

notes "
This port is experimental. Consider alternatives like QMPlay2(-devel), mplayer(-devel) or mpv.
"
