# -*- 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
PortGroup       legacysupport 1.1

# Availability.h
legacysupport.newest_darwin_requires_legacy 8

github.setup    tmux tmux 3.5a
if {${subport} eq ${name}} {
    revision        1
    conflicts       tmux-devel
}
subport tmux-devel {
    github.setup    tmux tmux 7c30056d96689cc8a66c748e7a18e180665b7d14
    version         20240927-[string range ${github.version} 0 6]
    revision        1
    conflicts       tmux
}
categories      sysutils
maintainers     {evermeet.cx:tessarek @tessus} openmaintainer
description     terminal multiplexer
long_description \
    tmux is a \"terminal multiplexer\", it enables a number of terminals \
    (or windows) to be accessed and controlled from a single terminal. \
    tmux is intended to be a simple, modern, BSD-licensed alternative \
    to programs such as GNU screen.
homepage        https://tmux.github.io
license         BSD

depends_build   path:bin/pkg-config:pkgconfig
depends_lib     port:libutf8proc \
                port:libevent \
                port:ncurses
configure.args-append \
                --enable-utf8proc \
                --sysconfdir=${prefix}/etc

if {${subport} eq ${name}} {
    github.tarball_from     releases
    checksums               rmd160  5eb06d152f3fcdeff725e922f2647df5be86ffac \
                            sha256  16216bd0877170dfcc64157085ba9013610b12b082548c7c9542cc0103198951 \
                            size    715619
}
subport tmux-devel {
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from     tarball
    checksums               rmd160  8f12630da75e816dcec96566b50606a39a8a760d \
                            sha256  4b7ddcf541a9a2ada78791d26882704f436b0b94bec538271fc33e42f5abd755 \
                            size    1047030

    use_autoreconf          yes
    autoreconf.cmd          ./autogen.sh
    autoreconf.args
    depends_build-append    port:autoconf \
                            port:automake \
                            port:libtool
    post-extract {
        set reinplace_cmd [subst -nobackslashes -nocommands {s/AC_INIT\(\[tmux\], (next-[0-9]+\.[0-9]+)\)/AC_INIT([tmux], \1 (${version}))/g}]
        reinplace -E $reinplace_cmd ${worksrcpath}/configure.ac
    }

    # https://github.com/tmux/tmux/issues/4119
    patchfiles-append       patch-devel-gcc4.diff
}

platform darwin {
    if {${os.major} < 23} {
        # Before Sonoma, macOS lacked terminal descriptions for tmux,
        # tmux-256color. Fallback on a terminal definition that is available on
        # these older versions
        configure.args-append   --with-TERM=screen-256color
    }
}

platform darwin 8 {
    # On Mac OS X 10.4 there's no libproc.h, so we use a previous version of osdep-darwin.c (c1b9948)
    post-extract {
        file copy -force ${filespath}/osdep-darwin.8.c ${worksrcpath}/osdep-darwin.c
    }
    # Normally `pkg-config ncurses --cflags` sets _XOPEN_SOURCE=600, which has a
    # side effect of setting _POSIX_C_SOURCE and hiding dozens of definitions
    # that are needed
    configure.env-append    LIBNCURSES_CFLAGS=-D_APPLE_C_SOURCE

    # "No rule to make target `compat/forkpty-darwin.c'"
    depends_build-append    port:gmake
    build.cmd               gmake
}

post-destroot {
    xinstall -m 0755 -d ${destroot}${prefix}/share/doc/tmux
    xinstall -m 0644 ${worksrcpath}/example_tmux.conf ${destroot}${prefix}/share/doc/tmux
    xinstall -m 0755 -d ${destroot}${prefix}/share/vim/vimfiles/ftdetect
    xinstall -m 0644 ${filespath}/ftdetect-tmux.vim ${destroot}${prefix}/share/vim/vimfiles/ftdetect/tmux.vim
}

variant sixel description {Support SIXEL graphics} {
    configure.args-append   --enable-sixel
}

default_variants-append +sixel

notes {
    On legacy systems, consider installing port tmux-pasteboard to\
    integrate the system pasteboard with tmux.
}

github.livecheck.regex {([0-9.]+[a-z]?)}
