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

def configure(conf):
    conf.check_cfg(package='alsa', atleast_version='1.0.18', args='--cflags --libs')
    conf.env['BUILD_DRIVER_ALSA'] = conf.is_defined('HAVE_ALSA')

    conf. check_cfg(package='libfreebob', atleast_version='1.0.0', args='--cflags --libs')
    conf.env['BUILD_DRIVER_FREEBOB'] = conf.is_defined('HAVE_LIBFREEBOB')

    conf. check_cfg(package='libffado', atleast_version='1.999.17', args='--cflags --libs')
    conf.env['BUILD_DRIVER_FFADO'] = conf.is_defined('HAVE_LIBFFADO')

    conf.define('HAVE_PPOLL', 1 )


def create_jack_driver_obj(bld, target, sources, uselib = None):
    driver = bld.new_task_gen('cxx', 'shlib')
    driver.features.append('cc')
    driver.env['shlib_PATTERN'] = 'jack_%s.so'
    driver.defines = ['HAVE_CONFIG_H','SERVER_SIDE', 'HAVE_PPOLL']
    driver.includes = ['.', '../linux', '../posix', '../common', '../common/jack', '../dbus']
    driver.target = target
    driver.source = sources
    driver.install_path = '${ADDON_DIR}/'
    if uselib:
        driver.uselib = uselib
    driver.uselib_local = 'serverlib'
    return driver

def build(bld):
    if bld.env['BUILD_JACKD'] == True:
        jackd = bld.new_task_gen('cxx', 'program')
        jackd.includes = ['../linux', '../posix', '../common/jack', '../common', '../dbus']
        jackd.defines = 'HAVE_CONFIG_H'
        jackd.source = ['../common/Jackdmp.cpp']
	if bld.env['IS_LINUX'] and bld.env['BUILD_JACKDBUS']: 
            jackd.source += ['../dbus/reserve.c', '../dbus/audio_reserve.c'] 
            jackd.uselib = 'PTHREAD DL RT DBUS-1'
	else:
            jackd.uselib = 'PTHREAD DL RT'		
        jackd.uselib_local = 'serverlib'
        jackd.target = 'jackd'
  	
    create_jack_driver_obj(bld, 'dummy', '../common/JackDummyDriver.cpp')
    
    alsa_driver_src = [
                       'alsa/JackAlsaDriver.cpp',
                       'alsa/alsa_rawmidi.c',
                       'alsa/alsa_seqmidi.c',
                       'alsa/alsa_midi_jackmp.cpp',
                       '../common/memops.c',
                       'alsa/generic_hw.c',
                       'alsa/hdsp.c',
		       'alsa/alsa_driver.c',
                       'alsa/hammerfall.c',
                       'alsa/ice1712.c'
                       ]

    alsarawmidi_driver_src = ['alsarawmidi/JackALSARawMidiDriver.cpp',
                              'alsarawmidi/JackALSARawMidiInputPort.cpp',
                              'alsarawmidi/JackALSARawMidiOutputPort.cpp',
                              'alsarawmidi/JackALSARawMidiPort.cpp',
                              'alsarawmidi/JackALSARawMidiReceiveQueue.cpp',
                              'alsarawmidi/JackALSARawMidiSendQueue.cpp'
                              ]

    ffado_driver_src = ['firewire/JackFFADODriver.cpp',
                        'firewire/JackFFADOMidiInputPort.cpp',
                        'firewire/JackFFADOMidiOutputPort.cpp',
                        'firewire/JackFFADOMidiReceiveQueue.cpp',
                        'firewire/JackFFADOMidiSendQueue.cpp'
                       ]

    if bld.env['BUILD_DRIVER_ALSA'] == True:
        create_jack_driver_obj(bld, 'alsa', alsa_driver_src, "ALSA")
        create_jack_driver_obj(bld, 'alsarawmidi', alsarawmidi_driver_src,
                               "ALSA")

    if bld.env['BUILD_DRIVER_FREEBOB'] == True:
        create_jack_driver_obj(bld, 'freebob', 'freebob/JackFreebobDriver.cpp', "LIBFREEBOB")

    if bld.env['BUILD_DRIVER_FFADO'] == True:
        create_jack_driver_obj(bld, 'firewire', ffado_driver_src, "LIBFFADO")

    create_jack_driver_obj(bld, 'net', '../common/JackNetDriver.cpp')

    create_jack_driver_obj(bld, 'loopback', '../common/JackLoopbackDriver.cpp')

    create_jack_driver_obj(bld, 'netone', [ '../common/JackNetOneDriver.cpp',
                                            '../common/netjack.c',
                                            '../common/netjack_packet.c' ], "SAMPLERATE CELT" )

