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

set emacs_version   29.4
set emacs_mac_ver   10.1

bitbucket.setup     mituharu emacs-mac emacs-${emacs_version}-mac-${emacs_mac_ver}
name                emacs-mac-app
conflicts           emacs-app emacs-app-devel
version             ${emacs_mac_ver}
revision            1
categories          aqua editors
maintainers         {amake @amake} openmaintainer

description         Emacs Mac port

long_description    ${name} is the \"Mac port\" of GNU Emacs ${emacs_version}. \
                    This provides a native GUI with tight OS integration.

platforms           {darwin >= 14}
license             GPL-3+

checksums           rmd160  8eeb76746ccf693e8314dd1eaedcf57caf7c2d89 \
                    sha256  c96775606b8a629eaeb551ad99ff486fc42cd42b0c09071f3fef4712c116d837 \
                    size    50004997

depends_lib         port:ncurses \
                    port:libxml2 \
                    path:lib/pkgconfig/gnutls.pc:gnutls \
                    port:lcms2 \
                    port:gmp \
                    port:jansson

patchfiles          patch-src_emacs.c.diff

if {${subport} eq ${name}} {
    conflicts-prepend   emacs-mac-app-devel
}

post-patch {
    reinplace "s|__PREFIX__|${prefix}|" ${worksrcpath}/src/emacs.c
}

universal_variant   no

use_autoreconf      yes
autoreconf.cmd      ./autogen.sh
autoreconf.args     all

depends_build       port:autoconf \
                    port:automake \
                    port:libtool \
                    path:bin/pkg-config:pkgconfig \
                    port:texinfo \
                    port:sqlite3 \
                    port:webp

set rpaths [list]

# The Mac port uses CoreText instead of HarfBuzz
configure.args      --disable-silent-rules \
                    --with-mac \
                    --enable-mac-app=${worksrcpath} \
                    --enable-mac-self-contained \
                    --without-dbus \
                    --without-gconf \
                    --without-libotf \
                    --without-m17n-flt \
                    --without-harfbuzz \
                    --without-imagemagick \
                    --without-rsvg \
                    --without-xaw3d \
                    --without-tree-sitter \
                    --with-libgmp \
                    --with-gnutls \
                    --with-xml2 \
                    --with-json \
                    --with-lcms2 \
                    --with-modules \
                    --with-sqlite3 \
                    --with-webp

platform darwin {
    configure.cflags-append -fobjc-arc
}

pre-configure {
    if {[llength $rpaths] > 0} {
        configure.ldflags-append "-Wl,-rpath " [join $rpaths " -Wl,-rpath "]
    }
}

subport ${name}-devel {
    set emacs_version   29.4
    set date            2025-09-20

    version         [string map {- {}} ${date}]
    revision        0

    long_description \
        ${name} is the \"Mac port\" of GNU Emacs ${emacs_version}. \
        This provides a native GUI with tight OS integration.

    fetch.type      git
    git.url         --shallow-since=${date}T00:00:00 --branch work https://bitbucket.org/mituharu/emacs-mac.git
    git.branch      238d7188c32a8d8308be577cb477edef7125a68b

    # --shallow-since needs a newer version of git than on some older systems,
    # so use MacPorts version
    depends_fetch-append port:git
    git.cmd         ${prefix}/bin/git

    conflicts-prepend   emacs-mac-app

    livecheck.version   ${git.branch}
    bitbucket.livecheck work

    if {[variant_isset nativecomp]} {
        notes "emacs devel subports don't always keep compatibility for native\
 compiled files. Better to cleanup your ~/.emacs.d/.local/cache/eln"
    }
}

destroot.destdir {}

post-destroot {
    set appname [expr {[variant_isset renameapp] ? "EmacsMac" : "Emacs"}]

    move ${worksrcpath}/Emacs.app ${destroot}${applications_dir}/${appname}.app

    # Install site-start.el
    set site_lisp ${destroot}${applications_dir}/${appname}.app/Contents/Resources/site-lisp
    xinstall -d ${site_lisp}
    file copy ${filespath}/site-start.el ${site_lisp}
    reinplace "s|__PREFIX__|${prefix}|g" ${site_lisp}/site-start.el

    # Install emacs-module.h header file
    xinstall -m 0755 -d ${destroot}${prefix}/include/emacs-mac
    xinstall -m 0644 ${worksrcpath}/src/emacs-module.h \
        ${destroot}${prefix}/include/emacs-mac
}

variant multitty description {Enable Multi-TTY support} {
    patchfiles-append   ${subport}-multi-tty.patch
}

variant rsvg description {Enable librsvg bindings} {
    depends_lib-append     path:lib/pkgconfig/librsvg-2.0.pc:librsvg
    configure.args-delete  --without-rsvg
    configure.args-append  --with-rsvg
}

variant imagemagick description {Enable ImageMagick bindings} {
    depends_lib-append     port:ImageMagick
    configure.args-delete  --without-imagemagick
    configure.args-append  --with-imagemagick
}

variant nativecomp description {Builds emacs with native compilation support} {
    set gcc_v                      15
    depends_lib-append             port:gcc${gcc_v}
    configure.args-append          --with-native-compilation=aot
    compiler.cpath-prepend         ${prefix}/include/gcc${gcc_v}
    compiler.library_path-prepend  ${prefix}/lib/gcc${gcc_v}
    lappend rpaths                 ${prefix}/lib/gcc${gcc_v}
}

variant treesitter description {Builds emacs with tree-sitter support} {
    configure.args-delete   --without-tree-sitter
    configure.args-append   --with-tree-sitter
    depends_lib-append  port:tree-sitter

    lappend rpaths ${prefix}/lib

    depends_run-append \
        port:tree-sitter-typescript \
        port:tree-sitter-javascript \
        port:tree-sitter-tsx \
        port:tree-sitter-c \
        port:tree-sitter-cpp \
        port:tree-sitter-java \
        port:tree-sitter-python \
        port:tree-sitter-css \
        port:tree-sitter-json \
        port:tree-sitter-c-sharp \
        port:tree-sitter-bash \
        port:tree-sitter-dockerfile \
        port:tree-sitter-cmake \
        port:tree-sitter-toml \
        port:tree-sitter-go \
        port:tree-sitter-go-mod \
        port:tree-sitter-yaml \
        port:tree-sitter-rust \
        port:tree-sitter-ruby

    notes-append "
To install tree-sitter grammar libraries not required by built-in *-ts-modes,\
please use M-x treesit-install-language-grammar. For details, please refer to\
etc/NEWS or the Emacs Lisp reference manual.
"
}

variant metal description {Enable experimental Metal support} {
    configure.args-append  --with-mac-metal
}

variant renameapp description {Rename app bundle to avoid conflict with emacs-app} {
    conflicts-delete    emacs-app emacs-app-devel
}

default_variants-append +nativecomp +treesitter +renameapp

if {${subport} eq ${name}} {
    livecheck.regex     {"emacs-[0-9.]+-mac-([0-9.]+)"}
    livecheck.version   ${version}
}
