#!/usr/bin/env python

APPNAME = 'tevent'
VERSION = '0.11.0'

import sys, os

# find the buildtools directory
top = '.'
while not os.path.exists(top+'/buildtools') and len(top.split('/')) < 5:
    top = top + '/..'
sys.path.insert(0, top + '/buildtools/wafsamba')

out = 'bin'

import wafsamba
from wafsamba import samba_dist, samba_utils
from waflib import Options, Logs, Context, Errors

samba_dist.DIST_DIRS('''lib/tevent:. lib/replace:lib/replace
                        lib/talloc:lib/talloc buildtools:buildtools
                        third_party/cmocka:third_party/cmocka
                        third_party/waf:third_party/waf''')

def options(opt):
    opt.BUILTIN_DEFAULT('replace')
    opt.PRIVATE_EXTENSION_DEFAULT('tevent', noextension='tevent')
    opt.RECURSE('lib/replace')
    opt.RECURSE('lib/talloc')


def configure(conf):
    conf.RECURSE('lib/replace')
    conf.RECURSE('lib/talloc')

    if conf.CHECK_FOR_THIRD_PARTY():
        conf.RECURSE('third_party/cmocka')
    else:
        if not conf.CHECK_CMOCKA():
            raise Errors.WafError('cmocka development package have not been found.\nIf third_party is installed, check that it is in the proper place.')
        else:
            conf.define('USING_SYSTEM_CMOCKA', 1)

    conf.env.standalone_tevent = conf.IN_LAUNCH_DIR()

    if not conf.env.standalone_tevent:
        if conf.CHECK_BUNDLED_SYSTEM_PKG('tevent', minversion=VERSION,
                                     onlyif='talloc', implied_deps='replace talloc'):
            conf.define('USING_SYSTEM_TEVENT', 1)
            if not conf.env.disable_python and \
                conf.CHECK_BUNDLED_SYSTEM_PYTHON('pytevent', 'tevent', minversion=VERSION):
                conf.define('USING_SYSTEM_PYTEVENT', 1)

    if conf.CHECK_FUNCS('epoll_create', headers='sys/epoll.h'):
        conf.DEFINE('HAVE_EPOLL', 1)

    tevent_num_signals = 64
    v = conf.CHECK_VALUEOF('NSIG', headers='signal.h')
    if v is not None:
        tevent_num_signals = max(tevent_num_signals, v)
    v = conf.CHECK_VALUEOF('_NSIG', headers='signal.h')
    if v is not None:
        tevent_num_signals = max(tevent_num_signals, v)
    v = conf.CHECK_VALUEOF('SIGRTMAX', headers='signal.h')
    if v is not None:
        tevent_num_signals = max(tevent_num_signals, v)
    v = conf.CHECK_VALUEOF('SIGRTMIN', headers='signal.h')
    if v is not None:
        tevent_num_signals = max(tevent_num_signals, v*2)

    if not conf.CONFIG_SET('USING_SYSTEM_TEVENT'):
        conf.DEFINE('TEVENT_NUM_SIGNALS', tevent_num_signals)

    conf.SAMBA_CHECK_PYTHON()
    conf.SAMBA_CHECK_PYTHON_HEADERS()

    conf.SAMBA_CONFIG_H()

    conf.SAMBA_CHECK_UNDEFINED_SYMBOL_FLAGS()

def build(bld):
    bld.RECURSE('lib/replace')
    bld.RECURSE('lib/talloc')

    if bld.CHECK_FOR_THIRD_PARTY():
        bld.RECURSE('third_party/cmocka')

    SRC = '''tevent.c tevent_debug.c tevent_fd.c tevent_immediate.c
             tevent_queue.c tevent_req.c tevent_wrapper.c
             tevent_poll.c tevent_threads.c
             tevent_signal.c tevent_standard.c tevent_timed.c tevent_util.c tevent_wakeup.c'''

    if bld.CONFIG_SET('HAVE_EPOLL'):
        SRC += ' tevent_epoll.c'

    if bld.CONFIG_SET('HAVE_SOLARIS_PORTS'):
        SRC += ' tevent_port.c'

    if bld.env.standalone_tevent:
        bld.env.PKGCONFIGDIR = '${LIBDIR}/pkgconfig'
        private_library = False
    else:
        private_library = True

    if not bld.CONFIG_SET('USING_SYSTEM_TEVENT'):
        tevent_deps = 'replace talloc'
        if bld.CONFIG_SET('HAVE_PTHREAD'):
            tevent_deps += ' pthread'

        bld.SAMBA_LIBRARY('tevent',
                          SRC,
                          deps=tevent_deps,
                          enabled= not bld.CONFIG_SET('USING_SYSTEM_TEVENT'),
                          includes='.',
                          abi_directory='ABI',
                          abi_match='tevent_* _tevent_*',
                          vnum=VERSION,
                          public_headers=('' if private_library else 'tevent.h'),
                          public_headers_install=not private_library,
                          pc_files='tevent.pc',
                          private_library=private_library)

    if not bld.CONFIG_SET('USING_SYSTEM_PYTEVENT') and not bld.env.disable_python:
        bld.SAMBA_PYTHON('_tevent',
                         'pytevent.c',
                         deps='tevent',
                         realname='_tevent.so',
                         cflags='-DPACKAGE_VERSION=\"%s\"' % VERSION)


        bld.INSTALL_WILDCARD('${PYTHONARCHDIR}', 'tevent.py', flat=False)

        # install out various python scripts for use by make test
        bld.SAMBA_SCRIPT('tevent_python',
                         pattern='tevent.py',
                         installdir='python')

    bld.SAMBA_BINARY('test_tevent_tag',
                     source='tests/test_tevent_tag.c',
                     deps='cmocka tevent',
                     install=False)

    bld.SAMBA_BINARY('test_tevent_trace',
                     source='tests/test_tevent_trace.c',
                     deps='cmocka tevent',
                     install=False)

def test(ctx):
    '''test tevent'''
    print("The tevent testsuite is part of smbtorture in samba4")

    samba_utils.ADD_LD_LIBRARY_PATH('bin/shared')
    samba_utils.ADD_LD_LIBRARY_PATH('bin/shared/private')

    pyret = samba_utils.RUN_PYTHON_TESTS(['bindings.py'])

    unit_test_ret = 0
    unit_tests = [
        'test_tevent_tag',
        'test_tevent_trace',
    ]

    for unit_test in unit_tests:
        unit_test_cmd = os.path.join(Context.g_module.out, unit_test)
        unit_test_ret = unit_test_ret or samba_utils.RUN_COMMAND(unit_test_cmd)

    sys.exit(pyret or unit_test_ret)

def dist():
    '''makes a tarball for distribution'''
    samba_dist.dist()

def reconfigure(ctx):
    '''reconfigure if config scripts have changed'''
    samba_utils.reconfigure(ctx)
