# $Id: Portfile 50980 2009-05-14 19:41:55Z and.damore@macports.org $

PortSystem 1.0

name                macfuse
version             1.7
revision            1
categories          fuse devel
platforms           macosx
maintainers         eridius
description         A FUSE-Compliant File System Implementation Mechanism for Mac OS X
long_description    MacFUSE implements a mechanism that makes it possible to implement \
                    a fully functional file system in a user-space program on Mac OS X \
                    (10.4 and above). 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. The core of MacFUSE is in a dynamically loadable kernel extension.

homepage            http://code.google.com/p/macfuse/
master_sites        macports
distname            macfuse-${version}-1
worksrcdir          macfuse-core
checksums           md5 e2a8d2dcf116ec806d47bd050b77dd23 \
                    sha1 5989a81a86ce74fadedf9c06119c6c279cd1cb6c

patchfiles          patch-10.4-project.pbxproj \
                    patch-10.5-project.pbxproj \
                    patch-10.5-packaging-macfuse-core-make-pkg.sh \
                    patch-build_macfuse.sh

livecheck.distname  MacFUSE-Core-(.*).dmg
livecheck.version   10.5-${version}.1

use_bzip2           yes

use_configure       no

destroot.violate_mtree yes

# patchfiles          patch-project.pbxproj

variant universal {
    # provide a dummy variant since the script will actually build universal
    # and we don't want to error out if someone says +universal
}

# ensure that user is running OS X 10.4 or higher
pre-fetch {
    if {${os.platform} != "darwin" || [rpm-vercomp ${os.version} 8.0.0] < 0} {
        return -code error "MacFUSE requires at least Mac OS X 10.4 to run"
    }
}

build {
    system "cd ${worksrcpath} && ./build_macfuse.sh | sed -ne '/^created: /h;p;$\{g;s/^created: //
                w ../dmgname
            \}'; exit $\{PIPESTATUS\[0\]\}"
}

destroot {
    delete ${destpath}${prefix}
    set dmgname [exec cat ${workpath}/dmgname]
    # we know the build script generates a .tar.bz2 right next to the dmg
    set tarname [string tolower [string replace $dmgname end-2 end "tar.bz2"]]
    system "tar -xjf $tarname -C ${destpath}"
    file rename ${destpath}/usr/local ${destpath}${prefix}
    foreach f [list "pkgconfig/fuse.pc" "libfuse.la" "libulockmgr.la"] {
        set path "${destpath}${prefix}/lib/${f}"
        reinplace s,/usr/local,${prefix}, $path
    }
    set libfuse_version 0
    foreach lib [list "libulockmgr.0" "libfuse.${libfuse_version}"] {
        set libpath "${prefix}/lib/${lib}.dylib"
        system "install_name_tool -id ${libpath} ${destpath}${libpath}"
    }
    system "install_name_tool -change /usr/local/lib/libfuse.${libfuse_version}.dylib ${prefix}/lib/libfuse.${libfuse_version}.dylib ${destpath}/Library/Frameworks/MacFUSE.framework/Versions/A/MacFUSE"
    # delete the whole directory the build script creates
    # when updating this Portfile DOUBLE CHECK THIS LINE
    delete [file dirname $dmgname]
}

post-activate {
    if {[string length [exec kextstat -lb com.google.filesystems.fusefs]] > 0} {
        ui_msg "********************************************************"
        ui_msg "*  fusefs is already loaded. You may need to restart.  *"
        ui_msg "*  Alternatively, if feeling adventurous, you can run  *"
        ui_msg "*  `sudo kextunload -b com.google.filesystems.fusefs`  *"
        ui_msg "********************************************************"
    }
}
