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

# The vim port should always follow the patch level used in the MacVim port.
# This is because they both share the same set of configuration files
# and ensures that the user's configuration files will always work with both.
set vim_version     9.2
set vim_patchlevel  0136
github.setup        vim vim ${vim_version}.${vim_patchlevel} v
revision            0
categories          editors
platforms           darwin freebsd
license             Vim GPL-2+
maintainers         {raimue @raimue} {@barracuda156 macos-powerpc.org:barracuda}

description         Vi \"workalike\" with many additional features
long_description    Vim is an advanced text editor that seeks to provide \
                    the power of the de-facto Unix editor 'Vi', with a more \
                    complete feature set.

homepage            https://www.vim.org

checksums           rmd160  3f4274d0c02e7443f178085cffce2c3b2f884ca7 \
                    sha256  23c6f6269361eefd279c76e6c13d743ed98b78f81b3f26ec11c8d853d2d3b6d0 \
                    size    19789304
github.tarball_from archive

depends_build-append \
                    port:gettext

depends_lib-append  port:gettext-runtime \
                    port:libiconv \
                    port:ncurses

# https://trac.macports.org/ticket/71656
patchfiles-append   patch-32bit.diff

post-patch {
    set features [open ${worksrcpath}/src/feature.h a+]
    puts $features "#define SYS_VIMRC_FILE \"${prefix}/etc/vimrc\""
    close $features
}

autoconf.cmd make   autoconf
autoconf.pre_args
autoconf.args
autoconf.dir        ${worksrcpath}/src

configure.args-append \
                    --disable-canberra \
                    --disable-darwin \
                    --disable-gpm \
                    --disable-gui \
                    --disable-libsodium \
                    --enable-fail-if-missing \
                    --enable-multibyte \
                    --mandir=${prefix}/share/man \
                    --with-compiledby="MacPorts" \
                    --with-developer-dir=${configure.developer_dir} \
                    --with-tlib=ncurses \
                    --without-local-dir \
                    --without-x

post-destroot {
    ln -s ${prefix}/bin/vim ${destroot}${prefix}/bin/vi
}

test.run            yes

if {![variant_isset tiny] && ![variant_isset small] && ![variant_isset big] && ![variant_isset huge]} {
    default_variants +huge
}

variant cocoa description {Enable Cocoa} {
    configure.args-delete   --disable-darwin
}

variant x11 description {Build CLI version with X support} {
    configure.args-delete   --without-x
    configure.args-append   --with-x --x-includes=${prefix}/include --x-libraries=${prefix}/lib
    depends_lib-append      port:xorg-libXt
}

variant athena description {Build GUI version using Athena widgets} requires x11 conflicts cocoa gtk2 gtk3 motif {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=athena
    depends_lib-append      port:xorg-libXaw
}

variant gtk2 description {Build GUI version using GTK 2.x widgets} requires x11 conflicts cocoa athena gtk3 motif {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=gtk2
    depends_lib-append      path:lib/pkgconfig/gtk+-2.0.pc:gtk2
    depends_build-append    path:bin/pkg-config:pkgconfig
}

variant gtk3 description {Build GUI version using GTK 3.x widgets} requires x11 conflicts cocoa athena gtk2 motif {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=gtk3
    depends_lib-append      path:lib/pkgconfig/gtk+-3.0.pc:gtk3
    depends_build-append    path:bin/pkg-config:pkgconfig
    depends_run-append      port:desktop-file-utils
    post-activate {
        system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
    }
}

variant motif description {Build GUI version with Motif widgets} requires x11 conflicts cocoa athena gtk2 gtk3 {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=motif
    depends_lib-append      lib:libXm:openmotif
}

configure.args-append --with-features=normal
set levels {tiny small big huge}
foreach level ${levels} {
    variant ${level} description "Build ${level} feature set" conflicts {*}[lsearch -inline -all -not -exact $levels $level] "
        configure.args-replace --with-features=normal --with-features=$level
    "
}

variant xim description {Build with support for X Input Method} {
    configure.args-append   --enable-xim
}

variant perl description {Enable Perl scripting} {
    configure.args-append   --enable-perlinterp
    depends_lib-append      path:bin/perl:perl5
}

set pythons_suffixes {27 39 310 311 312 313}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set major [string index ${s} 0]
    set v ${major}.[string range ${s} 1 end]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Enable Python scripting" conflicts {*}${c} "
        if {${major} == 2} {
            configure.args-append   --enable-pythoninterp --with-python-command=${prefix}/bin/python${v}
            patchfiles-append       patch-python.diff
        } else {
            configure.args-append   --enable-python3interp --with-python3-command=${prefix}/bin/python${v}
        }
        depends_lib-append      port:${p}
        use_autoconf yes
        # overwriting autoconf.cmd above removes dependency, add it again
        depends_build-append    port:autoconf
    "
}

variant ruby requires ruby18 description {Compatibility variant, requires +ruby18} {}
variant ruby18 conflicts ruby19 ruby24 ruby25 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby1.8
    depends_lib-append      port:ruby
}
variant ruby24 conflicts ruby18 ruby19 ruby25 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby2.4
    depends_lib-append      port:ruby24
}
variant ruby25 conflicts ruby18 ruby19 ruby24 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby2.5
    depends_lib-append      port:ruby25
}
variant ruby30 conflicts ruby18 ruby19 ruby24 ruby25 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby3.0
    depends_lib-append      port:ruby30
}
variant ruby31 conflicts ruby18 ruby19 ruby24 ruby25 ruby30 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby3.1
    depends_lib-append      port:ruby31
    # Ruby itself uses a modern gcc or clang, so building this with old Xcode gcc fails:
    # /opt/local/include/ruby-3.1.4/ruby/defines.h:55:23: error: stdalign.h: No such file or directory
    compiler.blacklist-append \
                            *gcc-4.0 *gcc-4.2
}
variant ruby32 conflicts ruby18 ruby19 ruby24 ruby25 ruby30 ruby31 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby3.2
    depends_lib-append      port:ruby32
    compiler.blacklist-append \
                            *gcc-4.0 *gcc-4.2
}
variant ruby33 conflicts ruby18 ruby19 ruby24 ruby25 ruby30 ruby31 ruby32 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby3.3
    depends_lib-append      port:ruby33
    compiler.blacklist-append \
                            *gcc-4.0 *gcc-4.2
}

variant tcl description {Enable Tcl scripting} {
    configure.args-append   --enable-tclinterp \
                            --with-tclsh=${prefix}/bin/tclsh
    patchfiles-append       patch-tcl.diff
    depends_lib-append      port:tcl

    use_autoconf            yes
    # Overwriting autoconf.cmd above removes dependency, add it again
    depends_build-append    port:autoconf
}

variant lua description {Enable Lua scripting} {
    PortGroup               lua 1.0

    configure.args-append   --enable-luainterp \
                            --with-lua-prefix=${lua.dir}
}

variant cscope description {Enable source code browsing with cscope} {
    configure.args-append   --enable-cscope
}

if {![variant_isset athena] && ![variant_isset gtk2] && ![variant_isset gtk3] && ![variant_isset motif]} {
    # Disable Cocoa on PureDarwin, as well as on < 10.6.
    # Notice, this also does not build on 10a190:
    # os_macosx.m:74: error: ‘NSPasteboardTypeString’ undeclared (first use in this function)
    # os_macosx.m:264: error: ‘DISPATCH_TIME_FOREVER’ undeclared (first use in this function)
    if {${os.platform} eq "darwin" && ${os.major} > 9} {
        default_variants-append +cocoa
    }
}
