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

import os
import Options
from logging import warning

source = """
    config.c
    mediainfo.c
    sqlite.c
    medialib.c
    object.c
    error.c
    output.c
    playlist.c
    collection.c
    collquery.c
    collserial.c
    collsync.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
    bindata.c
    sample.genpy
    utils.c
    visualization/format.c
    visualization/object.c
    visualization/udp.c
    visualization/xform.c
""".split()

def build(bld):
    env = bld.env

    prog = bld.new_task_gen('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/localtime_%s.c" % env["localtime_impl"])
    source.append("compat/statfs_%s.c" % env["statfs_impl"])
    if bld.env['HAVE_SEMCTL']:
        source.append("visualization/unixshm.c")
    else:
        source.append("visualization/dummy.c")

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

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

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

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

    bld.new_task_gen(features='man', files='xmms2d.1', section='1')

def configure(conf):
    conf.check_tool('python-generator', tooldir=os.path.abspath('waftools'))
    conf.check_cfg(package='gmodule-2.0', atleast_version='2.6.0', uselib_store='gmodule2', args='--cflags --libs')
    conf.check_cfg(package='gthread-2.0', atleast_version='2.6.0', uselib_store='gthread2', args='--cflags --libs')
    conf.check_cfg(package='sqlite3', atleast_version='3.5', uselib_store='sqlite3', args='--cflags --libs', mandatory=1)

    # Check for the sin function in the math lib
    conf.check_cc(lib='m', function_name='sin', header_name=['math.h'], uselib_store="math", mandatory=1)

    # Check if localtime_r exists
    if conf.check_cc(function_name='localtime_r', header_name=['time.h']):
        conf.env['localtime_impl'] = 'unix'
    else:
        conf.env['localtime_impl'] = 'dummy'

    # Detect the type of stat call used
    if Options.platform == 'win32':
        conf.env['statfs_impl'] = 'dummy'
    elif conf.check(header_name='sys/vfs.h'):
        if Options.platform == 'sunos':
            conf.env['statfs_impl'] = 'solaris'
        else:
            conf.env['statfs_impl'] = 'linux'
    elif conf.check(header_name='sys/param.h'):
        statfs_fragment = "#include <sys/param.h> \n#include <sys/mount.h> \nint main() { struct statfs foo; return 0; }\n"
        if not conf.check_cc(fragment=statfs_fragment, uselib_store="statfs", msg="Checking for struct statfs"):
            conf.env['statfs_impl'] = 'netbsd'
        else:
            conf.env['statfs_impl'] = 'bsd'
    else:
        conf.env['statfs_impl'] = 'dummy'

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

    if Options.platform == 'win32':
        conf.env['xmms_shared_library'] = True
    else:
        conf.env['xmms_shared_library'] = False

    if Options.platform == 'win32':
        conf.env['compat_impl'] = 'dummy'
    else:
        conf.env['compat_impl'] = 'unix'

    if not Options.options.without_unixshmserver:
        conf.check_cc(function_name='semctl', header_name=['sys/types.h','sys/ipc.h','sys/sem.h'])
        semun_fragment = "#include <time.h>\n#include <sys/sem.h>\n int main() { union semun foo; return 0; }\n"
        if not conf.check_cc(fragment=semun_fragment, uselib_store="semun", msg="Checking for union semun"):
            conf.env["CCDEFINES_shm"] += ["_SEM_SEMUN_UNDEFINED"]

    if not conf.env['HAVE_SEMCTL']:
        warning("Compiling visualization without shm support")

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

    conf.check_cfg(package='valgrind', uselib_store='valgrind', args='--cflags')

    return True

def set_options(opt):
    opt.add_option('--disable-shmvis-server', action='store_true',
                   dest='without_unixshmserver', default=False,
                   help="Disable shared memory support for visualization")
