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

epoch                   5
github.setup            textmate textmate 2.0.23 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
revision                4

maintainers             {cal @neverpanic} openmaintainer
name                    textmate2

categories              editors aqua
description             \
    TextMate brings Apple's approach to operating systems into the world of text editors. By \
    bridging UNIX underpinnings and GUI, TextMate cherry-picks the best of both worlds to the \
    benefit of expert scripters and novice users alike.
long_description        {*}${description}\
    TextMate is well-known for its large number of bundles adding support, snippets, completions \
    and commands for a huge number of file types.

platforms               {darwin >= 18}
license                 GPL-3+
use_xcode               yes

fetch.type              git
post-fetch {
    system -W ${worksrcpath} "${git.cmd} submodule update --init"
}

depends_build-append    path:lib/libssl.dylib:openssl \
                        path:share/curl/curl-ca-bundle.crt:curl-ca-bundle \
                        port:boost \
                        port:multimarkdown \
                        port:ninja \
                        port:proctools \
                        port:ragel \
                        port:sparsehash

depends_skip_archcheck  ninja ragel proctools

depends_lib-append      port:capnproto \
                        port:curl \
                        port:libiconv \
                        port:sqlite3 \
                        port:zlib

# Needs support for -std=c++2a.
compiler.blacklist      {clang < 920}

# The build system forces the use of libc++. This is ok because the port
# doesn't provide any C++ libraries nor use any C++ libraries from deps.
configure.cxx_stdlib    libc++

# Patch needed during build for download of bundles; will revert later and
# rebuild
patchfiles              patch-Frameworks__io__src__path.cc-use-home-from-env.diff \
                        patch-Applications__TextMate__src__RMateServer.mm-non-objc-ptr-cast-with-arc.diff \
                        0001-Avoid-clang-15-crash-by-using-correct-array-size.patch

pre-configure {
    copy ${filespath}/local.rave ${worksrcpath}
    reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/local.rave

    # the textmate build expects this to be present
    file mkdir $env(HOME)/Library/Caches/com.macromates.TextMate/Bundles
}

build.cmd               ninja
build.args              -v -j${build.jobs}
build.target            TextMate

post-build {
    # Revert the patch, retrigger the build
    system -W ${worksrcpath} "patch -R -p0 <${filespath}/patch-Frameworks__io__src__path.cc-use-home-from-env.diff"
    command_exec build
}

destroot {
    copy [glob ${workpath}/.home/build/textmate-*/release/Applications/TextMate/TextMate.app] ${destroot}${applications_dir}
    ln -s ${applications_dir}/TextMate.app/Contents/MacOS/mate ${destroot}${prefix}/bin/mate
}
