from waflib import Options, Errors, Utils
from waftools.plugin import plugin

source = """
daap_xform.c
daap_cmd.c
daap_conn.c
daap_util.c
daap_md5.c
cc_handlers.c
""".split()

def plugin_configure(conf):
    # Set the default fallthrough, if no "intelligent" backend is found.
    conf.env.XMMS_DAAP_BACKEND = "dummy"

    try:
        conf.check_cfg(package="avahi-glib", uselib_store="avahiglib",
               args="--cflags --libs")
    except Errors.ConfigurationError:
        have_avahiglib = False
    else:
        have_avahiglib = True

    try:
        conf.check_cfg(package="avahi-client", uselib_store="avahiclient",
                args="--cflags --libs")
    except Errors.ConfigurationError:
        have_avahiclient = False
    else:
        have_avahiclient = True

    conf.check_cfg(package='gio-2.0', atleast_version='2.22.0',
            uselib_store='gio2', args='--cflags --libs')

    # First look for Avahi mdns support
    if have_avahiglib and have_avahiclient:
        conf.env.XMMS_DAAP_BACKEND = "avahi"
    elif conf.check(header_name="dns_sd.h", mandatory=False):
        # We might have dnssd support. If we're not on OSX, check for the
        # presence of the lib.
        if Utils.unversioned_sys_platform() == "darwin":
            conf.env.XMMS_DAAP_BACKEND = "dnssd"
        elif conf.check(lib='dns_sd', uselib_store='dnssd', mandatory=False):
            conf.env.XMMS_DAAP_BACKEND = "dnssd"
    conf.msg('Selected daap backend', conf.env.XMMS_DAAP_BACKEND)

def plugin_build(bld, obj):
    _backend = bld.env.XMMS_DAAP_BACKEND

    obj.uselib.append("socket")
    obj.uselib.append("gio2")
    if _backend == "avahi":
        obj.uselib.extend(["avahiglib", "avahiclient"])
    elif _backend == "dnssd":
        obj.uselib.append("dnssd")

    obj.source.append("daap_mdns_%s.c" % _backend)

configure, build = plugin('daap', configure=plugin_configure,
                          libs=["curl"], build=plugin_build,
                          source=source)
