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

name                xinit
gitlab.instance     https://gitlab.freedesktop.org/xorg
gitlab.setup        app ${name} 1.4.4 ${name}-
revision            0
categories          x11
maintainers         {jeremyhu @jeremyhu} openmaintainer
license             MIT
description         X11 startup script processor
long_description    ${name} is responsible for configuring the initial environment \
                    when X11 starts up.

homepage            https://www.x.org

checksums           rmd160  0d6cf24da13e226f7cdc48a5c52df49f18a85af4 \
                    sha256  a7a2616ca38b7b9a1e3850d001ef881884434e3db1ef8784f58ff81310706c99 \
                    size    23716

use_bzip2           yes

depends_build-append \
                    path:bin/pkg-config:pkgconfig \
                    port:tradcpp \
                    port:xorg-util-macros

depends_lib         port:xorg-libX11

depends_run         port:mkfontscale \
                    bin:xauth:xauth \
                    bin:xrdb:xrdb \
                    bin:xset:xset

patch.pre_args-replace  -p0 -p1

# https://gitlab.freedesktop.org/xorg/app/xinit/-/issues/24
patchfiles          0001-Revert-darwin-Drop-support-for-macOS-10.7-and-earlie.patch

if {[vercmp ${os.version} 10.3.0] < 0} {
    patchfiles-append disable-launchagent.patch
}

# TODO: should this be used on 10.6.8, when xorg-server is installed as +old_abi?
# libdispatch is unsupported on 10a190 and earlier, despite possibly present headers.
# On macOS <= 10a190 run the test on non-powerpc systems only.
# patchfiles-append   patch-no-libdispatch-ppc.diff

use_autoreconf      yes
autoreconf.args     -fvi

configure.args      --disable-silent-rules \
                    --with-launchd-id-prefix=org.macports

configure.args-append   RAWCPP=${prefix}/bin/tradcpp

post-destroot {
    xinstall -d ${destroot}${prefix}/etc/X11/xinit/xinitrc.d
    xinstall -m 755 {*}[glob ${filespath}/xinitrc.d/*.sh] ${destroot}${prefix}/etc/X11/xinit/xinitrc.d
    xinstall -m 755 ${filespath}/font_cache.sh ${destroot}${prefix}/bin/font_cache

    reinplace "s|__PREFIX__|${prefix}|g" \
        {*}[glob ${destroot}${prefix}/etc/X11/xinit/xinitrc.d/*.sh] \
        ${destroot}${prefix}/bin/font_cache
}

platform darwin {
    if {${os.major} > 9} {
        # OS X 10.6.3 was the first version whose native X11 allowed the modern socket naming
        # for better X11 server coexistence, so we require it for installing the LaunchAgent
        if {[vercmp ${os.version} 10.3.0] < 0} {
            notes {
                To use MacPorts' X11 as the default server, install\
                xorg-server, load the LaunchAgent with the following\
                command, log out, and log back in.

                    $ launchctl load -w /Library/LaunchAgents/org.macports.startx.plist

                This will be remembered across port updates on Snow\
                Leopard and later. Note that doing this may cause\
                the Apple-provided X11 to fail to launch.}
        } else {
            notes {
                To use MacPorts' X11 as the default server, install\
                xorg-server, log out, and log back in.}
        }
    }

    post-activate {
        system "launchctl load /Library/LaunchDaemons/org.macports.privileged_startx.plist"
    }

    pre-deactivate {
        system "launchctl unload /Library/LaunchDaemons/org.macports.privileged_startx.plist"
    }
}

install.asroot      yes
