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

APPNAME = 'srs4-client'
VERSION = '2.1.0'

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

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

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

    ctx.env.SRS4_CLIENT_DISABLE_SHARED = ctx.options.srs4_client_disable_shared
    ctx.env.SRS4_CLIENT_ENABLE_OBJECTS = ctx.options.srs4_client_enable_objects
    ctx.env.SRS4_CLIENT_ENABLE_SLASH_COMMANDS = ctx.options.srs4_client_enable_slash_commands
    ctx.env.SRS4_CLIENT_ENABLE_PYTHON_BINDINGS = ctx.options.srs4_client_enable_python_bindings

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

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

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

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

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

    # Python bindings
    if ctx.env.SRS4_CLIENT_ENABLE_PYTHON_BINDINGS:
        # Load Python tool
        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/')
