# -*- 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           active_variants 1.1
PortGroup           app 1.1
PortGroup           qt6 1.0

github.setup        XiaoYouChR Ghost-Downloader-3 3.10.4 v
github.tarball_from archive
revision            0
name                [string tolower [join [lrange [split ${github.project} -] 0 1] -]]
categories          net python
homepage            https://gd.xychr.com
supported_archs     noarch
platforms           {darwin any}
license             GPL-3
maintainers         {i0ntempest @i0ntempest} openmaintainer

description         cross-platform QUIC AI-boost fluent-design multi-threaded downloader built with Python
long_description    {*}${description}

checksums           rmd160  8616ee699eb047546a1760cc79c8a88d016ba304 \
                    sha256  06947eb10ff320b9cdf2cc0f3a598a81d4ee54690e97efaba232d4eef7e23d56 \
                    size    8041694

set python_version  314
set python_branch   [string index ${python_version} 0].[string range ${python_version} 1 end]

depends_build-append \
                    port:py${python_version}-nuitka

# darkdetect[macos-listener] (1 extra dep)
# aioftp[socks] (1 extra dep)
# qrcode[png] (extra deps added its portfile)
# niquests[http3,socks,speedups] (extra deps added its portfile)
depends_lib-append  port:python${python_version} \
                    port:py${python_version}-pyobjc \
                    port:py${python_version}-desktop-notifier \
                    port:libtorrent-rasterbar \
                    port:py${python_version}-loguru \
                    port:py${python_version}-niquests \
                    port:py${python_version}-orjson \
                    port:py${python_version}-pyside6 \
                    port:py${python_version}-pyside6-fluent-widgets \
                    port:py${python_version}-qrcode \
                    port:py${python_version}-uvloop \
                    port:py${python_version}-aioftp \
                    port:py${python_version}-siosocks \
                    port:py${python_version}-m3u8 \
                    port:py${python_version}-mpegdash

qt6.depends_lib     qttranslations

require_active_variants \
                    port:libtorrent-rasterbar python${python_version}

post-patch {
    # We don't compile in standalone mode so sys.executable is the interpreter
    reinplace "s|sys.executable|sys.argv\[0\]|g" ${worksrcpath}/app/supports/paths.py
}

# Project does not have a typical Python project structure (pyproject.toml not usable for installation).
# deploy.py compiles standalone bundle and packs everything with nuitka into one app bundle - 
# avoid that, make our own bundle. We compile the main script and the app package only.
use_configure       no

build.cmd           ${prefix}/bin/python${python_branch} -m nuitka
build.target        ${github.project}.py
build.args          --verbose \
                    --plugin-enable=pyside6 \
                    --include-package=app \
                    --static-libpython=no \
                    --disable-ccache

app.executable      ${worksrcpath}/${github.project}.bin
app.icon            ${worksrcpath}/app/assets/logo.icns
app.name            {*}[join [lrange [split ${github.project} -] 0 1] " "]
app.identifier      com.${github.author}.[join [lrange [split ${github.project} -] 0 1] -]
app.retina          yes
app.sign            yes
app.signing_args    --deep
app.manual_post_destroot \
                    yes

set dest_app ${destroot}${applications_dir}/${app.name}.app
set dest_app_bin_dir ${dest_app}/Contents/MacOS/
set dest_app_res_dir ${dest_app}/Contents/Resources/
set dest_app_info_plist ${dest_app}/Contents/Info.plist
# see MACOS_DOCUMENT_TYPES in deploy.py for supported file types
set document_types [list \
    [list "Torrent File"   "torrent.icns" "Viewer" "Alternate" [list "torrent"]] \
    [list "M3U8 Playlist"  "m3u8.icns"    "Viewer" "Alternate" [list "m3u8" "m3u"]] \
    [list "DASH List"      "m3u8.icns"    "Viewer" "Alternate" [list "mpd"]] \
]

destroot {
    copy ${worksrcpath}/features/ ${dest_app_bin_dir}
    # see FEATURE_PACK_BLACKLIST in deploy.py
    set feature_pack_blacklist {jack_yao}
    foreach pack ${feature_pack_blacklist} {
        delete ${dest_app_bin_dir}/features/${pack}
    }
    set file_icons_dir ${worksrcpath}/app/assets/file_icons
    set copied_icons [list]
    foreach document_type ${document_types} {
        lassign ${document_type} type_name icon_file type_role handler_rank extensions
        if {[lsearch -exact ${copied_icons} ${icon_file}] < 0} {
            xinstall -m 0644 \
                ${file_icons_dir}/${icon_file} \
                ${dest_app_res_dir}/${icon_file}
            lappend copied_icons ${icon_file}
        }
    }
}

post-destroot {
    app::post_destroot

    # file associations
    set plistbuddy /usr/libexec/PlistBuddy
    exec ${plistbuddy} -c "Add :CFBundleDocumentTypes array" ${dest_app_info_plist}

    set i 0
    foreach document_type ${document_types} {
        lassign ${document_type} type_name icon_file type_role handler_rank extensions

        exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i} dict" ${dest_app_info_plist}
        exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeExtensions array" ${dest_app_info_plist}

        set j 0
        foreach ext ${extensions} {
            exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeExtensions:${j} string ${ext}" ${dest_app_info_plist}
            incr j
        }

        exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeIconFile string ${icon_file}" ${dest_app_info_plist}
        exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeName string ${type_name}" ${dest_app_info_plist}
        exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:CFBundleTypeRole string ${type_role}" ${dest_app_info_plist}
        exec ${plistbuddy} -c "Add :CFBundleDocumentTypes:${i}:LSHandlerRank string ${handler_rank}" ${dest_app_info_plist}

        incr i
    }

    app::sign
}

variant media description {enable and install dependencies for media downloading support} {
    set ::ffmpeg_ver \
                    8
    depends_run-append \
                    port:ffmpeg${::ffmpeg_ver} \
                    port:n-m3u8dl-re

    post-patch {
        # change default ffmpeg directory
        reinplace "s|{APP_DATA_DIR}/FFmpeg|${prefix}/libexec/ffmpeg${::ffmpeg_ver}|g" ${worksrcpath}/features/ffmpeg_pack/config.py
        # change default n-m3u8dl-re directory
        reinplace "s|{APP_DATA_DIR}/M3U8DL|${prefix}/bin|g" ${worksrcpath}/features/m3u8_pack/config.py
    }
}

default_variants    +media
