# -*- 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.1
set vim_patchlevel  0727
github.setup        vim vim ${vim_version}.${vim_patchlevel} v
revision            0
categories          editors
platforms           darwin freebsd
license             Vim GPL-2+
maintainers         {raimue @raimue}

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  862555466b868c9d99943f29d9384c440c67711e \
                    sha256  dfc41f36c2415acc00d1faef5e50d053be3c5a50b0c569d8ac4c73820be7c731 \
                    size    18094755

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

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-gui \
                    --without-x \
                    --without-local-dir \
                    --disable-gpm \
                    --disable-canberra \
                    --disable-libsodium \
                    --mandir=${prefix}/share/man \
                    --with-tlib=ncurses \
                    --enable-multibyte \
                    --with-developer-dir=${configure.developer_dir} \
                    --enable-fail-if-missing \
                    --with-compiledby="MacPorts"

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 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 gtk2 gtk3 motif {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=athena --disable-darwin
    depends_lib-append      port:xorg-libXaw
}
variant gtk2 description {Build GUI version using GTK 2.x widgets} requires x11 conflicts athena gtk3 motif {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=gtk2 --disable-darwin
    depends_lib-append      path:lib/pkgconfig/gtk+-2.0.pc:gtk2
    depends_build-append    port:pkgconfig
}
variant gtk3 description {Build GUI version using GTK 3.x widgets} requires x11 conflicts athena gtk2 motif {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=gtk3 --disable-darwin
    depends_lib-append      path:lib/pkgconfig/gtk+-3.0.pc:gtk3
    depends_build-append    port:pkgconfig
}
variant motif description {Build GUI version with Motif widgets} requires x11 conflicts athena gtk2 gtk3 {
    configure.args-delete   --disable-gui
    configure.args-append   --enable-gui=motif --disable-darwin
    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 ruby20 ruby21 ruby22 ruby23 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 ruby19 conflicts ruby18 ruby20 ruby21 ruby22 ruby23 ruby24 ruby25 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby1.9
    depends_lib-append      port:ruby19
}
variant ruby20 conflicts ruby18 ruby19 ruby21 ruby22 ruby23 ruby24 ruby25 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby2.0
    depends_lib-append      port:ruby20
}
variant ruby21 conflicts ruby18 ruby19 ruby20 ruby22 ruby23 ruby24 ruby25 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby2.1
    depends_lib-append      port:ruby21
}
variant ruby22 conflicts ruby18 ruby19 ruby20 ruby21 ruby23 ruby24 ruby25 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby2.2
    depends_lib-append      port:ruby22
}
variant ruby23 conflicts ruby18 ruby19 ruby20 ruby21 ruby22 ruby24 ruby25 ruby30 ruby31 ruby32 ruby33 description {Enable Ruby scripting} {
    configure.args-append   --enable-rubyinterp
    configure.args-append   --with-ruby-command=${prefix}/bin/ruby2.3
    depends_lib-append      port:ruby23
}
variant ruby24 conflicts ruby18 ruby19 ruby20 ruby21 ruby22 ruby23 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 ruby20 ruby21 ruby22 ruby23 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 ruby20 ruby21 ruby22 ruby23 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 ruby20 ruby21 ruby22 ruby23 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 ruby20 ruby21 ruby22 ruby23 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 ruby20 ruby21 ruby22 ruby23 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
}

# Disable Cocoa on PureDarwin, as well as on < 10.6
# or when building for powerpc, since it fails to build there:
# 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 "puredarwin" || ${configure.build_arch} eq "ppc" \
    || (${os.platform} eq "darwin" && ${os.major} < 10)} {
    configure.args-append   --disable-darwin
}
