# -*- 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.2.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 a6c52a104b81f8f9d5e83602f7a9010e50332a96 \
                    sha256 61b1abf6a1eebcad246e6703ad3e5c86f6d7e84e94a0a8fa48fe16c04e7bc703 \
                    size 4369769

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) for Xcode 16.x compatibility
    # The 'unsafe' keyword 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' {} \\;"

    # 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
"

