from waftools.plugin import plugin
from Params import g_platform

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

    # First look for Avahi mdns support
    if (conf.check_pkg("avahi-glib", destvar='avahiglib') and
        conf.check_pkg("avahi-client", destvar='avahiclient')):
        # Avahi found
        conf.env['XMMS_DAAP_BACKEND'] = 'avahi'
    elif conf.check_header('dns_sd.h'):
        # We might have dnssd support. If we're not on OSX, check for the
        # presence of the lib.
        if g_platform == 'darwin':
            conf.env['XMMS_DAAP_BACKEND'] = 'dnssd'
        elif conf.check_library2('dns_sd', uselib='dnssd', mandatory=0):
            conf.env['XMMS_DAAP_BACKEND'] = 'dnssd'
    return True

def plugin_build(bld, obj):
    daap_backend = bld.env_of_name('default')['XMMS_DAAP_BACKEND']
    obj.source.append("daap_mdns_%s.c" % daap_backend)
    if daap_backend == 'avahi':
        obj.uselib += ' avahiglib avahiclient'
    elif daap_backend == 'dnssd':
        obj.uselib += ' dnssd'
    if g_platform == 'win32':
        obj.uselib += ' socket'

configure, build = plugin('daap', configure=plugin_configure,
                          extra_libs=['curl'], build=plugin_build,
                          source="""daap_xform.c daap_cmd.c daap_conn.c
                                    daap_util.c daap_md5.c cc_handlers.c
                                 """.split())
