# -*- 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 release         181
github.setup        macvim-dev macvim ${release} release-
github.tarball_from archive
revision            0
name                MacVim
version             ${vim_version}.release${release}
categories          editors
license             Vim GPL-2+
maintainers         {raimue @raimue}

description         MacVim is a GUI version of vim for macOS
long_description \
    MacVim is a port of the text editor Vim to macOS that is meant to \
    integrate seamlessly with the native user interface.

homepage            https://macvim.org

checksums           rmd160  6f0b0cb3de5781073848e22681c248859129add3 \
                    sha256  ee4127ff18f55f04b69e401fc444c94b9e4d2bf60580ed18e85b78f2e34efbd3 \
                    size    27772511

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

# MacVim does not build for arch i386
# https://trac.macports.org/ticket/54666
supported_archs     arm64 x86_64

patchfiles          patch-remove-updater.diff \
                    patch-remove-Homebrew-python.diff

pre-fetch {
    if {${os.platform} eq "darwin" && ${os.major} < 12} {
        ui_error "${name} only runs on OS X 10.8 or greater."
        return -code error "incompatible Mac OS X version"
    }
}

# does not run correctly when build with older clang releases, see #30985
compiler.blacklist {clang < 400}

post-extract {
    # create cache directory
    file mkdir ${workpath}/caches
}

post-patch {
    # Disable Sparkle auto update
    system -W ${worksrcpath}/src/MacVim/ "/usr/libexec/PlistBuddy -c \"Delete :SUFeedURL\" Info.plist"
    system -W ${worksrcpath}/src/MacVim/ "/usr/libexec/PlistBuddy -c \"Delete :SUPublicEDKey\" Info.plist"
    system -W ${worksrcpath}/src/MacVim/ "/usr/libexec/PlistBuddy -c \"Add :SUEnableAutomaticChecks bool false\" Info.plist"
}

use_xcode yes

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

configure.args      --enable-gui=macvim \
                    --without-x \
                    --without-local-dir \
                    --disable-sparkle \
                    --disable-gpm \
                    --disable-canberra \
                    --disable-libsodium \
                    --with-tlib=ncurses \
                    --enable-multibyte \
                    --enable-fail-if-missing \
                    --with-compiledby="MacPorts"

# Old pre-compiled headers could influence build, #26723
build.args          XCODEFLAGS="CACHE_ROOT=${workpath}/caches"

if {[vercmp ${xcodeversion} 12.0] >= 0} {
    build.args    XCODEFLAGS="-scheme MacVim -configuration Release -derivedDataPath ${workpath}/.home/DerivedData CACHE_ROOT=${workpath}/caches -UseNewBuildSystem=NO MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target} ARCHS=${configure.build_arch}"
} elseif {[vercmp ${xcodeversion} 10.0] >= 0} {
    build.args    XCODEFLAGS="CACHE_ROOT=${workpath}/caches -UseNewBuildSystem=NO MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target}"
}

destroot {
    copy ${worksrcpath}/src/MacVim/build/Release/MacVim.app ${destroot}${applications_dir}
    ln -s ${applications_dir}/MacVim.app/Contents/bin/mvim ${destroot}${prefix}/bin/mvim

    # Create MacVim vimdiff, view, ex equivalents
    ln -s ${applications_dir}/MacVim.app/Contents/bin/mvim ${destroot}${prefix}/bin/mvimdiff
    ln -s ${applications_dir}/MacVim.app/Contents/bin/mvim ${destroot}${prefix}/bin/mview
    # Link as mvimex as bin/mex conflicts with texlive
    ln -s ${applications_dir}/MacVim.app/Contents/bin/mvim ${destroot}${prefix}/bin/mvimex

    # Create a separate dir that can be added to PATH to access non-gui MacVim tools
    xinstall -d ${destroot}${prefix}/libexec/macvim
    foreach bin {vim vimdiff view vi ex rvim rview} {
        ln -s ${applications_dir}/MacVim.app/Contents/bin/mvim ${destroot}${prefix}/libexec/macvim/${bin}
    }
}

test.run            yes

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

configure.args-append --with-features=normal
set levels {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
}
