# -*- 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 89810 2012-02-11 08:55:39Z and.damore@macports.org $

PortSystem 1.0

name                macfuse
version             2.0.3
revision            5
categories          fuse devel
# FUSE library is LGPL-2; kernel module and Obj-C SDK are BSD-licensed
license             {BSD LGPL-2}
platforms           macosx
maintainers         dports
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. 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
use_bzip2           yes
worksrcdir          core
checksums           md5     97ab76bbade128828596aca878c6c475 \
                    sha1    60ea1536091740e2faeb9ebf72d8696ccd11ce2f \
                    rmd160  e9a79de37d84ee5e21f63f02b924d04a5df94636

depends_build       port:pkgconfig port:autoconf port:automake port:libtool
depends_lib         port:libiconv

patchfiles          patch-10.5-to-10.6.diff \
                    patch-10.5-to-10.7.diff \
                    patch-macfuse_buildtool.sh.diff \
                    patch-sdk-objc_GMUserFileSystem.m.diff \
                    patch-sdk-objc_MacFUSE.xcodeproj-default-gcc.diff \
                    patch-sdk-objc_MacFuse.xcodeproj.diff \
                    patch-10.5_fusefs_fusefs.xcodeproj_project.pbxproj.diff \
                    patch-10.5_universal_build.diff \
                    patch-10.5_libfuse_fuse-2.7.3-macosx.patch.diff \
                    patch-10.6_libfuse_fuse-2.7.3-macosx.patch.diff \
                    patch-10.7_libfuse_fuse-2.7.3-macosx.patch.diff \
                    patch-10.7-cr_posix.diff \
                    patch-10.7_fusefs_mount_mount_fusefs.c.diff

livecheck.type      googlecode
livecheck.distname  MacFUSE-(.*)%2C.*.dmg

use_configure       no

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
}

# error out if running a 64-bit kernel; MacFUSE is 32-bit only
pre-archivefetch {
    set kernel_arch [exec /usr/bin/uname -m]
    if {${kernel_arch} == "x86_64" || ${kernel_arch} == "ppc64"} {
        return -code error "MacFUSE does not support 64-bit kernels; consider installing Fuse4X instead"
    }
}

# ensure that user is running OS X 10.5 or higher: MacFUSE requires XCode 3
pre-fetch {
    if {${os.platform} != "darwin" || [vercmp ${os.version} 9.0.0] < 0} {
        return -code error "MacFUSE requires at least Mac OS X 10.5 to build"
    }
}

# copy 10.5 source directory to 10.6 to prepare for the 10.5-to-10.6 patch
# (this patch really should be applied upstream)
#
# use cp -R instead of cp -r or copy to preserve symlinks
pre-patch {
    system "cp -R ${worksrcpath}/10.5 ${worksrcpath}/10.6"
    system "cp -R ${worksrcpath}/10.5 ${worksrcpath}/10.7"
}

post-patch {
    reinplace s,/usr/local,${prefix},g ${worksrcpath}/macfuse_buildtool.sh
    reinplace s,/usr/local,${prefix},g ${worksrcpath}/sdk-objc/MacFUSE.xcodeproj/project.pbxproj
    reinplace s,@@PREFIX@@,${prefix},g ${worksrcpath}/10.5/libfuse/fuse-2.7.3-macosx.patch
    reinplace s,@@PREFIX@@,${prefix},g ${worksrcpath}/10.6/libfuse/fuse-2.7.3-macosx.patch
    reinplace s,@@PREFIX@@,${prefix},g ${worksrcpath}/10.7/libfuse/fuse-2.7.3-macosx.patch
    reinplace s,@@WORKPATH@@,${workpath},g ${worksrcpath}/macfuse_buildtool.sh
    reinplace s,@@DESTPATH@@,${destpath},g ${worksrcpath}/macfuse_buildtool.sh
}

build.asroot    yes
# macfuse_buildtool has been patched to install directly into ${destpath},
# so destroot phase is unnecessary
build {
    system "cd ${worksrcpath} && ./macfuse_buildtool.sh -t smalldist"
    
    # Set file permissions on kernel module and binaries (see #25609)
    system "chown -R root:wheel ${destpath}/Library/Filesystems/fusefs.fs"
    file attributes ${destpath}/Library/Filesystems/fusefs.fs/Support/load_fusefs -permissions 04755
}

destroot.violate_mtree yes

destroot {
}

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 "********************************************************"
    }
}
