# -*- 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
# $Id: Portfile 90295 2012-03-01 00:55:16Z dports@macports.org $

PortSystem          1.0
PortGroup           xcode 1.0

name                fuse4x-kext
version             0.9.0
revision            1
set tag             [string map {. _ } $version]
categories          fuse devel
platforms           macosx
maintainers         dports
description         A kernel extension for Fuse4X

long_description    Fuse4X 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 dynamically loadable       \
                    kernel extension at the core of Fuse4X.

homepage            http://fuse4x.org/
license             BSD
master_sites        https://github.com/fuse4x/kext/tarball/fuse4x_${tag}

checksums           sha256  8001a515945ab3ea72df788e89ef66ce608e06ba1444163bfbba928056d0fad5 \
                    rmd160  89578f947aa916e6435e011ea7964673e213ca3f

extract.mkdir       yes
extract.post_args   "| tar --strip-components=1 -xf -"

# This port installs a kernel module, so it must be built for the same
# architecture as the kernel. We don't have a way to get that;
# build_arch is the userland architecture, which may be different
# (e.g. 64-bit userland with 32-bit kernel). So build a universal
# binary by default on Intel platforms. On PPC, there's only one
# kernel, so don't try to build it ppc64 or universal.
supported_archs     i386 x86_64 ppc
if {$build_arch == "i386" || $build_arch == "x86_64"} {
    default_variants    +universal
}

# kext destination: kext will be installed into ${kextdir}/fuse4x.kext.
#
# The kext is autoloaded by the library using load_fuse4x, so it
# doesn't have to go into /System/Library/Extensions. It doesn't
# matter where we put it, as long as it's kept in sync with the library.
#
# If updating the patchfile or ${kextdir}, must also update the
# corresponding patch in the fuse4x port!
patchfiles          patch-common_fuse_param.h.diff
set kextdir        "${prefix}/Library/Extensions"
post-patch {
    reinplace "s|@@KEXTPATH@@|${kextdir}/fuse4x.kext|" ${worksrcpath}/common/fuse_param.h
}

xcode.configuration Release

# Disable MacFUSE compatibility mode; not needed for the filesystems we build.
xcode.build.settings GCC_PREPROCESSOR_DEFINITIONS='FUSE4X_DISABLE_MACFUSE_MODE'

destroot.asroot     yes

destroot {
    xinstall -d ${destroot}${kextdir}
    file copy ${worksrcpath}/build/${xcode.configuration}/fuse4x.kext ${destroot}${kextdir}
    file attributes ${destroot}${kextdir}/fuse4x.kext -owner root -group wheel -permissions rwxr-xr-x

    xinstall -d ${destroot}${kextdir}/fuse4x.kext/Support
    xinstall -o root -m 4755 ${worksrcpath}/build/${xcode.configuration}/load_fuse4x ${destroot}${kextdir}/fuse4x.kext/Support
}


variant trace description {Enable debug logging: logs all filesystem operations} {
    # Note that this overrides the normal GCC_PREPROCESSOR_DEFINITIONS
    xcode.build.settings GCC_PREPROCESSOR_DEFINITIONS='FUSE4X_DISABLE_MACFUSE_MODE FUSE4X_DEBUG FUSE4X_TRACE FUSE4X_TRACE_LK FUSE4X_TRACE_MSLEEP FUSE4X_TRACE_OP FUSE4X_TRACE_VNCACHE FUSE4X_SERIALIZE_LOGGING'
}

# We don't need to load the kext, because the fuse4x library will
# autoload it when needed. However, if there's an existing version
# loaded, we'd better unload it because it might be a different
# version.
#
# Thus, check whether the kext is already loaded, and if so attempt to
# unload it. If that fails, the user probably needs to unmount some
# running filesystems; print a list.
#
# We do this both after deactivating an existing version (because the
# user might be removing the port) and after activating a new version
# (because the user might be upgrading from an installation that
# didn't unload the kext).
proc try_unload_kext {} {
    set kextid "org.fuse4x.kext.fuse4x"
    if {[string length [exec /usr/sbin/kextstat -lb $kextid]] > 0} {
        ui_msg "The Fuse4X kernel extension is already loaded. Attempting to unload it."
        if {![catch {exec /sbin/kextunload -b $kextid}]} {
            ui_msg "Successfully unloaded the previous installation of Fuse4X"
        } else {
            set mounted [exec /sbin/mount -t fuse4x]
            ui_warn "Unable to unload the existing Fuse4X kernel extension."
            ui_warn "Please unmount the following filesystems, and then run "
            ui_warn "`sudo kextunload -b $kextid`:"
            foreach line [split $mounted "\n"] {
                regexp {on (/.*) \(fuse4x} $line -> mountpoint
                ui_warn "  * $mountpoint"
            }
        }
    }
}

post-activate {
    try_unload_kext
}

post-deactivate {
    try_unload_kext
}

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     {Fuse4X-([0-9.]+).dmg}
