# -*- 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           golang 1.0
PortGroup           fuse 1.0

go.setup            github.com/rfjakob/gocryptfs 2.7.0 v
revision            0

categories          fuse
maintainers         {i0ntempest @i0ntempest} openmaintainer
license             MIT
description         Encrypted overlay filesystem written in Go
long_description    {*}${description}
homepage            https://nuetzlich.net/gocryptfs/

checksums           ${distname}${extract.suffix} \
                        rmd160  d2c8cf33ba8f674705d28bb019cca33821fba430 \
                        sha256  71cf484a4c1a26c2d1286bdd39ba19bbbd558b4c6c08574bb3177c8817a1f517 \
                        size    1406275

go.vendors          golang.org/x/text \
                        lock    v0.43.0 \
                        rmd160  c41d464475010b28049f857a1835cfc11659d489 \
                        sha256  9c0870bb9fcf5822f6ca770669ba5f1095f82b37dc0e4ce731bea533cdfda54b \
                        size    6768792 \
                    golang.org/x/term \
                        lock    v0.47.0 \
                        rmd160  3fc4c9d049b1f3c841ecbd1f9695ae328823e648 \
                        sha256  5fb2a607f0b47d13f8d14e3856bf1cd0b9166420f7d0aa64194ac355eb7f7960 \
                        size    16850 \
                    golang.org/x/sys \
                        lock    v0.49.0 \
                        rmd160  6ed878be1d13d77e0d3edddde72f331e07838490 \
                        sha256  a3b736fb807cc342af8da79303e000c845e6d7a3acda8b6404de1477dad0d580 \
                        size    1574657 \
                    golang.org/x/crypto \
                        lock    v0.58.0 \
                        rmd160  d28a3b1db2bae18ae35d0360cfc88a219e208cdc \
                        sha256  ff0fd89fb74658f3648ad504dd7c09225742f53dc3476cf9123b44419349ea51 \
                        size    2192966 \
                    github.com/spf13/pflag \
                        lock    v1.0.10 \
                        rmd160  eb2e68c61d88ce1e22cf4b75b31de9815ee375b5 \
                        sha256  8e29e9893c782030a639e4e1ff4442cef352fa54b64aaa11c35163d550ce10dd \
                        size    63166 \
                    github.com/sabhiram/go-gitignore \
                        lock    525f6e181f06 \
                        rmd160  dedbb7e6dcd1cf8dd5f625090fa8c6b4af8430d6 \
                        sha256  1966d81c0496a29d36e15731d3290bbc71be1b8ca3745a58b364d8a18972b153 \
                        size    8886 \
                    github.com/rfjakob/eme \
                        lock    v1.2.0 \
                        rmd160  64918654d87bef44830466e76d543a41f3c8fa21 \
                        sha256  4b071f5bac31f6ffa4d22d025605767374c637f154120f40a617e73798a9cda0 \
                        size    24567 \
                    github.com/pkg/xattr \
                        lock    v0.4.9 \
                        rmd160  9a3c128c3a1f66f0086c7e0991830cfec5c6407c \
                        sha256  58d70e46c81345035cbf3df6d6bb4a59c43c9a4c68c248801f701be5a1d79bce \
                        size    9642 \
                    github.com/moby/sys/mountinfo \
                        lock    mountinfo/v0.7.2 \
                        rmd160  11470bb42f47fe6c862a063760a88d10d3221a04 \
                        sha256  99bb4c2a8a867da0f5b1bd61cd9564893776901cff05ca74b1ff9e47723ed167 \
                        size    69146 \
                    github.com/hanwen/go-fuse/v2 \
                        lock    2d50fff207d7 \
                        rmd160  b78314537a5f00cf33c331b24cf37b150b84a015 \
                        sha256  b7276c3c15e8c1152ffcdb4df9595be2068bd1b2d8fa50f9abbfd62dc0b32f5e \
                        size    233595 \
                    github.com/aperturerobotics/jacobsa-crypto \
                        lock    v1.1.0 \
                        rmd160  00d1922ba828943d42f05b0f796863d1ddf68e2b \
                        sha256  35ea86d147e1d3909ee96c8f9997038e714fdf8dfd55b8191c9d623dd747c027 \
                        size    3655579

set fuseidx [lsearch -exact ${go.vendors} github.com/hanwen/go-fuse/v2]
if {$fuseidx < 0 || [lindex ${go.vendors} [expr {$fuseidx + 1}]] ne "lock"} {
    error "Cannot find go-fuse lock in go.vendors"
}
set gitversionfuse  [lindex ${go.vendors} [expr {$fuseidx + 2}]]
set builddate       [clock format ${source_date_epoch} -gmt 1 -format {%Y-%m-%d}]
set ldflags         "-X \"main.GitVersion=${version}\" -X \"main.GitVersionFuse=${gitversionfuse}\" -X \"main.BuildDate=${builddate}\""

build.env-append    CGO_ENABLED=0
build.args-append   -ldflags="${ldflags}" \
                    -tags without_openssl

post-build {
    set envargs [lmap e ${build.env} {shellescape $e}]
    system -W ${worksrcpath}/${name}-xray "env [join $envargs] ${build.cmd} [join ${build.args}]"
}

destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/
    xinstall -m 0755 ${worksrcpath}/${name}-xray/${name}-xray ${destroot}${prefix}/bin/
    xinstall -m 0644 ${worksrcpath}/Documentation/gocryptfs.1 ${destroot}${prefix}/share/man/man1
    xinstall -m 0644 ${worksrcpath}/Documentation/gocryptfs-xray.1 ${destroot}${prefix}/share/man/man1
}

# According to [1] there is no benefit to build gocryptfs with openssl on M1 Macs.
# According to [2] on most modern CPUs openssl provides no speed benefits. I confirmed this by tests on
# my Intel i7 Macbook Pro 2019. Therefore reducing the dependencies and defaulting to build it without openssl.
# [1]: https://github.com/rfjakob/gocryptfs/issues/556#issuecomment-848185514
# [2]: https://github.com/rfjakob/gocryptfs/wiki/CPU-Benchmarks
variant openssl description {Build with openssl support} {
    PortGroup       openssl 1.0

    build.env-append \
                    PKG_CONFIG_PATH=[openssl::pkgconfig_dir]
    build.env-replace \
                    CGO_ENABLED=0 CGO_ENABLED=1
    build.args-delete \
                    -tags without_openssl
}

variant doc description {Build man pages} {
    depends_build-append port:pandoc

    post-build {
        system -W ${worksrcpath}/Documentation "sh MANPAGE-render.bash"
    }
}

variant fs_link description "Link ${name} to a .fs bundle in /Library/Filesystems" {
    post-destroot {
        set dir /Library/Filesystems/${name}.fs/Contents/Resources
        xinstall -d ${destroot}${dir}
        ln -s ${prefix}/bin/${name} ${destroot}${dir}/mount_${name}
    }

    destroot.violate_mtree \
                        yes

    notes-append "
        With +fs_link, you may use \'mount -t ${name}\' and use ${name} in /etc/fstab.
    "
}

default_variants +doc

# Exclude pre-release versions.
github.livecheck.regex \
                    {([0-9.]+)}
