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

github.setup        nullpointerdepressivedisorder MiddleDrag 1.4.3 v
github.tarball_from archive
revision            0
categories          sysutils
maintainers         {@NullPointerDepressiveDisorder middledrag.app:hello} \
                    openmaintainer
license             MIT
homepage            https://middledrag.app
description         Three-finger trackpad gestures for middle-click and middle-drag on macOS
long_description    Mac trackpads don't have a middle mouse button. Many apps expect one. \
                    MiddleDrag fixes this. Three-finger tap for middle-click. Three-finger \
                    drag for middle-drag. Works alongside Mission Control and other system \
                    gestures.

platforms           {darwin >= 24}
supported_archs     x86_64 arm64

use_configure       no
use_xcode           yes

minimum_xcodeversions {24 16.4 25 16.4 26 16.4}

checksums           rmd160 c090fa747ff7b11f5b9a3480f48e73fc8bfb58ab \
                    sha256 4a6715c14d7c1ffa8280dc4a35861b0c0e4d27c5b63fb444fa06ae37eeb18e85 \
                    size 4377866

xcode.scheme        MiddleDrag
xcode.configuration Release
xcode.destroot.type none

build.pre_args-append \
                    -IDEPackageSupportDisableManifestSandbox=1 \
                    -IDEPackageSupportDisablePluginExecutionSandbox=1 \
                    -derivedDataPath ${workpath}/DerivedData

xcode.build.settings-append \
                    OTHER_LDFLAGS='-F/System/Library/PrivateFrameworks -framework MultitouchSupport -framework CoreFoundation -framework CoreGraphics' \
                    FRAMEWORK_SEARCH_PATHS='/System/Library/PrivateFrameworks' \
                    OTHER_SWIFT_FLAGS='-disable-sandbox' \
                    SWIFT_STRICT_MEMORY_SAFETY=NO \
                    ENABLE_USER_SCRIPT_SANDBOXING=NO \
                    SDKROOT=macosx \
                    MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target} \
                    CODE_SIGN_IDENTITY=- \
                    CODE_SIGNING_ALLOWED=NO \
                    CODE_SIGN_STYLE=Manual \
                    PROVISIONING_PROFILE_SPECIFIER= \
                    DEVELOPMENT_TEAM= \
                    CODE_SIGNING_REQUIRED=NO

post-patch {
    # Remove Swift 6.2 strict memory safety keywords (unsafe/@ unsafe, etc.) for Xcode 16.x compatibility
    # The 'unsafe' and 'safe' keywords requires Swift 6.2 (Xcode 26+) per SE-0458
    system -W ${worksrcpath} "find MiddleDrag -name '*.swift' -exec sed -i '' -e 's/@unsafe //g' -e 's/unsafe //g' {} \\;"
    system -W ${worksrcpath} "find MiddleDrag -name '*.swift' -exec sed -i '' -e 's/@safe //g' -e 's/safe //g' {} \\;"

    # Disable Sparkle auto-update for MacPorts-managed installation
    system -W ${worksrcpath} "/usr/libexec/PlistBuddy -c \"Delete :SUFeedURL\" Info.plist || true"
    system -W ${worksrcpath} "/usr/libexec/PlistBuddy -c \"Set :SUEnableAutomaticChecks false\" Info.plist || true"
}

destroot {
    file copy ${worksrcpath}/build/${xcode.configuration}/MiddleDrag.app ${destroot}${applications_dir}/MiddleDrag.app
}

notes "
MiddleDrag requires Accessibility permissions to function.
After launching, grant permission in:
System Settings > Privacy & Security > Accessibility
"

