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

PortSystem              1.0
PortGroup               github 1.0

github.setup            HandBrake HandBrake 1.11.0
name                    [string tolower ${github.project}]
revision                1
categories              multimedia
maintainers             {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
license                 GPL-2
description             HandBrake is an open-source video transcoder
long_description        {*}${description}
homepage                https://handbrake.fr

github.tarball_from     releases

# TODO: switch to downloading sources without git fetch.
# master_sites-append     https://github.com/HandBrake/HandBrake-contribs/releases/download/contribs/:contrib \
                        https://github.com/HandBrake/HandBrake-contribs/releases/download/contribs2/:contrib

distname                HandBrake-${version}-source
use_bzip2               yes

checksums               ${distname}${extract.suffix} \
                        rmd160  0d162f304c36c5cd0db6e57743e0fdb9db748605 \
                        sha256  c5de77365b083f519c76b9edcc0685d8bda9ce04fc0ad59c3c38145355ef1b17 \
                        size    11756006

extract.rename          yes

# TODO: powerpc build hardcodes gcc14 at the moment. This is okay for a while,
# but has to be changed. We also do not want to hardcode clang, which is what upstream does.

set py_ver              3.11
set py_ver_nodot        [string map {. {}} ${py_ver}]
configure.python        ${prefix}/bin/python${py_ver}

post-extract {
    # This currently assumes gcc14:
    copy ${filespath}/ppc-darwin-gcc.meson ${worksrcpath}/make/cross/
    # Fix broken configure and build of ffmpeg:
    copy ${filespath}/A32-ffmpeg-fix-static_asserts.patch ${worksrcpath}/contrib/ffmpeg/
    # Disabled since we use the port:
    # copy ${filespath}/A02-x264-ppc.patch ${worksrcpath}/contrib/x264/
    if {[string match *clang* ${configure.compiler}]} {
            copy ${filespath}/A02-fix-avx2-flags.patch ${worksrcpath}/contrib/zimg/
    }
}

patch.pre_args-replace  -p0 -p1

# Broken in upstream: https://github.com/HandBrake/HandBrake/issues/6320
patchfiles              0001-configure.py-use-correct-build-tuple-for-powerpc-dar.patch \
                        0002-Disable-vtb.patch \
                        0003-Build-fixes.patch

# Avoid building what can be used from ports:
patchfiles-append       0004-Avoid-unneccessary-bundled-deps.patch

# Fix build target (not used at the moment):
patchfiles-append       0005-contrib_libvpx.patch

if {[string match *gcc* ${configure.compiler}]} {
    patchfiles-append   0006-Cocoa-compat.patch
}

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    depends_lib-append  path:lib/MacportsLegacySupport.dylib:legacy-support

    patchfiles-append   0007-contrib_ffmpeg_module.patch \
                        0008-legacy-support.patch \
                        0009-fix-dvdnav-dvdread-build.patch

    post-patch {
        reinplace "s|@PREFIX@|${prefix}|g" \
            ${worksrcpath}/gtk/meson.build ${worksrcpath}/gtk/module.defs \
            ${worksrcpath}/libhb/module.defs ${worksrcpath}/contrib/ffmpeg/module.defs
    }
}

post-patch {
    fs-traverse f ${worksrcpath} {
        if {[string match *.py ${f}]} {
            reinplace -q "s|/usr/bin/env python3|${configure.python}|" ${f}
        }
    }
}

depends_build-append    port:autoconf \
                        port:automake \
                        path:bin/cmake:cmake \
                        path:bin/git:git \
                        port:gnutar \
                        port:libtool \
                        port:meson \
                        port:ninja \
                        path:bin/pkg-config:pkgconfig \
                        port:python${py_ver_nodot}

platform i386 {
    depends_build-append \
                        port:nasm
}

depends_lib-append      port:bzip2 \
                        path:lib/pkgconfig/cairo.pc:cairo \
                        path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                        path:lib/pkgconfig/glib-2.0.pc:glib2 \
                        path:lib/pkgconfig/gtk4.pc:gtk4 \
                        port:jansson \
                        port:lame \
                        path:lib/pkgconfig/libass.pc:libass \
                        port:libiconv \
                        path:include/turbojpeg.h:libjpeg-turbo \
                        port:libogg \
                        port:libopus \
                        port:libtheora \
                        port:libvorbis \
                        path:lib/pkgconfig/vpx.pc:libvpx \
                        port:libxml2 \
                        path:lib/pkgconfig/pango.pc:pango \
                        port:speex \
                        port:x264 \
                        port:xorg-libX11 \
                        port:xz \
                        port:zlib

depends_run-append      port:dbus \
                        port:desktop-file-utils

compiler.c_standard             2011
compiler.thread_local_storage   yes
compiler.blacklist-append       {clang < 1300}

configure.args-append   --disable-libdovi \
                        --disable-vt \
                        --disable-xcode \
                        --enable-asm \
                        --enable-ffmpeg-aac \
                        --enable-ffmpeg-prores \
                        --enable-gtk \
                        --enable-x265 \
                        --force \
                        --prefix=${prefix} \
                        --verbose

build.dir               ${worksrcpath}/build
build.target
build.args-append       V=1

configure.ldflags-append \
                        -L${worksrcpath}/contrib/lib

post-activate {
    system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
}

# https://trac.macports.org/ticket/70137
notes "
If you get an error upon launching the app,\
set GSK_RENDERER=cairo in the environment.\
You may also add it into a shell config file.
"
