# -*- 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           active_variants 1.1
PortGroup           github 1.0
PortGroup           gobject_introspection 1.0

github.setup        lathiat avahi 0.7 v
revision            1
categories          net devel
maintainers         nomaintainer
license             LGPL-2.1+
platforms           darwin
description         Avahi is an Implementation of the DNS Service Discovery and Multicast DNS \
                    specifications for Zeroconf Networking.

long_description    ${description} It uses D-Bus for communication between user applications and a \
                    system daemon. The daemon is used to coordinate application efforts in caching \
                    replies, necessary to minimize the traffic imposed on networks.

homepage            http://avahi.org/
github.tarball_from releases

checksums           rmd160  40a58c780a01ae03a234cd52735ba8d63c93b867 \
                    sha256  57a99b5dfe7fdae794e3d1ee7a62973a368e91e414bd0dfa5d84434de5b14804

patchfiles          patch-avahi-daemon-Makefile.in.diff \
                    patch-avahi-dnsconfd-Makefile.in.diff \
                    patch-avahi-utils-Makefile.in.diff \
                    patch-configure.diff \
                    patch-initscript-darwin-org.freedesktop.avahi-daemon.plist.in.diff \
                    patch-initscript-darwin-org.freedesktop.avahi-dnsconfd.plist.in.diff

# ensure MacPorts machine-id from dbus is found
# see https://trac.macports.org/ticket/53894 for O_CLOEXEC
patchfiles-append   patch-avahi_daemon.diff

post-patch {
    # this preprocessor macro causes a lot of things to be undefined inside netinet/in.h system header.
    reinplace "s/-D_XOPEN_SOURCE=500//" ${worksrcpath}/configure

    # use correct location of dbus from patch-avahi_daemon.diff
    reinplace "s|__MACPORTS_PREFIX__|${prefix}|g" ${worksrcpath}/avahi-daemon/main.c
}

depends_build       port:pkgconfig \
                    port:intltool

depends_lib         port:expat \
                    port:gdbm \
                    port:gettext \
                    port:libdaemon

gobject_introspection yes

if {![variant_isset underscore]} {
    set avahi_user  avahi
    set avahi_group avahi
} else {
    set avahi_user  _avahi
    set avahi_group _avahi
}
add_users ${avahi_user} group=${avahi_group} realname=Avahi\ Service

# "Priviliged [sic] access group for Avahi clients"
# defaults to netdev
# macOS has no equivalent group
#     see https://wiki.ubuntu.com/Security/Privileges#Connect_to_wireless_and_ethernet_networks
#     see https://wiki.debian.org/SystemGroups
set avahi_priv_group admin

configure.args      --disable-autoipd \
                    --disable-silent-rules \
                    --disable-qt3 \
                    --disable-qt4 \
                    --disable-mono \
                    --disable-monodoc \
                    --with-distro=darwin \
                    --enable-compat-libdns_sd \
                    --disable-gtk3 \
                    --disable-xmltoman \
                    --disable-gtk \
                    --disable-pygtk \
                    --disable-tests \
                    --with-avahi-priv-access-group=${avahi_priv_group} \
                    --with-avahi-user=${avahi_user} \
                    --with-avahi-group=${avahi_group}

# __APPLE_USE_RFC_2292 should be removed once avhi is updated to support RFC 3542
configure.cppflags-append   -L${prefix}/lib -D__APPLE_USE_RFC_2292

set launch_daemons {org.freedesktop.avahi-daemon.plist org.freedesktop.avahi-dnsconfd.plist}

post-destroot {
    xinstall -d -m 0755 ${destroot}${prefix}/Library/LaunchDaemons
    foreach fl ${launch_daemons} {
        file attributes ${destroot}/Library/LaunchDaemons/${fl} -permissions 0644
        move ${destroot}/Library/LaunchDaemons/${fl} ${destroot}${prefix}/Library/LaunchDaemons
    }

    # previous command should move all LaunchDaemons
    foreach fl [glob -nocomplain -tails -directory ${destroot}/Library/LaunchDaemons *] {
        ui_error "${destroot}/Library/LaunchDaemons should be empty"
        ui_error "${fl} should not exist in ${destroot}/Library/LaunchDaemons"
        return -code error "unaccounted for file in LaunchDaemons"
    }

    if {${startupitem.install}} {
        foreach fl ${launch_daemons} {
            ln -s ${prefix}/Library/LaunchDaemons/${fl} ${destroot}/Library/LaunchDaemons/${fl}
        }
    }
}

post-activate {
    if {${startupitem.install}} {
        foreach fl ${launch_daemons} {
            file attributes ${prefix}/Library/LaunchDaemons/${fl} -owner root -group wheel
        }
    }
}

variant mono description {Enable Mono support} {
    depends_lib-append      port:mono \
                            port:gtk-sharp2

    configure.args-delete   --disable-mono

    post-patch {
        reinplace {s/\.so\.0/.0.dylib/} ${worksrcpath}/avahi-ui-sharp/avahi-ui-sharp.dll.config.in
    }

    post-build {
        # TODO This shouldn't be needed, we need to fix Mono dllmap library path.
        reinplace -E "s|(target=\")|\\1${prefix}/lib/|" \
            ${worksrcpath}/avahi-sharp/avahi-sharp.dll.config \
            ${worksrcpath}/avahi-ui-sharp/avahi-ui-sharp.dll.config
    }
}

variant test description {Build tests} {
    configure.args-delete   --disable-tests
    configure.args-append   --enable-tests
    test.run            yes
    test.target         check
}

variant gtk description {Build with GTK2} {
    depends_lib-append      port:gtk2 \
                            port:libglade2

    configure.args-delete   --disable-gtk
}

variant gtk3 description {Build with GTK3} {
    depends_lib-append      port:gtk3

    configure.args-delete   --disable-gtk3
}

default_variants +gtk +gtk3

variant qt4 description {Build with Qt4} {
    PortGroup   qt4 1.0
    configure.args-delete   --disable-qt4
}

# just use python27 with no python variants

configure.python        ${prefix}/bin/python2.7

depends_lib-append      port:dbus-python27 \
                        port:py27-gdbm

depends_lib-append      port:py27-gobject3
configure.pkg_config_path-append ${prefix}/Library/Frameworks/Python.framework/Versions/2.7/lib/pkgconfig

# see https://trac.macports.org/ticket/55054
pre-configure {
    configure.env-append PKG_CONFIG_PATH=[join ${configure.pkg_config_path} ":"]
    configure.pkg_config_path
}

if [variant_isset gtk] {
    depends_lib-append      port:py27-pygtk
    configure.args-delete   --disable-pygtk
}

variant x11 conflicts quartz {
    require_active_variants gtk2 x11 quartz
}

variant quartz conflicts x11 {
    require_active_variants gtk2 quartz x11
}

if {![variant_isset quartz]} {
    default_variants-append +x11
}

variant underscore description {Put underscore in front of Avahi daemon user} {
    # For darwin ${os.major} > 8, daemon users have an underscore in front of the usernames and groups.
    # This variant allows the user to follow that convention.
}

if {${startupitem.install}} {
    notes "############################################################################
# Startup items have been generated that will aid in
# starting ${name} with launchd. They are disabled
# by default. Execute the following commands to start them,
# and to cause them to launch at startup:
#"
    foreach fl ${launch_daemons} {
        notes-append "# sudo launchctl load -w /Library/LaunchDaemons/${fl}"
    }
    notes-append "############################################################################"
} else {
        notes "############################################################################
# Startup items were not installed for ${name}
# Some programs which depend on ${name} might not function properly.
# To load ${name} manually, run
#"
    foreach fl ${launch_daemons} {
        notes-append "# sudo launchctl load -w ${prefix}/Library/LaunchDaemons/${fl}"
    }
    notes-append "############################################################################"
}