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

APPNAME = 'btp-client'
VERSION = '1.2.0'

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

def configure(ctx):
    ctx.env.BTP_CLIENT_DISABLE_SHARED = ctx.options.btp_client_disable_shared
    ctx.env.BTP_CLIENT_ENABLE_OBJECTS = ctx.options.btp_client_enable_objects
    ctx.env.BTP_CLIENT_ENABLE_SLASH_COMMANDS = ctx.options.btp_client_enable_slash_commands
    ctx.env.BTP_CLIENT_ENABLE_PYTHON_BINDINGS = ctx.options.btp_client_enable_python_bindings

    ctx.check(header_name='termios.h',
              features='c cprogram',
              mandatory=False,
              define_name='BTP_CLIENT_HAVE_TERMIOS_H')

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

def build(ctx):

    # Object files
    if ctx.env.BTP_CLIENT_ENABLE_OBJECTS:
        ctx.objects(
            target   = APPNAME + '-core',
            source   = ['src/crc32.c', 'src/error.c', 'src/bounds.c'],
            includes = 'include',
            export_includes = 'include',
            use      = ['util', 'csp'])

        ctx.objects(
            target   = APPNAME,
            source   = ['src/client.c'],
            export_includes = 'include',
            use      = APPNAME + '-core')

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

    # Build shared library
    if not ctx.env.BTP_CLIENT_DISABLE_SHARED:
        ctx.shlib(
            name     = APPNAME + '-shlib',
            target   = APPNAME,
            source   = ['src/crc32.c', 'src/error.c', 'src/client.c', 'src/bounds.c'],
            includes = 'include',
            export_includes = 'include',
            install_path = '${LIBDIR}/satlab/',
            use = ['csp_shlib', 'util-shlib'])

    # Python bindings
    if ctx.env.BTP_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', 'zmq'])

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