#!/usr/bin/env python
# encoding: utf-8

APPNAME = 'blib-client'
VERSION = '1.0.0'

def options(ctx):
    # Load C compiler tool
    ctx.load('compiler_c')

    ctx.add_option('--blib-client-disable-shared', action='store_true', help='Disable build of shared library')
    ctx.add_option('--blib-client-enable-objects', action='store_true', help='Enable build of object files')
    ctx.add_option('--blib-client-enable-slash-commands', action='store_true', help='Enable slash commands')
    ctx.add_option('--blib-client-enable-csp-client', action='store_true', help='Enable CSP client')
    ctx.add_option('--blib-client-enable-python-bindings', action='store_true', help='Enable Python bindings')

def configure(ctx):
    # Load C compiler tool
    ctx.load('compiler_c')

    ctx.env.BLIB_CLIENT_DISABLE_SHARED = ctx.options.blib_client_disable_shared
    ctx.env.BLIB_CLIENT_ENABLE_OBJECTS = ctx.options.blib_client_enable_objects
    ctx.env.BLIB_CLIENT_ENABLE_SLASH_COMMANDS = ctx.options.blib_client_enable_slash_commands
    ctx.env.BLIB_CLIENT_ENABLE_CSP_CLIENT = ctx.options.blib_client_enable_csp_client
    ctx.env.BLIB_CLIENT_ENABLE_PYTHON_BINDINGS = ctx.options.blib_client_enable_python_bindings

    if ctx.env.BLIB_CLIENT_ENABLE_PYTHON_BINDINGS:
        ctx.load('python')
        ctx.check_python_headers()

def build(ctx):
    # Load C compiler tool
    ctx.load('compiler_c')

    ctx.env.BLIB_CLIENT_SOURCES = []

    if ctx.env.BLIB_CLIENT_ENABLE_CSP_CLIENT:
        ctx.env.append_unique('BLIB_CLIENT_SOURCES', ['src/blib_client.c'])

    # Object files
    if ctx.env.BLIB_CLIENT_ENABLE_OBJECTS:
        ctx.objects(
            target   = APPNAME,
            source   = ctx.env.BLIB_CLIENT_SOURCES,
            includes = 'include',
            export_includes = 'include',
            use      = 'csp' if ctx.env.BLIB_CLIENT_ENABLE_CSP_CLIENT else '')

        if ctx.env.BLIB_CLIENT_ENABLE_SLASH_COMMANDS:
            ctx.objects(
                target   = APPNAME + '-cmd',
                source   = 'src/cmd/blib_client_commands.c',
                use      = [APPNAME, 'slash', 'util'])

    # Build shared library
    if not ctx.env.BLIB_CLIENT_DISABLE_SHARED:
        ctx.shlib(
            name     = APPNAME + '-shlib',
            target   = APPNAME,
            source   = ctx.env.BLIB_CLIENT_SOURCES,
            includes = 'include',
            export_includes = 'include',
            install_path = '${LIBDIR}/satlab/',
            use      = 'csp_shlib' if ctx.env.BLIB_CLIENT_ENABLE_CSP_CLIENT else '')

    # Python bindings
    if ctx.env.BLIB_CLIENT_ENABLE_PYTHON_BINDINGS:
        ctx.load('python')

        # Make name compatible with Python
        APPNAME_PYTHON = APPNAME.replace('-', '_')

        # Build Python extension module
        ctx.shlib(
            features     = 'pyext',
            name         = APPNAME + '-python',
            target       = APPNAME_PYTHON + '_bindings',
            source       = ctx.path.ant_glob('python/bindings/*.c'),
            install_path = '${PYTHONDIR}/satlab/' + APPNAME_PYTHON,
            rpath        = ctx.env.LIBDIR + '/satlab',
            use          = [APPNAME + '-shlib'])

        # Build pure Python module
        ctx(features       = 'py',
            source         = 'python/__init__.py',
            install_path   = '${PYTHONDIR}/satlab/' + APPNAME_PYTHON,
            install_from   = 'python/')

        # Install update tool
        ctx.install_as('${BINDIR}/blib-update',
                       'tools/blib-update.py',
                       chmod=0o755)
