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

name                osxfuse
epoch               2
# Note: do not update past 3.8.3
# The current maintainer has decided not to make source available for
# new versions of the software past that version. Until he relents or
# until someone forks the package, we're stuck at this version
version             3.8.3
set branch          [join [lrange [split ${version} .] 0 1] .]
categories          fuse devel
platforms           macosx
license             BSD APSL
maintainers         nomaintainer

description         A FUSE-Compliant File System Implementation Mechanism \
                    for Mac OS X
long_description    FUSE for OS X implements a mechanism that makes it \
                    possible to implement a fully functional file system \
                    in a user-space program on Mac OS X. It aims to be \
                    API-compliant with the FUSE (File-system in USErspace) \
                    mechanism that originated on Linux.  Therefore, many \
                    existing FUSE file systems become readily usable on \
                    Mac OS X. This port provides the user-space library \
                    and header files for building filesystems.

homepage            https://osxfuse.github.io/

conflicts           macfuse

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     "FUSE for macOS (\\d+(?:\\.\\d+)*)"

# Use the published signed kext for OS 10.9 (Darwin 13) and later.
# Don't even fetch and extract the dmg unnecessarily, since the commands don't
# work on some earlier OS versions.
if {${os.major} >= 13} {
    set use_signed_kext    yes
} else {
    set use_signed_kext    no
}

# We will build user-space components for the specified arch, and
# kernel modules for the kernel arch.
set kernel_arch [exec uname -m]
# If building for different kernel arch than our buildslaves use, force a build
# from source. The binary package IDs don't encode the kernel arch.
if {${kernel_arch} ne "x86_64"} {
    archive_sites
}

distfiles
dist_subdir ${name}/${version}
set mp.dist {
    osxfuse     48bc246
    kext        416c143
    framework   8b79906
    prefpane    095ba9c
    fuse        1a1977a
    support     2cdc560
}

depends_build       port:autoconf \
                    port:automake \
                    port:libtool

set mp.osxfuse_rev  [lindex ${mp.dist} [expr [lsearch ${mp.dist} osxfuse] + 1]]
worksrcdir          osxfuse-osxfuse-${mp.osxfuse_rev}

foreach { mp.comp mp.rev } ${mp.dist} {
    set f ${mp.comp}-${mp.rev}.tar.gz
    master_sites-append https://github.com/osxfuse/${mp.comp}/tarball/${mp.rev}/:${mp.comp}
    distfiles-append    ${f}:${mp.comp}
}

if { $use_signed_kext } {
    master_sites-append https://github.com/osxfuse/osxfuse/releases/download/osxfuse-${version}
    distfiles-append ${name}-${version}.dmg
}

checksums           osxfuse-48bc246.tar.gz \
                    rmd160  e36ee956945058e54573eb7bd705664beac87ccf \
                    sha256  cc810302c14e1e06808a95647fffd2e0655b201ba3e14b390059d5d150c164d7 \
                    size    32351 \
                    kext-416c143.tar.gz \
                    rmd160  05417d7d42aec901e1f93f764514460b477e4492 \
                    sha256  011b03b8819cdc467ef923a2dc97feae7915a2db77da7d6195c2fc98e6045f42 \
                    size    119375 \
                    framework-8b79906.tar.gz \
                    rmd160  d3ad907fc8ed42fb7ca5cdf08ceb8aa09ba1ec74 \
                    sha256  f08a2e063aa6b3299e4edc4cd898a557c20b6d6c85eb26cd489eb6a642d682e6 \
                    size    35758 \
                    prefpane-095ba9c.tar.gz \
                    rmd160  c7794cd3d644cf1f036b248d21a83901d93187ca \
                    sha256  2426c4669aeb1c1179e5e89af46f2d9a30743f6929419c6414c7eeb8b3212fae \
                    size    5346030 \
                    fuse-1a1977a.tar.gz \
                    rmd160  26735fb56773d142f1e9a9a0fed3e64ec1fe6cdd \
                    sha256  562f92a9a2b1acad725d8ea86494e53ed4f8bf48cf2664f964d069552b236e97 \
                    size    231739 \
                    support-2cdc560.tar.gz \
                    rmd160  096fc6f329f626ca63d9ec5901004173357327bd \
                    sha256  1c65b389628d5a675d700330143c55c60854faafd791a0743a05cf310721fcf8 \
                    size    3391234 \
                    osxfuse-3.8.3.dmg \
                    rmd160  4c26f209dd60329ebe97a0f4c65b374181142584 \
                    sha256  87e507c44c19689beefa3d47dd00ba953254d9e616cb633c1b4343407fe99700 \
                    size    6967386

# extract phase will just extract the dmg; post-extract will expand
# the tarballs
if { $use_signed_kext } {
    use_dmg yes
    extract.only ${name}-${version}.dmg
}

variant bridge_support description {Build BridgeSupport metadata} {}

post-extract {
    # Extract the pkg and the appropriate payload from the binary dmg
    if { $use_signed_kext } {
        system -W ${workpath}/${name}-${version} "pkgutil --expand 'FUSE for macOS.pkg' ${workpath}/pkg"
        system -W ${workpath}/pkg/Core.pkg "gzip -dc Payload | cpio -id"
    }

    # Extract the source tarballs
    foreach { mp.comp mp.rev } ${mp.dist} {
        if { $use_signed_kext } {
            system -W ${workpath} "tar -xvf ${distpath}/${mp.comp}-${mp.rev}.tar.gz"
        }
        if {${mp.comp} ne "osxfuse"} {
            # Replace existing empty directory if it exists
            file delete ${workpath}/${worksrcdir}/${mp.comp}
            move ${workpath}/osxfuse-${mp.comp}-${mp.rev} ${workpath}/${worksrcdir}/${mp.comp}
        }
    }

    if {![variant_isset bridge_support]} {
        reinplace "s|/usr/bin/gen_bridge_metadata|/usr/bin/gen_bridge_metadata-disabled|" \
            ${worksrcpath}/framework/OSXFUSE.xcodeproj/project.pbxproj
    }
}

pre-build {
    if {[variant_isset bridge_support] && ${os.major} >= 17 && [catch {system "/usr/bin/gen_bridge_metadata --version > /dev/null 2>&1"}]} {
        ui_error "This port will fail to build because of a bug in macOS,"
        ui_error "unless you apply one of the following two workarounds:"
        ui_error ""
        ui_error "    sudo mkdir -p /usr/local/lib"
        ui_error "    sudo ln -s \$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib /usr/local/lib"
        ui_error ""
        ui_error "or:"
        ui_error ""
        ui_error "    sudo ln -s XcodeDefault.xctoolchain \$(xcode-select -p)/Toolchains/OSX10.13.xctoolchain"
        ui_error ""
        ui_error "See https://trac.macports.org/ticket/54939 for more information."
        return -code error "gen_bridge_metadata workaround not performed"
    }
}

if { ! $use_signed_kext } {
    patchfiles      patch-build.d_targets_packagemanager.sh.diff

    if {${configure.build_arch} in [list ppc ppc64]} {
        patchfiles-append \
                    patch-fix-valid-archs.diff
    }
}

patchfiles-append   patch-IDECustomDerivedData.diff

post-patch {
    reinplace "s|@@WORKSRCPATH@@|${worksrcpath}|" ${worksrcpath}/build.sh
}

use_configure   no

build.cmd       ./build.sh -t packagemanager
build.target    -a build
build.args      -v 5 \
                --build-directory="${workpath}" -- \
                -a [join [get_canonical_archs] " -a "] \
                --framework-prefix="${prefix}" \
                --fsbundle-prefix="${prefix}" \
                --library-prefix="${prefix}"

# Clear CPATH and LIBRARY_PATH because a ncurses include file
# conflicts with the one in MacPorts. It doesn't matter much anyway
# because osxfuse doesn't have any dependencies to link against.
compiler.cpath
compiler.library_path

destroot.target -a install
destroot.args   -v 5 --build-directory="${workpath}"
destroot.destdir -- "${destroot}${prefix}"


# On Mavericks and Yosemite, replace the kext we just built with the
# one from the binary distribution. This works around OS X's
# unwillingness to load an unsigned kext (and our inability to sign
# kexts). See #45521.
#
# Unlike newer versions, Mavericks does not require the kext to be
# signed, but will display a warning if it's unsigned -- so use the
# signed binary instead here too.
if { $use_signed_kext } {
    post-destroot {
        delete ${destroot}${prefix}/Library/Filesystems/osxfuse.fs/Contents/Extensions
        file copy ${workpath}/pkg/Core.pkg/Library/Filesystems/osxfuse.fs/Contents/Extensions \
            ${destroot}${prefix}/Library/Filesystems/osxfuse.fs/Contents
    }
}

post-destroot {
    # Set proper permissions
    fs-traverse f ${destroot}${prefix}/Library {
        file attributes $f -owner root -group wheel
    }
    # only if macOS > 10.8
    if {${os.platform} eq "darwin" && ${os.major} > 12} {
        # Enable setuid on helper binary
        file attributes ${destroot}${prefix}/Library/Filesystems/osxfuse.fs/Contents/Resources/load_osxfuse -permissions 04755
    }
}

destroot.violate_mtree yes

notes {
    When upgrading, unmount all FUSE filesystems and then unload the kernel extension.
    Unloading can be done via: sudo kextunload -b com.github.osxfuse.filesystems.osxfuse
    Alternatively (or if this fails), just reboot your computer now.
}

# Could probably be supported by setting ARCHS correctly above
universal_variant no
