# encoding: utf-8
#
# WAF build scripts for XMMS2
# Copyright (C) 2006-2008 XMMS2 Team
#

import os
import Common
import Params

source = """config.c
            mediainfo.c
            sqlite.c
            medialib.c
            object.c
            error.c
            output.c
            playlist.c
            collection.c
            collquery.c
            collserial.c
            ipc.c
            log.c
            plugin.c
            magic.c
            ringbuf.c
            xform.c
            streamtype.c
            converter_plugin.c
	    segment_plugin.c
            ringbuf_xform.c
            outputplugin.c
            strfunc.c
            bindata.c
            sample.genpy
            utils.c""".split()

def build(bld):
    env = bld.env()

    prog = bld.create_obj('cc', 'program')
    prog.target = 'xmms2d'
    prog.includes = '. ../include ../includepriv'
    prog.source = ["main.c"]
    prog.source.append("compat/signal_%s.c" % env['compat_impl'])
    prog.source.append("compat/symlink_%s.c" % env['compat_impl'])
    prog.source.append("compat/checkroot_%s.c" % env['compat_impl'])

    source.append("compat/statfs_%s.c" % env["statfs_impl"])

    if env['xmms_shared_library']:
        lib = bld.create_obj('cc', 'shlib')
        lib.target = 'xmms2core'
        lib.source = source
        lib.includes = '. ../include ../includepriv'
        lib.uselib = 'glib2 gmodule2 gthread2 sqlite3 statfs socket'
        lib.uselib_local = 'xmmsipc xmmssocket xmmsutils xmmstypes'

        prog.uselib_local = 'xmms2core'
    else:
        prog.source += source

    prog.uselib_local += ' xmmsipc xmmssocket xmmsutils xmmstypes'
    prog.uselib = 'glib2 gmodule2 gthread2 sqlite3 statfs socket'

    if env['xmms_icon']:
        prog.add_objects = 'xmms_icon'

    obj = bld.create_obj('man')
    obj.files = ['xmms2d.1']

def configure(conf):
    conf.check_tool('python-generator', tooldir=os.path.abspath('waftools'))
    conf.check_pkg2('gmodule-2.0', '2.6.0', uselib='gmodule2')
    conf.check_pkg2('gthread-2.0', '2.6.0', uselib='gthread2')
    conf.check_pkg2('sqlite3', '0.0', uselib='sqlite3')

    # Check for the sin function in the math lib
    test = conf.create(enumerator='function')
    test.function = 'sin'
    test.headers = ['math.h']
    test.libs = ['m']
    test.mandatory = 1
    test.run()

    # Detect the type of stat call used

    # This function is ploped down here for now because the change has
    # been submitted to upstream, and will hopefully get rolled back
    # into the waf dist.
    def check_header(header_name, mandatory=0):
        test = conf.create(enumerator='header')
        test.name = header_name
        test.mandatory = mandatory
        return test.run()

    if Params.g_platform == 'win32':
        conf.env['statfs_impl'] = 'dummy'
    elif check_header('sys/vfs.h'):
        if Params.g_platform == 'sunos5':
            conf.env['statfs_impl'] = 'solaris'
        else:
            conf.env['statfs_impl'] = 'linux'
    elif check_header('sys/param.h'):
        conf.env['statfs_impl'] = 'bsd'
    else:
        conf.env['statfs_impl'] = 'dummy'

    # Add Darwin stuff
    if Params.g_platform == 'darwin':
        conf.env['LINKFLAGS'] += ['-framework', 'CoreFoundation']
        conf.env['CCDEFINES'] += ['USE_BUNDLES']

    if Params.g_platform == 'win32':
        conf.env['xmms_shared_library'] = True
    else:
        conf.env['xmms_shared_library'] = False

    if Params.g_platform == 'win32':
        conf.env['compat_impl'] = 'dummy'
    else:
        conf.env['compat_impl'] = 'unix'

    conf.env.append_value('XMMS_PKGCONF_FILES', ('xmms2-plugin', ''))

    return True

def set_options(opt):
    pass
