# -*- 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 68276 2010-05-30 22:05:09Z adfernandes@macports.org $

PortSystem      1.0
PortGroup       muniversal 1.0

name            dbus
version         1.2.24
revision        1
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     565346cecd9cfecf1463540c6086cc2c \
                sha1    9593717348ea36ba228b46f165bb67c127c12be8 \
                rmd160  2f51def784056bc16fbec2ddc3be22c022a94fb5

# There is a proposal to use launchd with dbus (see https://bugs.freedesktop.org/show_bug.cgi?id=14259).
patchfiles      0001-make-session-bus-listen-tag-configurable.patch \
                0002-add-launchd-implementation.patch \
                0003-look-up-DISPLAY-from-launchd-if-not-initialized.patch \
                0004-enable-launchd.patch \
                0005-dont-dup-closed-stdin.patch \
                0006-bus_system-conf-in.patch
patch.pre_args  -p1

depends_build       \
    port:pkgconfig

set dbus_user     messagebus
set dbus_group    messagebus
set startup_root  ""

depends_lib     port:expat

use_autoreconf  yes

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.
        reinplace {s|<false />|<true />|} \
            ${worksrcpath}/bus/org.freedesktop.dbus-session.plist.in
    }
}

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 {
    # 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 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 "# 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."
            error "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.type  regex
livecheck.url   [lindex ${master_sites} 0]
livecheck.regex {dbus-(\d+\.\d*[02468](\.\d+)*)\.tar}
