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

name            emacs23
version         23.4
revision        6

categories      editors
license         GPL-3+
maintainers     {dports @drkp} openmaintainer
description     The GNU Emacs text editor
long_description \
    GNU Emacs is a self-documenting, customizable, extensible real-time \
    display editor. Users new to Emacs will be able to use basic        \
    features fairly rapidly by studying the tutorial and using the      \
    self-documentation features. Emacs also has an extensive            \
    interactive manual browser. It is easily extensible since its       \
    editing commands are written in Lisp.

platforms       darwin freebsd
homepage        https://www.gnu.org/software/emacs/emacs.html
master_sites    gnu:emacs
distname        emacs-${version}
dist_subdir     emacs

checksums       rmd160  5fb7e4cd4270a524b8723ad86a145a0e47336bf7 \
                sha256  b9a2b8434052771f797d2032772eba862ff9aa143029efc72295170607289c18

patchfiles      patch-configure.diff \
                patch-src_xgselect.c.diff \
                patch-lisp_files.el.diff

set major       [lindex [split ${version} .] 0]
set suffix      ".${name}"

configure.args  --without-x \
                --without-dbus \
                --without-gconf \
                --without-libotf \
                --without-m17n-flt \
                --without-gpm \
                --program-suffix=${suffix} \
                --infodir ${prefix}/share/info/${name}

depends_build   port:pkgconfig \
                port:texinfo
depends_lib     port:ncurses

post-destroot {
    xinstall -d ${destroot}${prefix}/share/emacs/${version}/leim
    delete ${destroot}${prefix}/bin/ctags${suffix}
    delete ${destroot}${prefix}/share/man/man1/ctags.1
    # remove non-version-specific site-lisp dir
    delete ${destroot}${prefix}/share/emacs/site-lisp
    delete ${destroot}${prefix}/var/games/emacs/snake-scores
    delete ${destroot}${prefix}/var/games/emacs/tetris-scores
    
    # would be better to rename this and rewrite with appropriate paths
    delete ${destroot}${prefix}/share/applications/emacs.desktop
    delete ${destroot}${prefix}/share/icons
    
    # add suffixes to man pages to match binary names
    foreach f [glob -dir ${destroot}${prefix}/share/man/man1 *.1] {
        move $f "[file rootname $f]${suffix}.1"
    }

    # give the emacs binary (and its manpage) a more sane name
    move ${destroot}${prefix}/bin/emacs${suffix} \
         ${destroot}${prefix}/bin/${name}
    move ${destroot}${prefix}/bin/emacs-${version}${suffix} \
         ${destroot}${prefix}/bin/emacs-${version}
    move ${destroot}${prefix}/share/man/man1/emacs${suffix}.1 \
         ${destroot}${prefix}/share/man/man1/${name}.1

}

livecheck.type  regex
livecheck.url   http://ftp.gnu.org/gnu/emacs/?C=M&O=D
livecheck.regex emacs-(${major}\\.\\d+\\w*)\\.tar

variant x11 description {Builds emacs as a X11 program with Lucid widgets} {
    configure.args-delete   --without-x
    configure.args-append   --with-x-toolkit=lucid \
                            --without-xaw3d \
                            --with-xpm \
                            --with-jpeg \
                            --with-tiff \
                            --with-gif \
                            --with-png \
                            --without-rsvg \
                            --with-xft
    depends_lib-append      port:xorg-libXmu \
                            port:xorg-libXaw \
                            port:xpm \
                            port:jpeg \
                            port:tiff \
                            port:giflib \
                            port:libpng \
                            port:Xft2

    # autoconf appears to be dropping linker flags for freetype &
    # fontconfig; work around this. See #28083
    configure.ldflags-append -lfreetype -lfontconfig
}

platform darwin {
    post-patch {
        # Leopard's XCode 3.1.x ld(1) man page claims -no_pie is supported, but it's not
        if {${os.major} < 9} {
            reinplace "s:-fno-pie::" ${worksrcpath}/src/s/darwin.h
        } elseif {${os.major} > 10} {
            reinplace "s:-fno-pie:-fno-pie -Wl,-no_pie:" ${worksrcpath}/src/s/darwin.h

            # I believe the above reinplace is sufficient, but I'm leaving this to be safe --jeremyhu
            configure.ldflags-append -Wl,-no_pie
        }
    }
}

variant motif requires x11 description {Builds emacs as an X11 program with Motif widgets} {
    configure.args-delete   --with-x-toolkit=lucid
    configure.args-append   --with-x-toolkit=motif
    depends_lib-append      lib:libXm:openmotif
}

variant gtk requires x11 description {Builds emacs as an X11 program with GTK+2 widgets} {
    configure.args-delete   --with-x-toolkit=lucid
    configure.args-delete   --without-gconf
    configure.args-delete   --without-rsvg
    configure.args-append   --with-x-toolkit=gtk
    configure.args-append   --with-gconf
    configure.args-append   --with-rsvg
    depends_lib-append      port:gtk2 \
                            path:lib/pkgconfig/glib-2.0.pc:glib2 \
                            port:gconf \
                            port:librsvg
}

variant dbus description {Builds emacs with D-Bus support} {
    configure.args-delete   --without-dbus
    configure.args-append   --with-dbus
    depends_lib-append      port:dbus
}