# -*- 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.4 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  b8e4d77fee26a86b43a0555b7c4b52cbe8326ad5 \
                    sha256  a4ad624407ed3f06703af3eebf44077d8ef555f0973c63679f1373c76be74ea8 \
                    size    12871483

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 -W ${worksrcpath}/Support/ "/usr/libexec/PlistBuddy -c \"Set :LSMinimumSystemVersion ${macosx_deployment_target}\" Info.plist"
    reinplace "s|/usr/local|${prefix}|g" ${worksrcpath}/Sources/FtopSensors/ProcessProvider.swift
    # 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
    set helper_dir ${destroot}${prefix}/libexec/${name}
    xinstall -d ${app_bin_dir} ${app_dir}/Contents/Resources ${helper_dir}
    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}

    # Helper binary needs setuid
    file attributes ${app_bin_dir}/ftop-helper -permissions u+s

    # 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/
    ln -s ${applications_dir}/Ftop.app/Contents/MacOS/ftop-helper ${helper_dir}/

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

post-activate {
    # The symlink to the helper binary needs setuid too, the activation phase clears it
    system -W ${prefix}/libexec/${name} "chmod -h u+s ftop-helper"
}
