#!/usr/bin/env python
from waflib.extras import autowaf as autowaf
from waflib import Options

# Version of this package (even if built as a child)
LIBSURFACES_VERSION = '4.1.0'

# Library version (UNIX style major, minor, micro)
# major increment <=> incompatible changes
# minor increment <=> compatible changes (additions)
# micro increment <=> no interface changes
LIBSURFACES_LIB_VERSION = '4.1.0'

# Variables for 'waf dist'
APPNAME = 'libsurfaces'
VERSION = LIBSURFACES_VERSION

# Mandatory variables
top = '.'
out = 'build'

children = [
    'faderport',
    'faderport8',
    'cc121',
    'generic_midi',
    'mackie',
    'us2400',
    'launch_control_xl',
    'osc',
]

def options(opt):
    autowaf.set_options(opt)

def sub_config_and_use(conf, name, has_objects = True):
    conf.recurse(name)
    autowaf.set_local_lib(conf, name, has_objects)

def configure(conf):
    global children

    autowaf.set_recursive()

    autowaf.check_pkg(conf, 'libusb-1.0', uselib_store='USB', mandatory=False)
    #if Options.options.tranzport and conf.is_defined('HAVE_USB'):
    #    conf.define('BUILD_TRANZPORT', 1)

    if conf.is_defined('HAVE_USB'):
        children += [ 'push2' ]
        children += [ 'contourdesign' ]

    else:
        print ('You are missing the libusb-1.0 development package needed to compile Push2 and ContourDesign support')

    if conf.is_defined('HAVE_HIDAPI') and Options.options.maschine:
        children += [ 'maschine2' ]
        conf.define('BUILD_MASCHINE', 1)

    conf.check_cc (header_name='cwiid.h', define_name='HAVE_CWIID_H',mandatory=False)
    if conf.is_defined('HAVE_CWIID_H'):
        conf.check_cc (header_name='bluetooth/bluetooth.h', define_name='HAVE_BLUETOOTH_H',mandatory=False)
        if conf.is_defined('HAVE_BLUETOOTH_H'):
            autowaf.check_pkg(conf, 'cwiid', uselib_store='CWIID', atleast_version='0.6.00',mandatory=False)
            if conf.is_defined ('HAVE_CWIID'):
                conf.define ('BUILD_WIIMOTE', 1)
                children += [ 'wiimote' ]
            else:
                print('You have the cwiid headers needed to compile wiimote support BUT you are missing the pkg-config file for cwiid')
        else:
            print('You are missing the libbluetooth headers needed to compile wiimote support')
    else:
        print('You are missing the cwiid headers needed to compile wiimote support')

    autowaf.check_pkg(conf, 'libwebsockets', uselib_store='WEBSOCKETS', atleast_version='2.0.0', mandatory=False)
    if conf.is_defined('HAVE_WEBSOCKETS'):
        children += [ 'websockets' ]

    for i in children:
        sub_config_and_use(conf, i)

def build(bld):
    bld.recurse('generic_midi')
    bld.recurse('faderport')
    bld.recurse('faderport8')
    bld.recurse('cc121')
    bld.recurse('mackie')
    bld.recurse('us2400')
    bld.recurse('launch_control_xl')
    bld.recurse('osc')

    if bld.is_defined('BUILD_WIIMOTE'):
        bld.recurse('wiimote')
    if bld.is_defined('BUILD_TRANZPORT'):
        bld.recurse('tranzport')
    if bld.is_defined('HAVE_USB'):
        bld.recurse('push2')
        bld.recurse('contourdesign')
    if bld.is_defined('BUILD_MASCHINE'):
        bld.recurse('maschine2')
    if bld.is_defined ('HAVE_WEBSOCKETS'):
        bld.recurse('websockets')

def shutdown():
    autowaf.shutdown()
