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

import subprocess

APPNAME = 'satctl'
VERSION = '1.27.0'

top = '.'
out = 'build'

modules = [
    'client/polaris-ais',
    'client/polaris-adsb',
    'client/polaris-lora',
    'client/srs3',
    'client/srs4',
    'client/srx8',
    'client/ais',
    'lib/csp',
    'lib/slash',
    'lib/btp-client',
    'lib/prop-client',
    'lib/blib-client',
    'lib/util']

# Override VERSION with revision if building from git repo
try:
    rev = subprocess.check_output(
        ['git', '--git-dir=.git', 'describe', '--always', '--dirty=+'],
        stderr=subprocess.DEVNULL
    )
    VERSION = rev.decode('ascii').strip()
except:
    pass

def options(ctx):
    ctx.add_option('--satctl-enable-python-bindings', action='store_true', help='Enable Python bindings')

    ctx.recurse(modules)

def configure(ctx):
    ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS = ctx.options.satctl_enable_python_bindings

    ctx.load('gnu_dirs')

    ctx.env.CFLAGS = [
        '-O2',
        '-D_FORTIFY_SOURCE=3',
        '-flto=auto',
        '-Wall',
        '-Wshadow',
        '-Wextra',
        '-Wstrict-prototypes',
        '-Wwrite-strings',
        '-Wcast-align',
        '-Wlogical-op',
        '-Wduplicated-cond',
        '-Wnull-dereference',
        '-Wno-unused-parameter',
        '-Wno-sign-compare',
    ]

    ctx.env.VERSION = VERSION

    # Check for ZMQ
    ctx.check_cfg(package='libzmq', args='--cflags --libs', mandatory=False)

    # Check for libsocketcan
    ctx.check_cfg(package='libsocketcan', args='--cflags --libs', mandatory=False)

    ctx.load('compiler_c')
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.load('python')
        ctx.check_python_headers()

    # CSP options
    ctx.options.disable_stlib = True
    ctx.options.enable_objects = True
    ctx.options.enable_if_can = True
    ctx.options.enable_if_kiss = True
    ctx.options.enable_if_mcast = True
    ctx.options.enable_if_zmqhub = ctx.env.HAVE_LIBZMQ
    ctx.options.enable_crc32 = True
    ctx.options.enable_rdp = True
    ctx.options.enable_can_socketcan = True
    ctx.options.enable_if_slgnd = True
    ctx.options.enable_if_sludp = True
    ctx.options.with_driver_usart = 'linux'
    ctx.options.with_rtable = 'cidr'
    ctx.options.enable_csperf = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.enable_shlib = True
        ctx.options.enable_python3_bindings = True

    # util options
    ctx.options.util_enable_objects = True
    ctx.options.util_enable_linux = True
    ctx.options.util_enable_slash = True
    ctx.options.util_enable_slash_csp = True
    if not ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.util_disable_shared = True

    # prop-client options
    ctx.options.prop_client_enable_objects = True
    ctx.options.prop_client_enable_slash_commands = True
    ctx.options.prop_client_enable_csp_client = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.prop_client_enable_python_bindings = True
    else:
        ctx.options.prop_client_disable_shared = True

    # btp-client options
    ctx.options.btp_client_enable_objects = True
    ctx.options.btp_client_enable_slash_commands = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.btp_client_enable_python_bindings = True
    else:
        ctx.options.btp_client_disable_shared = True

    # blib-client options
    ctx.options.blib_client_enable_objects = True
    ctx.options.blib_client_enable_slash_commands = True
    ctx.options.blib_client_enable_csp_client = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.blib_client_enable_python_bindings = True
    else:
        ctx.options.blib_client_disable_shared = True

    # srs3-client options
    ctx.options.srs3_client_enable_objects = True
    ctx.options.srs3_client_enable_slash_commands = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.srs3_client_enable_python_bindings = True
    else:
        ctx.options.srs3_client_disable_shared = True

    # srs4-client options
    ctx.options.srs4_client_enable_objects = True
    ctx.options.srs4_client_enable_slash_commands = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.srs4_client_enable_python_bindings = True
    else:
        ctx.options.srs4_client_disable_shared = True

    # srx8-client options
    ctx.options.srx8_client_enable_objects = True
    ctx.options.srx8_client_enable_slash_commands = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.srx8_client_enable_python_bindings = True
    else:
        ctx.options.srx8_client_disable_shared = True

    # polaris-ais-client options
    ctx.options.polaris_ais_client_enable_objects = True
    ctx.options.polaris_ais_client_enable_slash_commands = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.polaris_ais_client_enable_python_bindings = True
    else:
        ctx.options.polaris_ais_client_disable_shared = True

    # polaris-adsb-client options
    ctx.options.polaris_adsb_client_enable_objects = True
    ctx.options.polaris_adsb_client_enable_slash_commands = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.polaris_adsb_client_enable_python_bindings = True
    else:
        ctx.options.polaris_adsb_client_disable_shared = True

    # polaris-lora-client options
    ctx.options.polaris_lora_client_enable_objects = True
    ctx.options.polaris_lora_client_enable_slash_commands = True
    if ctx.env.SATCTL_ENABLE_PYTHON_BINDINGS:
        ctx.options.polaris_lora_client_enable_python_bindings = True
    else:
        ctx.options.polaris_lora_client_disable_shared = True

    # Define for slash
    ctx.env.DEFINES += ['SLASH_HAVE_SELECT']

    ctx.recurse(modules)

def build(ctx):
    ctx.recurse(modules)

    use = [
        'csp',
        'slash',
        'util',
        'util-linux',
        'util-slash',
        'btp-client-cmd',
        'prop-client',
        'blib-client',
        'ais-client-cmd',
        'srs3-client-cmd',
        'srs4-client-cmd',
        'srx8-client-cmd',
        'polaris-ais-client-cmd',
        'polaris-adsb-client-cmd',
        'polaris-lora-client-cmd']

    if ctx.env.HAVE_LIBZMQ:
        use.append('LIBZMQ')

    if ctx.env.HAVE_LIBSOCKETCAN:
        use.append('LIBSOCKETCAN')

    ctx.program(
        target   = APPNAME,
        source   = ['src/satctl.c'],
        use      = use,
        defines  = ['SATCTL_VERSION="{}"'.format(VERSION)],
        lib      = ['pthread', 'util', 'rt'])

    ctx(target='satctl.1',
        source='man/satctl.1.in',
        rule='sed -e "s,@version@,${VERSION},g" ${SRC} > ${TGT}')

    ctx.install_files('${MANDIR}/man1', 'satctl.1')

def dist(ctx):
    ctx.base_name = 'satlab-' + APPNAME + '-' + VERSION
    ctx.algo      = 'tar.xz'
    ctx.excl      = '**/.* build Jenkinsfile'
