# -*- mode: python; -*-

Import("env")
Import("get_option")

env = env.Clone()

if not env['MONGO_HAVE_LIBMONGOC']:
    Return()

env.AutoInstall(
    '$PREFIX_DOCDIR/mongoc_embedded',
    source=[
        '#/LICENSE-Community.txt',
        '../LICENSE-Embedded.txt',
    ],
    AIB_COMPONENT='embedded',
    AIB_ROLE='base',
)

def create_mongoc_env(env):
    mongocEnv = env.Clone()
    mongocEnv.AppendUnique(LIBS=['bson-1.0', 'mongoc-1.0'])
    return mongocEnv

mongocEmbeddedEnv = create_mongoc_env(env)

mongocEmbeddedEnv.AppendUnique(
    CPPDEFINES=[
        'MONGOC_EMBEDDED_COMPILING',
     ],
)

if get_option('link-model') == 'static':
    mongocEmbeddedEnv.AppendUnique(
        CPPDEFINES=[
            'MONGOC_EMBEDDED_STATIC',
        ],
    )

# Please see the note in ../mongo_embedded/SConscript about how to
# interpret and adjust the current and compatibility versinos.
mongocEmbeddedEnv.AppendUnique(
    SHLINKFLAGS=[
        '$MONGO_EXPORT_FILE_SHLINKFLAGS',
    ],
)

if mongocEmbeddedEnv.TargetOSIs('darwin'):
    # Please see the note in ../mongo_embedded/SConscript about how to
    # interpret and adjust the current and compatibility versinos.
    mongocEmbeddedEnv.AppendUnique(
        SHLINKFLAGS=[
            '-Wl,-current_version,1',
            '-Wl,-compatibility_version,1',
        ],
    )

mongocEmbeddedTargets = mongocEmbeddedEnv.Library(
    target='mongoc_embedded',
    source=[
        'mongoc_embedded.cpp',
    ],
    LIBDEPS=[
        # No LIBDEPS or LIBDEPS_PRIVATE to mongo libraries are allowed in this library. They would get duplicated in mongo_embedded_capi.
        '$BUILD_DIR/mongo/embedded/mongo_embedded/mongo_embedded',
    ],
    AIB_COMPONENT='embedded',
)

env.AutoInstall(
    '$PREFIX_INCLUDEDIR/mongoc_embedded/v1/mongoc_embedded',
    source=['mongoc_embedded.h'],
    AIB_COMPONENT='embedded',
    AIB_ROLE='dev'
)

yamlEnv = env.Clone()
yamlEnv.InjectThirdParty(libraries=['yaml'])

if get_option('link-model') != 'dynamic-sdk':
    mongocEmbeddedTestEnv = create_mongoc_env(yamlEnv)
    clientTest = mongocEmbeddedTestEnv.CppUnitTest(
        target='mongoc_embedded_test',
        source=[
            'mongoc_embedded_test.cpp',
            'mongoc_embedded_test.idl',
        ],
        LIBDEPS=[
            '$BUILD_DIR/mongo/base',
            '$BUILD_DIR/mongo/db/server_options_core',
            '$BUILD_DIR/mongo/unittest/unittest',
            '$BUILD_DIR/mongo/util/options_parser/options_parser',
            'mongoc_embedded',
        ],
        UNITTEST_HAS_CUSTOM_MAINLINE=True,
        AIB_COMPONENT='embedded-test',
    )
