# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 52400 2009-06-15 23:17:26Z nox@macports.org $

PortSystem      1.0
PortGroup       muniversal 1.0

name            dbus
version         1.2.14
maintainers     mcalhoun openmaintainer
categories      devel
platforms       darwin
description     A message bus system, a simple way for applications to talk to one another.

long_description \
    ${description}

homepage        http://www.freedesktop.org/Software/dbus
master_sites    http://dbus.freedesktop.org/releases/dbus

checksums           md5     2c267ccd45d0b18db8c9edacad63ec98 \
                    sha1    67909bdd146d3a993131d54e70a143992edbe927 \
                    rmd160  abd90d99ba476a060e65008cf8635d427b6a1503

# There is a proposal to use launchd with dbus (see https://bugs.freedesktop.org/show_bug.cgi?id=14259).
# patch-launchd-integration.diff is a slightly modified patch from Fink.
patchfiles      patch-launchd-integration.diff

use_autoreconf  yes

depends_build       \
    port:pkgconfig  \
    port:autoconf   \
    port:automake   \
    port:libtool

set dbus_user     messagebus
set dbus_group    messagebus
set startup_root  ""

depends_lib     port:expat

configure.args  --disable-doxygen-docs \
                --disable-xml-docs \
                --without-x \
                --enable-launchd \
                --with-launchd-agent-dir=${prefix}/Library/LaunchAgents

pre-configure {
    # Value must be set in pre-configure because dbus_user may change in variant.
    configure.args-append  --with-dbus-user=${dbus_user}
}

post-patch {
    reinplace "s|/usr/local|${prefix}|g" ${worksrcpath}/dbus/dbus-sysdeps-unix.c
    reinplace {s|broken_poll="no (cross compiling)"|broken_poll=yes|} ${worksrcpath}/configure
    
    if { ! [variant_isset no_startupitem] } {
        # Disable if installed into startup directory.
        reinplace "s|<false />|<false />\\\n\\\n\\\t<key>Disabled</key>\\\n\\\t<true/>|" \
            ${worksrcpath}/bus/org.freedesktop.dbus-session.plist.in
    }
    
    if { ${os.major} >= 9 } {
        # Allow OnDemand for Leopard and above.
        reinplace {s|<false />|<true />|} \
            ${worksrcpath}/bus/org.freedesktop.dbus-session.plist.in
    }
}

platform darwin 7 {
    depends_build-delete    port:autoconf port:automake port:libtool
    depends_lib-append      port:xorg-libsm port:xorg-libX11

    patchfiles-delete      patch-launchd-integration.diff

    use_autoreconf          no

    configure.args-delete   --without-x \
                            --enable-launchd \
                            --with-launchd-agent-dir=${prefix}/Library/LaunchAgents
    configure.args-append   --with-x

    post-patch {
        # Neither _SC_GETPW_R_SIZE_MAX nor _SC_GETGR_R_SIZE_MAX are defined in Panther.
        # The value of -1 is used to emulate a failed sysconf call.
        reinplace "s|sysconf (_SC_GETPW_R_SIZE_MAX)|-1|" \
            ${worksrcpath}/dbus/dbus-sysdeps-unix.c
        reinplace "s|sysconf (_SC_GETGR_R_SIZE_MAX)|-1|" \
            ${worksrcpath}/dbus/dbus-sysdeps-util-unix.c
        
        # -Wno-pointer-sign does not work on Panther.
        reinplace {/CFLAGS="$CFLAGS -Wno-pointer-sign"/d} ${worksrcpath}/configure
    }

    if { ! [variant_isset  no_startupitem] } {
        startupitem.create       yes
    }
    startupitem.name         dbus
    startupitem.executable   ${prefix}/bin/dbus-daemon --system --nofork
}

use_parallel_build  yes

test.run        yes
test.target     check

pre-test {
    if {![variant_isset test]} {
        ui_error "test variant must be activated to enable test support."
        error "Please enable test variant."
    }
}

destroot.keepdirs \
    ${destroot}${prefix}/share/dbus-1/services \
    ${destroot}${prefix}/var/run/dbus \
    ${destroot}${prefix}/etc/dbus-1/system.d \
    ${destroot}${prefix}/etc/dbus-1/session.d

set plistDir  ${prefix}/Library/LaunchDaemons
set plistFl   ${plistDir}/org.freedesktop.dbus-system.plist

# universal_archs_to_use might not be set before pre-fetch.
pre-destroot {
    global merger_dont_diff merger_configure_env

    # PortGroup muniversal has difficulty merging three files.
    if {[info exists universal_archs_to_use] && [llength ${universal_archs_to_use}] == 3} {
        set merger_dont_diff "${prefix}/lib/dbus-1.0/include/dbus/dbus-arch-deps.h"
    }
}

post-destroot {
    if {![variant_isset darwin_7]} {
        # Simplify startup script over startupitem.create.
        # See http://trac.macports.org/ticket/15081
        xinstall -d -m 0755 ${destroot}${plistDir}
        set plist [open "${destroot}${plistFl}" w 0644]
        
        puts ${plist} "<?xml version='1.0' encoding='UTF-8'?>"
        puts ${plist} "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\""
        puts ${plist} "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\" >"
        puts ${plist} "<plist version='1.0'>"
        puts ${plist} "<dict>"
        
        puts ${plist} "<key>Label</key><string>org.freedesktop.dbus-system</string>"
        
        puts ${plist} "<key>ProgramArguments</key>"
        puts ${plist} "<array>"
        puts ${plist} "\t<string>${prefix}/bin/dbus-daemon</string>"
        puts ${plist} "\t<string>--system</string>"
        puts ${plist} "\t<string>--nofork</string>"
        puts ${plist} "</array>"
        
        puts ${plist} "<key>OnDemand</key><false/>"
        
        if { ! [variant_isset no_startupitem] } {
            puts ${plist} "<key>Disabled</key><true/>"
        }
        
        puts ${plist} "</dict>"
        puts ${plist} "</plist>"
        
        close ${plist}
        
        if { ! [variant_isset no_startupitem] } {
            xinstall -d -m 0755 ${destroot}${startup_root}/Library/LaunchDaemons
            xinstall -d -m 0755 ${destroot}${startup_root}/Library/LaunchAgents
            ln -s ${plistFl} ${destroot}${startup_root}/Library/LaunchDaemons
            ln -s ${prefix}/Library/LaunchAgents/org.freedesktop.dbus-session.plist ${destroot}${startup_root}/Library/LaunchAgents
        }
    }
    
    system "env DYLD_LIBRARY_PATH=${destroot}${prefix}/lib ${destroot}${prefix}/bin/dbus-uuidgen --ensure=${destroot}${prefix}/var/lib/dbus/machine-id"
}

pre-activate {
    addgroup  ${dbus_group}
    adduser   ${dbus_user} gid=[existsgroup ${dbus_group}] realname=Message\ Bus
    
    if { [file exists ${prefix}/var/lib/dbus/machine-id] } {
        # See http://trac.macports.org/ticket/19234
        delete ${prefix}/var/lib/dbus/machine-id
    }
}

post-activate {
    file attributes ${prefix}/var/run/dbus -group ${dbus_group} -owner ${dbus_user}
    file attributes ${prefix}/libexec/dbus-daemon-launch-helper -group ${dbus_group}
    
    if {![variant_isset darwin_7] && ![variant_isset no_startupitem] } {        
        if { ! [variant_isset no_root] } {
            set sudo "sudo "
            file attributes /Library/LaunchAgents/org.freedesktop.dbus-session.plist -owner root -group wheel
            file attributes /Library/LaunchDaemons/[file tail ${plistFl}] -owner root -group wheel
        } else {
            set sudo ""
        }
        
        ui_msg "#################################################################################################"
        ui_msg "# Startup items have been generated that will aid in"
        ui_msg "# starting ${name} with launchd. They are disabled"
        ui_msg "# by default. Execute the following command to start them,"
        ui_msg "# and to cause it to launch at startup:"
        ui_msg "#"
        ui_msg "# ${sudo}launchctl load -w ${startup_root}/Library/LaunchDaemons/[file tail ${plistFl}]"
        ui_msg "# ${sudo}launchctl load -w ${startup_root}/Library/LaunchAgents/org.freedesktop.dbus-session.plist"
        ui_msg "##################################################################################################"
    }
}

variant test description {enable tests} {
    configure.args-append   --enable-tests
}

variant no_startupitem description {Do not install startup files.} {}

variant no_root description {Run the DBUS daemon as MacPorts install user.} {
    pre-fetch {
        if { ${install.user}=="root" || ${install.group}=="wheel" } {
            ui_error "The DBUS daemon should not be run as root."
            erorr "Please do not use this variant with your MacPorts configuration."
        }
    }

    set dbus_user     ${install.user}
    set dbus_group    ${install.group}
    set startup_root  [join [lrange [exec /usr/bin/dscl . -read Users/${install.user} NFSHomeDirectory] 1 end]]
    
    # Files are installed into user's startup directory.
    if { ![variant_isset no_staruptitem] } {
        destroot.violate_mtree  yes
    }
}

set cross_opts  "ac_cv_have_abstract_sockets=no NM='/usr/bin/nm -p'"
if { ${os.arch}=="i386" } {
    if { ${os.major} >= 10 } {
        set merger_configure_env(ppc) ${cross_opts}
    } else {
        set merger_configure_env(ppc) "NM='/usr/bin/nm -p'"
    }
    set merger_configure_env(ppc64)   ${cross_opts}
} else {
    set merger_configure_env(i386)    ${cross_opts}
    set merger_configure_env(x86_64)  ${cross_opts}
}

livecheck.check regex
livecheck.regex {D-Bus (\d+(?:\.\d+)*)}
