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

github.setup        macfuse macfuse 4.10.2 macfuse-
revision            0
name                macfuse
conflicts           osxfuse
categories          fuse
platforms           {darwin > 12}
supported_archs     arm64 x86_64
license             Restrictive
maintainers         {makr @mohd-akram} {ra1nb0w @ra1nb0w} openmaintainer

description         FUSE for macOS
long_description    FUSE extends macOS by adding support for user space file systems.

homepage            https://macfuse.github.io

github.tarball_from releases
distname            ${name}-${version}
use_dmg             yes

checksums           rmd160  85f2837123a9185d5123b6f4bed737335e11c059 \
                    sha256  fcca8ec48742288dccdd36db65bb92c4fe4bc23c2c7c5b76aa0ac68e06aebe88 \
                    size    6476618

set pkg ${workpath}/pkg/Core.pkg

post-extract {
    system -W ${worksrcpath} \
        "pkgutil --expand 'Extras/macFUSE ${version}.pkg' ${workpath}/pkg"
    system -W ${pkg} "gzip -dc Payload | cpio -id"
}

use_configure       no

build {
    foreach file {libfuse.2.dylib libfuse3.4.dylib} {
        system -W ${pkg} "install_name_tool -id ${prefix}/lib/$file \
            usr/local/lib/$file"

        system -W ${pkg} "install_name_tool \
            -id ${prefix}/Library/Frameworks/macFUSE.framework/Versions/A/macFUSE \
            -change /usr/local/lib/$file ${prefix}/lib/$file \
            Library/Frameworks/macFUSE.framework/Versions/A/macFUSE"

        system -W ${pkg} "codesign -fs - \
            usr/local/lib/$file \
            Library/Frameworks/macFUSE.framework/Versions/A/macFUSE"
    }
}

set fs_dir /Library/Filesystems
set dir ${fs_dir}/macfuse.fs

destroot {
    copy ${pkg}/Library/Filesystems ${destroot}${prefix}/Library
    xinstall -W ${pkg}${dir}/Contents/Resources -m 4755 load_macfuse mount_macfuse \
        ${destroot}${prefix}${dir}/Contents/Resources
    copy {*}[glob ${pkg}/Library/Frameworks/*] ${destroot}${prefix}/Library/Frameworks
    copy {*}[glob ${pkg}/usr/local/include/*] ${destroot}${prefix}/include
    copy {*}[glob ${pkg}/usr/local/lib/pkgconfig/*] ${destroot}${prefix}/lib/pkgconfig
    reinplace "s|/usr/local|${prefix}|g" {*}[glob ${destroot}${prefix}/lib/pkgconfig/*]
    copy {*}[glob -type f ${pkg}/usr/local/lib/*] ${destroot}${prefix}/lib
    # remove unwanted files like ._uninstall_macfuse.app
    system "find ${destroot}${prefix} -type f -iname '._*' -delete"
}

variant fs_link description "Link ${name} to a .fs bundle in /Library/Filesystems" {
    post-destroot {
        xinstall -d ${destroot}${fs_dir}
        ln -sf ${prefix}${dir} ${destroot}${dir}
    }

    destroot.violate_mtree \
                    yes
}

if {![variant_isset fs_link]} {
    notes "
    Run the following before using macFUSE:

        sudo ln -fsn ${prefix}${dir} ${dir}

    Alternatively, use the +fs_link variant to automatically create this symlink.
    "
}
