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

PortSystem          1.0
PortGroup           github 1.0

github.setup        Nongfsq ftop 0.2.1 v
github.tarball_from archive
revision            0
categories          sysutils aqua
platforms           {macosx >= 24}
supported_archs     arm64
license             MIT
maintainers         {i0ntempest @i0ntempest} openmaintainer

description         Floating system monitor for Apple Silicon Macs
long_description    ${name} displays CPU usage and frequency, memory pressure, \
                    network activity, GPU usage, power, temperature, and the \
                    busiest processes in a resizable native floating panel.

checksums           rmd160  4818208582f41527db111d4beaf1a0f7f5acd763 \
                    sha256  4a7eefd2afe3c33ff04e3cfa92f012da3a7de93b81e9e822e210ef181c05a0ef \
                    size    12586225

use_configure       no
universal_variant   no
installs_libs       no

# Avoid headers in ${prefix}/include shadowing the SDK's Clang modules.
compiler.cpath

post-patch {
    reinplace "s|\\.macOS(\\.v15)|.macOS(\"${macosx_deployment_target}\")|" ${worksrcpath}/Package.swift
    system "/usr/libexec/PlistBuddy -c \"Set :LSMinimumSystemVersion ${macosx_deployment_target}\" ${worksrcpath}/Support/Info.plist"
    # Disable automatic and manual update checks; MacPorts manages upgrades.
    reinplace {s|guard !busy, let current = Self\.current|guard false, !busy, let current = Self.current|} ${worksrcpath}/Sources/FtopApp/Updater.swift
}

pre-fetch {
    if {${xcodecltversion} eq "none" || [vercmp ${xcodecltversion} < 16.0]} {
        return -code error "${name} requires Xcode/CLT 16.0 or later (Swift 6)."
    }
}

# SwiftPM's sandbox cannot be nested inside the MacPorts build sandbox.
# Its shared paths must also stay inside the writable work directory.
set swift_build_flags \
                    "--disable-sandbox --jobs ${build.jobs} \
                    --cache-path ${workpath}/swiftpm/cache \
                    --config-path ${workpath}/swiftpm/configuration \
                    --security-path ${workpath}/swiftpm/security"

build.cmd           swift
build.target        build
build.args          --configuration release {*}${swift_build_flags}

test.run            yes
test.cmd            swift
test.target         test
test.args           {*}${swift_build_flags}

destroot {
    # Match the bundle layout in upstream's scripts/bundle.sh.
    set app_dir ${destroot}${applications_dir}/Ftop.app
    set app_bin_dir ${app_dir}/Contents/MacOS
    xinstall -d ${app_bin_dir} ${app_dir}/Contents/Resources
    xinstall -m 0644 ${worksrcpath}/Support/Info.plist ${app_dir}/Contents
    xinstall -m 0644 ${worksrcpath}/Support/AppIcon.icns ${app_dir}/Contents/Resources

    # FtopPanel and ftop must have distinct names on case-insensitive disks.
    xinstall -m 0755 ${worksrcpath}/.build/release/FtopApp ${app_bin_dir}/FtopPanel
    xinstall -m 0755 -W ${worksrcpath}/.build/release ftop ftop-helper ${app_bin_dir}

    # Sign the auxiliary executables first, then the bundle's main executable.
    system -W ${app_bin_dir} "/usr/bin/codesign --force --sign - ftop-helper ftop"
    system -W ${destroot}${applications_dir} "/usr/bin/codesign --force --sign - Ftop.app"

    ln -s ${applications_dir}/Ftop.app/Contents/MacOS/ftop ${destroot}${prefix}/bin/ftop

    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 ${worksrcpath}/LICENSE ${destroot}${prefix}/share/doc/${name}
}
