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

example_programs = {
    'jack_freewheel' : 'freewheel.c',
    'jack_connect' : 'connect.c',
    'jack_lsp' : 'lsp.c',
    'jack_metro' : 'metro.c',
    'jack_midiseq' : 'midiseq.c',
    'jack_midisine' : 'midisine.c',
    'jack_showtime' : 'showtime.c',
    'jack_simple_client' : 'simple_client.c',
    'jack_zombie' : 'zombie.c',
    'jack_load' : 'ipload.c',
    'jack_unload' : 'ipunload.c',
    'jack_showtime' : 'showtime.c',
    'jack_alias' : 'alias.c',
    'jack_bufsize' : 'bufsize.c',
    'jack_evmon' : 'evmon.c',
    'jack_monitor_client' : 'monitor_client.c',
    'jack_thru' : 'thru_client.c',
    'jack_server_control' : 'server_control.cpp',
    }

example_libs = {
    'inprocess' : 'inprocess.c',
    }

def configure(conf):
    e = conf.check_cc(header_name='sndfile.h', define_name="HAVE_SNDFILE")
 
    if conf.is_defined('HAVE_SNDFILE'):
        conf.env['LIB_SNDFILE'] = ['sndfile']

    e = conf.check_cc(header_name='ncurses.h', define_name="HAVE_NCURSES")

    if conf.is_defined('HAVE_NCURSES'):
        conf.env['LIB_NCURSES'] = ['ncurses']

    e = conf.check_cc(lib='readline', define_name="HAVE_READLINE")

    if conf.is_defined('HAVE_READLINE'):
        conf.env['LIB_READLINE'] = ['readline']

    conf.env['BUILD_EXAMPLE_CLIENT_TRANSPORT'] = conf.is_defined('HAVE_READLINE') and conf.is_defined('HAVE_NCURSES')

    conf.env['BUILD_EXAMPLE_CLIENT_REC'] = conf.is_defined('HAVE_SNDFILE')

def build(bld):
    if bld.env['IS_LINUX']:
        os_incdir = ['../linux', '../posix']
    if bld.env['IS_MACOSX']:
        os_incdir = ['../macosx', '../posix']
    for example_program, example_program_source in example_programs.items():
        prog = bld.new_task_gen('cxx', 'program')
        prog.features.append('cc')
        prog.includes = os_incdir + ['../common/jack', '../common']
        prog.source = example_program_source
        if bld.env['IS_MACOSX']:
            prog.env.append_value("CPPFLAGS", "-mmacosx-version-min=10.4 -arch i386 -arch ppc")
            #prog.env.append_value("LINKFLAGS", "-arch i386 -arch ppc")
            prog.env.append_value("LINKFLAGS", "")
        if bld.env['IS_LINUX']:
            prog.uselib = 'RT'
        if example_program == 'jack_server_control':
            prog.uselib_local = 'serverlib'
        else:
            prog.uselib_local = 'clientlib'              
        prog.target = example_program
   
    #if bld.env['BUILD_EXAMPLE_CLIENT_TRANSPORT']
    if bld.env['BUILD_EXAMPLE_CLIENT_TRANSPORT'] == True:
        prog = bld.new_task_gen('cc', 'program')
        prog.includes = os_incdir + ['../common/jack', '../common']
        prog.source = 'transport.c'
        if bld.env['IS_MACOSX']:
            prog.env.append_value("CPPFLAGS", "-mmacosx-version-min=10.4 -arch i386 -arch ppc")
            #prog.env.append_value("LINKFLAGS", "-arch i386 -arch ppc")
            prog.env.append_value("LINKFLAGS", "")
        if bld.env['IS_LINUX']:
            prog.uselib = 'RT READLINE NCURSES'
        if bld.env['IS_MACOSX']:
	    prog.uselib = 'READLINE NCURSES'
        prog.uselib_local = 'clientlib'
        prog.target = 'jack_transport'

    if bld.env['BUILD_EXAMPLE_CLIENT_REC'] == True:
        prog = bld.new_task_gen('cc', 'program')
        prog.includes = os_incdir + ['../common/jack', '../common']
        prog.source = 'capture_client.c'
        if bld.env['IS_MACOSX']:
            prog.env.append_value("CPPFLAGS", "-mmacosx-version-min=10.4 -arch i386 -arch ppc")
            #prog.env.append_value("LINKFLAGS", "-arch i386 -arch ppc")
            prog.env.append_value("LINKFLAGS", "")
        if bld.env['IS_LINUX']:
            prog.uselib = 'RT SNDFILE'
        if bld.env['IS_MACOSX']:
            prog.uselib = 'SNDFILE'
        prog.uselib_local = 'clientlib'
        prog.target = 'jack_rec'

    for example_lib, example_lib_source in example_libs.items():
        lib = bld.new_task_gen('cc', 'shlib')
        lib.env['shlib_PATTERN'] = '%s.so'
        lib.includes = os_incdir + ['../common/jack', '../common']
        lib.target = example_lib
        lib.source = example_lib_source
        if bld.env['IS_MACOSX']:
            lib.env.append_value("CPPFLAGS", "-mmacosx-version-min=10.4 -arch i386 -arch ppc")
            #lib.env.append_value("LINKFLAGS", "-arch i386 -arch ppc")
            lib.env.append_value("LINKFLAGS", "")
        lib.uselib_local = 'serverlib'
        lib.install_path = '${ADDON_DIR}/'

    bld.symlink_as('${PREFIX}/bin/jack_disconnect', 'jack_connect')

    bld.install_files('${PREFIX}/bin', 'jack_control', chmod=0755)
