# -*- 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
PortGroup           python 1.0

name                youtube-dl

if {${subport} eq ${name}} {
    github.setup    ytdl-org ${subport} 2024.07.11 {} -nightly
    revision        0
    checksums       rmd160  2688fca2c3b4f86b16edea3dcc34e6fc89f8111b \
                    sha256  06a2f1f9421d6063ae77e70d4d4a70b31839aeaaea4fc297b8b6dfe95053952d \
                    size    1707209
    github.tarball_from archive

    homepage        https://${github.author}.github.io/${github.project}

    python.pep517_backend setuptools

    set symlinks [list \
        etc/bash_completion.d/${subport}.bash-completion \
        etc/fish/completions/${subport}.fish \
        share/zsh/site-functions/_${subport} \
    ]

    post-destroot {
        # https://github.com/ytdl-org/youtube-dl/issues/9231
        xinstall -d ${destroot}${python.prefix}/share/zsh/site-functions
        xinstall -m 0644 ${worksrcpath}/youtube-dl.plugin.zsh \
            ${destroot}${python.prefix}/share/zsh/site-functions/_youtube-dl
    }

    livecheck.type  regex
    livecheck.url   ${homepage}/latest/version
    livecheck.regex {^([0-9.]+)$}
}

subport yt-dlp {
    github.setup    yt-dlp ${subport} 2026.03.17
    revision        0
    checksums       rmd160  3e4528e80d60d82c1212319a7c668b985c2a2756 \
                    sha256  c74f56fc88a7f9b7c995646f30538974a03dd7609f55257b20594fdeeb1e13df \
                    size    6209587
    github.tarball_from releases
    dist_subdir     ${subport}/${version}
    distname        ${subport}

    worksrcdir      ${subport}

    python.pep517_backend hatch

    set symlinks [list \
        share/bash-completion/completions/${subport} \
        share/fish/vendor_completions.d/${subport}.fish \
        share/man/man1/${subport}.1 \
        share/zsh/site-functions/_${subport} \
    ]
}

categories          net multimedia www
maintainers         {ryandesign @ryandesign}
supported_archs     noarch
platforms           {darwin any}
license             public-domain
installs_libs       no

description         command-line program to download videos from YouTube.com and other sites
long_description    ${name} is a small {*}${description}.

patchfiles-append   ${subport}-config-location.patch \
                    ${subport}-update.patch

# https://github.com/yt-dlp/yt-dlp/issues/14404
subport yt-dlp-ejs {
    # Must be updated together with yt-dlp, to a matching version.
    python.rootname yt_dlp_ejs
    version         0.8.0
    revision        0
    description     Support for external JavaScript runtime
    checksums       rmd160  6be40d6ee24651d68f3621ccda8ccae8d31b8940 \
                    sha256  79300e5fca7f937a1eeede11f0456862c1b41107ce1d726871e0207424f4bdb4 \
                    size    53443
    dist_subdir     ${subport}/${version}
    # https://github.com/yt-dlp/ejs/issues/32
    master_sites    https://files.pythonhosted.org/packages/py3/y/${python.rootname}/
    distname        ${python.rootname}-${version}-py3-none-any
    extract.suffix  .whl
    extract.only
    patchfiles

    # quickjs, quickjs-devel, quickjs-ng
    depends_run     path:bin/qjs:quickjs

    build           {}
    destroot.target ${distpath}/${distfiles}
}

post-patch {
    if {${subport} eq ${name}} {
        set optfile options.py
    } else {
        set optfile utils/_utils.py
    }
    if {${subport} eq "yt-dlp"} {
        reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
            ${subport}.1
    }
    if {${subport} ne "yt-dlp-ejs"} {
        reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
            README.md \
            [string map {- _} ${subport}]/${optfile}
    }
}

post-destroot {
    if {${subport} ne "yt-dlp-ejs"} {
        foreach f ${symlinks} {
            xinstall -d ${destroot}${prefix}/[file dirname ${f}]
            ln -s ${python.prefix}/${f} ${destroot}${prefix}/${f}
        }

        set docdir ${prefix}/share/doc/${subport}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} LICENSE README.md ${destroot}${docdir}
    }
}

if {${subport} ne "yt-dlp-ejs"} {
    variant ffmpeg description "Add ffmpeg dependency, used to extract audio" {
        depends_run-append  path:bin/AtomicParsley:AtomicParsley \
                            path:bin/ffmpeg:ffmpeg
    }
    default_variants        +ffmpeg
}

variant python310 conflicts python311 python312 python313 description {Use Python 3.10} {}
variant python311 conflicts python310 python312 python313 description {Use Python 3.11} {}
variant python312 conflicts python310 python311 python313 description {Use Python 3.12} {}
variant python313 conflicts python310 python311 python312 description {Use Python 3.13} {}

if {![variant_isset python310] && ![variant_isset python311] && ![variant_isset python312] && ![variant_isset python313]} {
    default_variants +python313
}

if {[variant_isset python310]} {
    python.default_version  310
} elseif {[variant_isset python311]} {
    python.default_version  311
} elseif {[variant_isset python312]} {
    python.default_version  312
} elseif {[variant_isset python313]} {
    python.default_version  313
}

# WARNING: hlsnative has detected features it does not support, extraction will be delegated to ffmpeg
depends_lib-append          port:py${python.version}-pycryptodome

if {${subport} eq "yt-dlp"} {
    depends_run-append      port:yt-dlp-ejs

    # https://github.com/yt-dlp/yt-dlp/wiki/EJS
    notes-append "
        To complete the set-up, add `--js-runtimes quickjs:${prefix}/bin/qjs` line\
        to yt-dlp config file. For example, ~/yt-dlp.conf is a valid config location.
    "
}
