# -*- 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 108023 2013-07-10 20:34:19Z cal@macports.org $

PortSystem              1.0
PortGroup               github 1.0
PortGroup               xcodeversion 1.0
PortGroup               conflicts_build 1.0

github.setup            textmate textmate 9445 a
epoch                   1
version                 2.0.${github.version}

maintainers             cal 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.

conflicts_build         libc-headers
platforms               darwin
supported_archs         x86_64
license                 GPL-3+

minimum_xcodeversions   {11 4.4}

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

patchfiles              patch-configure-versionnumber.diff \
                        patch-create_default_bundles_tbz-verbose.diff

# The build will fail with deployment target set to 10.8
macosx_deployment_target 10.7

post-patch {
    reinplace "s/@REVISION@/${github.version}/" ${worksrcpath}/configure
    fs-traverse file ${worksrcpath} {
        if {[file tail ${file}] == "target"} {
            reinplace "/TESTS *=/d" ${file}
            reinplace "/TEST_SOURCES *=/d" ${file}
        }
    }
}

depends_build-append    port:ninja \
                        port:ragel \
                        port:proctools

depends_skip_archcheck  ninja ragel proctools

depends_lib-append      port:boost \
                        port:multimarkdown \
                        port:curl \
                        port:openssl \
                        port:libiconv \
                        port:zlib

if {${os.platform} != "darwin" || ${os.major} < 11} {
    depends_fetch
    depends_build
    depends_lib
    pre-fetch {
        ui_error "${name} only works with Mac OS X 10.7 Lion or later."
        return -code error "incompatible Mac OS X version"
    }
}

configure.env-append    builddir=${worksrcpath}/build \
                        tag=-macports

pre-configure {
    # the textmate build expects this to be present
    file mkdir $env(HOME)/Library/Caches/com.macromates.TextMate
    # patch the code to use $HOME from env rather than from getpwent(3)
    system -W ${worksrcpath} "patch -p0 <${filespath}/patch-Frameworks__io__src__path.cc-use-home-from-env.diff"
}
post-configure {
    # revert the patch
    system -W ${worksrcpath} "patch -R -p0 <${filespath}/patch-Frameworks__io__src__path.cc-use-home-from-env.diff"
}

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

destroot {
    copy ${worksrcpath}/build/Applications/TextMate/TextMate.app ${destroot}${applications_dir}
    xinstall -m 0755 ${worksrcpath}/build/Applications/mate/mate ${destroot}${prefix}/bin
}
