#!/usr/bin/env python

# For Samba 4.21.x
LDB_VERSION = '2.10.0'

import sys, os

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

def options(opt):

    opt.add_option('--without-ldb-lmdb',
                   help='disable new LMDB backend for LDB',
                   action='store_true', dest='without_ldb_lmdb', default=False)

def configure(conf):
    # where does the default LIBDIR end up? in conf.env somewhere?
    #
    conf.env.ldb_modules_install_dir = conf.SUBST_ENV_VAR('LDBMODULESDIR')
    conf.CONFIG_PATH('LDB_MODULESDIR', conf.env.ldb_modules_install_dir)

    if not conf.CHECK_CODE('return !(sizeof(size_t) >= 8)',
                           "HAVE_64_BIT_SIZE_T_FOR_LMDB",
                           execute=True,
                           msg='Checking for a 64-bit host to '
                           'support lmdb'):
        Logs.warn("--without-ldb-lmdb implied as this "
                  "host is not 64-bit")

        if not Options.options.without_ad_dc and \
           conf.CONFIG_GET('ENABLE_SELFTEST'):
            Logs.warn("NOTE: Some AD DC parts of selftest will fail")

        conf.env.REQUIRE_LMDB = False
    elif Options.options.without_ldb_lmdb:
        if not Options.options.without_ad_dc and \
           conf.CONFIG_GET('ENABLE_SELFTEST'):
            raise Errors.WafError('--without-ldb-lmdb conflicts '
                                 'with --enable-selftest while '
                                 'building the AD DC')

        conf.env.REQUIRE_LMDB = False
    else:
        conf.env.REQUIRE_LMDB = True

    # if lmdb support is enabled then we require lmdb
    # is present, build the mdb back end and enable lmdb support in
    # the tools.
    if conf.env.REQUIRE_LMDB:
        if not conf.CHECK_CFG(package='lmdb',
                              args='"lmdb >= 0.9.16" --cflags --libs',
                              msg='Checking for lmdb >= 0.9.16',
                              mandatory=False):
            if not conf.CHECK_CODE('''
                    #if MDB_VERSION_MAJOR == 0 \
                      && MDB_VERSION_MINOR <= 9 \
                      && MDB_VERSION_PATCH < 16
                    #error LMDB too old
                    #endif
                    ''',
                    'HAVE_GOOD_LMDB_VERSION',
                    headers='lmdb.h',
                    msg='Checking for lmdb >= 0.9.16 via header check'):

                if not Options.options.without_ad_dc:
                    raise Errors.WafError('Samba AD DC and --enable-selftest '
                                         'requires '
                                         'lmdb 0.9.16 or later')
                else:
                    raise Errors.WafError('ldb build (unless --without-ldb-lmdb) '
                                         'requires '
                                         'lmdb 0.9.16 or later')

        if conf.CHECK_FUNCS_IN('mdb_env_create', 'lmdb', headers='lmdb.h'):
            conf.DEFINE('HAVE_LMDB', '1')
            conf.env.HAVE_LMDB = True

    conf.env.ldb_is_public_library \
        = not samba_bundled.LIB_MUST_BE_PRIVATE(conf, 'ldb')

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

    # we're not currently linking against the ldap libs, but ldb.pc.in
    # has @LDAP_LIBS@
    bld.env.LDAP_LIBS = ''

    LDB_MAP_SRC = bld.SUBDIR('ldb_map',
                             'ldb_map.c ldb_map_inbound.c ldb_map_outbound.c')

    COMMON_SRC = bld.SUBDIR('common',
                            '''ldb_modules.c ldb_ldif.c ldb_parse.c ldb_msg.c ldb_utf8.c
                            ldb_debug.c ldb_dn.c ldb_match.c ldb_options.c ldb_pack.c
                            ldb_attributes.c attrib_handlers.c ldb_controls.c qsort.c''')

    bld.SAMBA_MODULE('ldb_ldap', 'ldb_ldap/ldb_ldap.c',
                     init_function='ldb_ldap_init',
                     module_init_name='ldb_init_module',
                     deps='talloc lber ldap ldb',
                     enabled=bld.env.ENABLE_LDAP_BACKEND,
                     internal_module=False,
                     subsystem='ldb')

    if bld.PYTHON_BUILD_IS_ENABLED():
        name = bld.pyembed_libname('pyldb-util')
        bld.SAMBA_LIBRARY(name,
                          deps='replace ldb',
                          source='pyldb_util.c',
                          private_library=True,
                          pyembed=True,
                          enabled=bld.PYTHON_BUILD_IS_ENABLED())

        bld.SAMBA_PYTHON('pyldb', 'pyldb.c',
                         deps='replace ldb ' + name,
                         realname='ldb.so',
                         cflags='-DPACKAGE_VERSION=\"%s\"' % LDB_VERSION)

        bld.SAMBA_SCRIPT('_ldb_text.py',
                         pattern='_ldb_text.py',
                         installdir='python')

        bld.INSTALL_FILES('${PYTHONARCHDIR}', '_ldb_text.py')

    if bld.is_install:
        modules_dir = bld.EXPAND_VARIABLES('${LDB_MODULESDIR}')
    else:
        # when we run from the source directory, we want to use
        # the current modules, not the installed ones
        modules_dir = os.path.join(os.getcwd(), 'bin/modules/ldb')

    abi_match = '!ldb_*module_ops !ldb_*backend_ops ldb_*'

    ldb_headers = ('include/ldb.h include/ldb_errors.h '
                   'include/ldb_module.h include/ldb_handlers.h')

    bld.SAMBA_LIBRARY('ldb',
                      COMMON_SRC + ' ' + LDB_MAP_SRC,
                      deps='tevent LIBLDB_MAIN replace',
                      includes='include',
                      public_headers=ldb_headers,
                      public_headers_install=True,
                      pc_files='ldb.pc',
                      vnum=LDB_VERSION,
                      private_library=False,
                      manpages='man/ldb.3',
                      abi_directory='ABI',
                      abi_match = abi_match)

    # generate a include/ldb_version.h
    def generate_ldb_version_h(t):
        '''generate a vscript file for our public libraries'''

        tgt = t.outputs[0].bldpath(t.env)

        v = t.env.LDB_VERSION.split('.')

        f = open(tgt, mode='w')
        try:
            f.write('#define LDB_VERSION "%s"\n' % t.env.LDB_VERSION)
            f.write('#define LDB_VERSION_MAJOR %d\n' % int(v[0]))
            f.write('#define LDB_VERSION_MINOR %d\n' % int(v[1]))
            f.write('#define LDB_VERSION_RELEASE %d\n' % int(v[2]))
        finally:
            f.close()
        return
    t = bld.SAMBA_GENERATOR('ldb_version.h',
                            rule=generate_ldb_version_h,
                            dep_vars=['LDB_VERSION'],
                            target='include/ldb_version.h',
                            public_headers='include/ldb_version.h',
                            public_headers_install=bld.env.ldb_is_public_library)
    t.env.LDB_VERSION = LDB_VERSION

    bld.SAMBA_MODULE('ldb_asq',
                     'modules/asq.c',
                     init_function='ldb_asq_init',
                     module_init_name='ldb_init_module',
                     internal_module=False,
                     deps='ldb',
                     subsystem='ldb')

    bld.SAMBA_MODULE('ldb_server_sort',
                     'modules/sort.c',
                     init_function='ldb_server_sort_init',
                     internal_module=False,
                     module_init_name='ldb_init_module',
                     deps='ldb',
                     subsystem='ldb')

    bld.SAMBA_MODULE('ldb_paged_searches',
                     'modules/paged_searches.c',
                     init_function='ldb_paged_searches_init',
                     internal_module=False,
                     module_init_name='ldb_init_module',
                     deps='ldb',
                     subsystem='ldb')

    bld.SAMBA_MODULE('ldb_rdn_name',
                     'modules/rdn_name.c',
                     init_function='ldb_rdn_name_init',
                     internal_module=False,
                     module_init_name='ldb_init_module',
                     deps='ldb',
                     subsystem='ldb')

    bld.SAMBA_MODULE('ldb_sample',
                     'tests/sample_module.c',
                     init_function='ldb_sample_init',
                     internal_module=False,
                     module_init_name='ldb_init_module',
                     deps='ldb',
                     subsystem='ldb')

    bld.SAMBA_MODULE('ldb_skel',
                     'modules/skel.c',
                     init_function='ldb_skel_init',
                     internal_module=False,
                     module_init_name='ldb_init_module',
                     deps='ldb',
                     subsystem='ldb')

    bld.SAMBA_MODULE('ldb_sqlite3',
                     'sqlite3/ldb_sqlite3.c',
                     init_function='ldb_sqlite3_init',
                     internal_module=False,
                     module_init_name='ldb_init_module',
                     enabled=False,
                     deps='ldb',
                     subsystem='ldb')

    bld.SAMBA_MODULE('ldb_tdb',
                     bld.SUBDIR('ldb_tdb',
                                '''ldb_tdb_init.c'''),
                     init_function='ldb_tdb_init',
                     module_init_name='ldb_init_module',
                     internal_module=False,
                     deps='ldb ldb_tdb_int ldb_key_value',
                     subsystem='ldb')

    bld.SAMBA_LIBRARY('ldb_tdb_int',
                      bld.SUBDIR('ldb_tdb',
                                 '''ldb_tdb_wrap.c ldb_tdb.c'''),
                      private_library=True,
                      deps='ldb tdb ldb_key_value ldb_tdb_err_map')

    bld.SAMBA_LIBRARY('ldb_tdb_err_map',
                      bld.SUBDIR('ldb_tdb',
                                 '''ldb_tdb_err_map.c '''),
                      private_library=True,
                      deps='ldb tdb')

    bld.SAMBA_LIBRARY('ldb_key_value',
                      bld.SUBDIR('ldb_key_value',
                                '''ldb_kv.c ldb_kv_search.c ldb_kv_index.c
                                ldb_kv_cache.c'''),
                      private_library=True,
                      deps='tdb ldb ldb_tdb_err_map')

    if bld.CONFIG_SET('HAVE_LMDB'):
        bld.SAMBA_MODULE('ldb_mdb',
                         bld.SUBDIR('ldb_mdb',
                                    '''ldb_mdb_init.c'''),
                         init_function='ldb_mdb_init',
                         module_init_name='ldb_init_module',
                         internal_module=False,
                         deps='ldb ldb_key_value ldb_mdb_int',
                         subsystem='ldb')

        bld.SAMBA_LIBRARY('ldb_mdb_int',
                          bld.SUBDIR('ldb_mdb',
                                     '''ldb_mdb.c '''),
                          private_library=True,
                          deps='ldb lmdb ldb_key_value')
        lmdb_deps = ' ldb_mdb_int'
    else:
        lmdb_deps = ''


    bld.SAMBA_MODULE('ldb_ldb',
                     bld.SUBDIR('ldb_ldb',
                                '''ldb_ldb.c'''),
                     init_function='ldb_ldb_init',
                     module_init_name='ldb_init_module',
                     internal_module=False,
                     deps='ldb ldb_tdb_int ldb_key_value' + lmdb_deps,
                     subsystem='ldb')

    # have a separate subsystem for common/ldb.c, so it can rebuild
    # for install with a different -DLDB_MODULESDIR=
    bld.SAMBA_SUBSYSTEM('LIBLDB_MAIN',
                        'common/ldb.c',
                        deps='tevent tdb',
                        includes='include',
                        cflags=['-DLDB_MODULESDIR=\"%s\"' % modules_dir])

    LDB_TOOLS='ldbadd ldbsearch ldbdel ldbmodify ldbedit ldbrename'
    for t in LDB_TOOLS.split():
        bld.SAMBA_BINARY(t, 'tools/%s.c' % t, deps='ldb-cmdline ldb',
                         manpages='man/%s.1' % t)

    # ldbtest doesn't get installed
    bld.SAMBA_BINARY('ldbtest', 'tools/ldbtest.c', deps='ldb-cmdline ldb',
                     install=False)

    if bld.CONFIG_SET('HAVE_LMDB'):
        lmdb_deps = ' lmdb'
    else:
        lmdb_deps = ''
    # ldbdump doesn't get installed
    bld.SAMBA_BINARY('ldbdump',
                     'tools/ldbdump.c',
                     deps='ldb-cmdline ldb' + lmdb_deps,
                     install=False)

    bld.SAMBA_LIBRARY('ldb-cmdline',
                      source='tools/ldbutil.c tools/cmdline.c',
                      deps='ldb dl popt',
                      private_library=True)

    bld.SAMBA_BINARY('ldb_tdb_mod_op_test',
                     source='tests/ldb_mod_op_test.c',
                     cflags='-DTEST_BE=\"tdb\"',
                     deps='cmocka ldb',
                     install=False)

    bld.SAMBA_BINARY('ldb_tdb_guid_mod_op_test',
                     source='tests/ldb_mod_op_test.c',
                     cflags='-DTEST_BE=\"tdb\" -DGUID_IDX=1',
                     deps='cmocka ldb',
                     install=False)

    bld.SAMBA_BINARY('ldb_tdb_kv_ops_test',
                     source='tests/ldb_kv_ops_test.c',
                     cflags='-DTEST_BE=\"tdb\"',
                     deps='cmocka ldb',
                     install=False)

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

    bld.SAMBA_BINARY('ldb_msg_test',
                     source='tests/ldb_msg.c',
                     deps='cmocka ldb',
                     install=False)

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

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

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

    bld.SAMBA_BINARY('test_ldb_comparison_fold',
                     source='tests/test_ldb_comparison_fold.c',
                     deps='cmocka ldb ldbwrap',
                     install=False)

    bld.SAMBA_BINARY('ldb_key_value_test',
                     source='tests/ldb_key_value_test.c',
                     deps='cmocka ldb ldb_tdb_err_map',
                     install=False)

    bld.SAMBA_BINARY('ldb_parse_test',
                     source='tests/ldb_parse_test.c',
                     deps='cmocka ldb ldb_tdb_err_map',
                     install=False)

    bld.SAMBA_BINARY('ldb_filter_attrs_test',
                     source='tests/ldb_filter_attrs_test.c',
                     deps='cmocka ldb ldb_tdb_err_map',
                     install=False)

    bld.SAMBA_BINARY('ldb_filter_attrs_in_place_test',
                     source='tests/ldb_filter_attrs_in_place_test.c',
                     deps='cmocka ldb ldb_tdb_err_map',
                     install=False)

    bld.SAMBA_BINARY('ldb_key_value_sub_txn_tdb_test',
                     bld.SUBDIR('ldb_key_value',
                         '''ldb_kv_search.c
                            ldb_kv_index.c
                            ldb_kv_cache.c''') +
                     'tests/ldb_key_value_sub_txn_test.c',
                     cflags='-DTEST_BE=\"tdb\"',
                     deps='cmocka ldb ldb_tdb_err_map',
                     install=False)

    # If both libldap and liblber are available, test ldb_ldap
    # code for a regression of bz#14413 -- even if we don't build
    # it ourselves and simply using the system version
    if bld.env.LIB_LDAP and bld.env.LIB_LBER:
        bld.SAMBA_BINARY('lldb_ldap_test',
                         source='tests/lldb_ldap.c',
                         deps='cmocka talloc lber ldap ldb',
                         install=False)

    if bld.CONFIG_SET('HAVE_LMDB'):
        bld.SAMBA_BINARY('ldb_mdb_mod_op_test',
                         source='tests/ldb_mod_op_test.c',
                         cflags='-DTEST_BE=\"mdb\" -DGUID_IDX=1 '
                              + '-DTEST_LMDB=1',
                         deps='cmocka ldb lmdb',
                         install=False)

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

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

        bld.SAMBA_BINARY('ldb_mdb_kv_ops_test',
                         source='tests/ldb_kv_ops_test.c',
                         cflags='-DTEST_BE=\"mdb\" -DTEST_LMDB=1',
                         deps='cmocka ldb',
                         install=False)

        bld.SAMBA_BINARY('ldb_lmdb_free_list_test',
                         source='tests/ldb_lmdb_free_list_test.c',
                         cflags='-DTEST_BE=\"mdb\" -DTEST_LMDB=1',
                         deps='cmocka ldb',
                         install=False)
        #
        # We rely on the versions of the ldb_key_value functions included
        # in ldb_key_value_sub_txn_test.c taking priority over the versions
        # in the ldb_key_value shared library.
        # If this turns out to not be the case, the dependencies will
        # need to be unrolled, and all the source files included and the
        # ldb_tdb module initialization code will need to be called
        # manually.
        bld.SAMBA_BINARY('ldb_key_value_sub_txn_mdb_test',
                         bld.SUBDIR('ldb_key_value',
                             '''ldb_kv_search.c
                                ldb_kv_index.c
                                ldb_kv_cache.c''') +
                         'tests/ldb_key_value_sub_txn_test.c',
                         cflags='-DTEST_BE=\"mdb\"',
                         deps='cmocka ldb ldb_tdb_err_map',
                         install=False)
    else:
        bld.SAMBA_BINARY('ldb_no_lmdb_test',
                         source='tests/ldb_no_lmdb_test.c',
                         deps='cmocka ldb',
                         install=False)

