# -*- 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} 2021.12.17
    revision        3
    checksums       rmd160  beca02d231a287488cecffe4c98651b4d6537b9d \
                    sha256  9f3b99c8b778455165b4525f21505e86c7ff565f3ac319e19733d810194135df \
                    size    3332299

    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/man/man1/${subport}.1 \
        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.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} 2025.10.22
    revision        0
    checksums       rmd160  83ddc3b5c6fa99da2554ba48039d8d82d934e2f3 \
                    sha256  8ed2971172cbe1295258341e9f04527a7f27578538ce70ef4c9ebaba8966a969 \
                    size    6025669
    dist_subdir     ${subport}/${version}
    distname        ${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
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}.

github.tarball_from releases

worksrcdir          ${subport}

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

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

post-destroot {
    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}
}

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
