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

APPNAME = 'polaris-adsb-client'
VERSION = '1.1.0'


def options(ctx):

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


def configure(ctx):

    ctx.env.POLARIS_ADSB_CLIENT_DISABLE_SHARED = ctx.options.polaris_adsb_client_disable_shared
    ctx.env.POLARIS_ADSB_CLIENT_ENABLE_OBJECTS = ctx.options.polaris_adsb_client_enable_objects
    ctx.env.POLARIS_ADSB_CLIENT_ENABLE_SLASH_COMMANDS = ctx.options.polaris_adsb_client_enable_slash_commands
    ctx.env.POLARIS_ADSB_CLIENT_ENABLE_PYTHON_BINDINGS = ctx.options.polaris_adsb_client_enable_python_bindings


def build(ctx):
    ctx.load('compiler_c')

    ctx(name=APPNAME + '-props',
        rule='${PROPGEN} ${SRC} -i ${TGT[0].bldpath()} -s ${TGT[1].bldpath()}',
        source=ctx.path.find_resource('props/polaris_adsb_props.json'),
        target=[ctx.path.find_or_declare('include/satlab/polaris_adsb_props.h'), ctx.path.find_or_declare('src/polaris_adsb_props.c')])

    # Object files
    if ctx.env.POLARIS_ADSB_CLIENT_ENABLE_OBJECTS:
        ctx.objects(
            target=APPNAME,
            source=ctx.path.ant_glob('src/polaris*.c') + [ctx.path.find_resource('src/polaris_adsb_props.c')],
            includes='include',
            export_includes='include',
            use=['csp', 'util', 'prop-client', APPNAME + '-props'])

        if ctx.env.POLARIS_ADSB_CLIENT_ENABLE_SLASH_COMMANDS:
            ctx.objects(
                target=APPNAME + '-cmd',
                source='src/cmd_polaris.c',
                includes='include',
                use=[APPNAME, 'csp', 'slash', 'util', 'prop-client-cmd', 'btp-client-cmd'])

    # Shared library
    if not ctx.env.POLARIS_ADSB_CLIENT_DISABLE_SHARED:
        ctx.shlib(
            name=APPNAME + '-shlib',
            target=APPNAME,
            source=ctx.path.ant_glob('src/polaris*.c') + [ctx.path.find_resource('src/polaris_adsb_props.c')],
            includes='include',
            export_includes='include',
            install_path='${LIBDIR}/satlab/',
            use=['csp_shlib', 'util-shlib', 'prop-client-shlib', APPNAME + '-props'])

    # Python bindings
    if ctx.env.POLARIS_ADSB_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/')
