# -*- 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           xcodeversion 1.0

name                HandBrake
version             0.10.0
categories          multimedia aqua
platforms           macosx
license             GPL-2
maintainers         {larryv @larryv}

subport HandBrakeCLI {
    categories-delete aqua
}


description         The open source video transcoder
long_description    HandBrake is a tool for converting video from \
                    nearly any format to a selection of modern, \
                    widely supported codecs.
homepage            http://handbrake.fr/

subport HandBrakeCLI {
    description-append          (CLI)
    long_description-append {*}{(This port provides the command-line
                                interface only; install the `HandBrake`
                                port for the graphical user interface.)}
}


# The MacGui uses NS_ENUM, boolean literals, NSDictionary
# subscripting, and other modern Objective-C features
# (https://developer.apple.com/library/mac/releasenotes/ObjectiveC/ObjCAvailabilityIndex).
minimum_xcodeversions {12 4.4}
set minDarwinVersion 12

subport HandBrakeCLI {
    # TODO: Fix build on Snow Leopard. All the contribs build fine,
    # but linking the HandBrakeCLI executable fails.
    minimum_xcodeversions
    set minDarwinVersion 11
}

set minOSXVersion 10.[expr {$minDarwinVersion - 4}]
if {${os.platform} eq "darwin" && ${os.major} < $minDarwinVersion} {
    pre-fetch {
        ui_error "Building ${subport} ${version} requires\
                OS X $minOSXVersion or later."
        return -code error "unsupported platform"
    }
}


# TODO: Build correctly for a non-native arch.
if {${os.arch} ne "i386"
        || [catch {sysctl hw.cpu64bit_capable} isNative64Bit]} {
    supported_archs i386 x86_64
} elseif {$isNative64Bit} {
    supported_archs x86_64
} else {
    supported_archs i386
}


# +universal would be possible, but not implemented yet
# see ${worksrcpath}/doc/BUILD-Mac for details
# TODO: Implement? Is this even worth bothering with?
universal_variant   no


# Autotools: fribidi, libdvdnav, libdvdread, libogg, libvorbis
# CMake: x265
# Python: configure script
# Yasm: libav, libvpx, x264, x265
depends_build       port:autoconf \
                    port:automake \
                    path:bin/cmake:cmake \
                    port:libtool \
                    port:pkgconfig \
                   {bin:python2(\.(4|5|6|7))?:python27} \
                    port:yasm

# bzip2: freetype
# libiconv: libxml2
# zlib: freetype, libxml2
depends_lib         port:bzip2 \
                    port:libiconv \
                    port:zlib

# Configure the bundled fontconfig to use the port's fonts.
depends_lib-append  port:fontconfig

subport HandBrake {
    depends_run     port:HandBrakeCLI
}

# TODO: Sort out depends_skip_archcheck.


# Many of the contrib build systems detect CPU features at build time.
archive_sites


# use_bzip2 must come first so master_sites gets the right extract.suffix.
use_bzip2           yes
master_sites        http://handbrake.fr/rotation.php?file=${distname}${extract.suffix}&dummy=:src \
                    http://download.handbrake.fr/handbrake/contrib:contrib

# The subports use the same distfiles.
dist_subdir ${name}

checksums           ${distname}${extract.suffix} \
                        rmd160  c0140fd33b4ca3fea3dd43a5960a71055a00acc8 \
                        sha256  a91042da3cc08454844f7c171ee2fde4f0ed08176407fc0328d5118227ed4114 \
                    fontconfig-2.8.0.tar.gz \
                        rmd160  fefee412cad4fa71b573e997e14f1df12f439be2 \
                        sha256  fa2a1c6eea654d9fce7a4b1220f10c99cdec848dccaf1625c01f076b31382335 \
                    freetype-2.4.7.tar.bz2 \
                        rmd160  48abfcd4546cbd6626796ab121252e804ddcc6d8 \
                        sha256  ba22620479954582371286268ef75e6e932c0270ee67d6797bbf3ed9d31a5d77 \
                    fribidi-0.19.2.tar.gz \
                        rmd160  4fafb4b3140bbbcf4c2c58c714b02db2d71e5491 \
                        sha256  49cf91586e48b52fe25872ff66c1da0dff0daac2593f9f300e2af12f44f64177 \
                    lame-3.98.tar.gz \
                        rmd160  c10c20128622e16f9cac3d346bea8e390e4bfdc5 \
                        sha256  40235e84dfe4760ad3f352590a64b7bda1502a386c97d06229df356426e37686 \
                    libass-0.10.0-1.tar.gz \
                        rmd160  da29a27a4ae107629d0ea6dd7ec21f1d36167b16 \
                        sha256  a0c652f4a5c1bfd58cd47ff627fff2dfbd471b40f765aa96fd1b251c0058b867 \
                    libav-v10.1.tar.bz2 \
                        rmd160  fc9420a1ce8f081df592dbc2ac4500771103b38b \
                        sha256  bc3807426d4e73fe8c245730b0df4ba95766cf7e17e47c738ee302f73d066ac9 \
                    libbluray-0.5.0.tar.bz2 \
                        rmd160  7816daf8dced33765be85621ab3cd3da61edd76d \
                        sha256  d414b548ff97c081e1a2f337c0904f44e65d91a00d2efa4a46a1ecd3de285b4d \
                    libdvdnav-5.0.1-0-gaa3659d.tar.gz \
                        rmd160  a712563c197a3f24636be7aa87396400f110e0f7 \
                        sha256  574b7af7ff52e21048ab62c0b14271e651faafb5d46f8efc64dbd82ff34e162b \
                    libdvdread-5.0.0-6-gcb1ae87.tar.gz \
                        rmd160  708aa5031b16c9b63d6382e9dd223da874702d53 \
                        sha256  d2e4200c3c5d5f812892f9c14851c94e2f707d54e7328946c6397ac999f15f17 \
                    libogg-1.3.0.tar.gz \
                        rmd160  143fa1639ee2175a6609f32287414d6b23deea6f \
                        sha256  a8de807631014615549d2356fd36641833b8288221cea214f8a72750efe93780 \
                    libsamplerate-0.1.4.tar.gz \
                        rmd160  b2ac47d592702a410d5524e1550251c0e75f1879 \
                        sha256  4b4af3ecaee05c8875a9b113c6a2f816f06f283fb882914e57b21c0b08b67b75 \
                    libtheora-1.1.0.tar.bz2 \
                        rmd160  c89c7d0622a89dbf2fcb5e6cf00c7e1bae42b0da \
                        sha256  74be9fe9f85d18c45bdcbb018cebf12c74e2234aeecb4d4c4cb92d80bdd287e2 \
                    libvorbis-aotuv_b6.03.tar.bz2 \
                        rmd160  6e25b9b6545b2e568038349fafe559999b9c3387 \
                        sha256  95455420f07e4b3abdf32bda9f5921e9ed3f1afdc3739098dc090150a42fd7fd \
                    libvpx-v1.3.0.tar.bz2 \
                        rmd160  bf74821ac8f20ed30f44b6e01473ee1f332f721c \
                        sha256  d3dcc8d84af51c6c382b214397c62402e37a799e8ebcda6f4217aef0010451a9 \
                    libxml2-2.7.7.tar.gz \
                        rmd160  9f72c778c3e9e348a68eada0cf624c012658c085 \
                        sha256  af5b781418ba4fff556fa43c50086658ea8a2f31909c2b625c2ce913a1d9eb68 \
                    x264-r2479-dd79a61.tar.gz \
                        rmd160  da2113422d71a85518533499defb2c63f096c6a3 \
                        sha256  7307a83085f4110f8ae690748f00b5acb4882ec34b83ef260fec264c018c49e9 \
                    x265-8768-5e604833c5aa-1.4.tar.bz2 \
                        rmd160  c6cc8072db0b98482760e43216f3926729f225c5 \
                        sha256  7e7f5f2883ccd39dd0e26c7f3ef3c9f80234d669e29811016d8993987d47ab92

# Use `checksums` to construct `distfiles`, instead of maintaining and
# synchronizing two separate lists.
distfiles
foreach {f - - - -} ${checksums} {
    if {$f eq "${distname}${extract.suffix}"} {
        distfiles-append ${f}:src
    } else {
        distfiles-append ${f}:contrib
    }
}


extract.only ${distname}${extract.suffix}
post-extract {
    # Make contrib distfiles available to the build.
    ln -s ${distpath} ${worksrcpath}/download
}


# Patches maintained at https://github.com/larryv/HandBrake.
patch.pre_args-replace  -p0 -p1
patchfiles \
    0001-Add-a-default-font-directory-for-fontconfig-to-use-w.patch \
    0002-BuildSystem-Create-configs-for-OS-X-Yosemite.patch \
    0003-Build-contribs-verbosely.patch \
    0004-MP-BuildSystem-Allow-Xcode-compiler-override.patch \
    0005-MP-BuildSystem-Don-t-abort-on-missing-GCC.patch \
    0006-MP-BuildSystem-Add-targets-for-MacGui-and-CLI.patch \
    0007-MP-BuildSystem-Destroot-and-install-to-right-locatio.patch \
    0008-MP-Force-contrib-builds-to-use-MacPorts-tools.patch \
    0009-MP-MacGui-Disable-Sparkle.patch \
    0010-MP-fontconfig-Use-our-port-s-configuration.patch \
    0011-MP-libav-Respect-our-compiler-selection-on-Darwin-i3.patch \
    0012-libvpx-Add-build-targets-for-OS-X-10.10-Yosemite.patch \
    0013-libvpx-Fix-discovery-of-Darwin-SDKs.patch \
    0014-libvpx-Fix-Clang-cross-building-on-OS-X.patch \
    0015-MP-libvpx-Enable-ccache-if-requested.patch \
    0016-MP-libvpx-Set-build-target-explicitly.patch


# Since HandBrake itself is built using Xcode, restrict ourselves to the
# Xcode-provided compilers.
compiler.blacklist apple* macports*
set xcGCCVersion \
        [switch ${configure.compiler} {
            clang        {expr {"com.apple.compilers.llvm.clang.1_0"}}
            gcc-4.0      {expr {"4.0"}}
            gcc-4.2      {expr {"4.2"}}
            llvm-gcc-4.2 {expr {"com.apple.compilers.llvmgcc42"}}
        }]

# TODO: Respect deployment target in all contrib builds.
set xcconfigOS osx[string map {. {}} ${macosx_deployment_target}]

configure.args      --arch=${configure.build_arch} \
                    --curl=false \
                    --fetch=curl \
                    --gcc='${configure.cc}' \
                    --verbose \
                    --xcode-config=${xcconfigOS}.${configure.build_arch} \
                    --xcode-driver=terminal \
                    APPLICATIONS_DIR='${applications_dir}' \
                    BUILD.jobs=${build.jobs} \
                    CCACHE=${configure.ccache} \
                    GCC_VERSION=$xcGCCVersion


build.type          gnu
build.dir           ${worksrcpath}/build
build.target        macosx.gui.build
build.args          X265.CONFIGURE.exe='${prefix}/bin/cmake'

destroot.target     macosx.gui.install

subport HandBrakeCLI {
    build.target    macosx.cli.build
    destroot.target macosx.cli.install
    # Since HandBrake depends on HandBrakeCLI, install the docs through
    # HandBrakeCLI so they're always available.
    post-destroot {
        set docDir ${destroot}${prefix}/share/doc/${name}
        xinstall -d $docDir
        xinstall -m 0644 -W ${worksrcpath} \
            AUTHORS COPYING CREDITS NEWS THANKS TRANSLATIONS $docDir
    }
}


livecheck.type      regex
livecheck.url       https://handbrake.fr/downloads.php
livecheck.regex     {Current Version: (\d(?:\.\d+)+)}

subport HandBrakeCLI {
    livecheck.type  none
}
