#
# Copyright (C) 2007 Arnold Krille
# Copyright (C) 2007 Pieter Palmers
# Copyright (C) 2008 Marc-Olivier Barre
#
# This file originates from FFADO (www.ffado.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
from string import Template

Import('env')

# paths where include files can be found
env.AppendUnique(CPPPATH=['#/', '#/common'])

# A symlinking command
symlinkcmd = 'rm -f $TARGET;ln -nsf $SOURCE.name $TARGET'

#
# Source files section
#

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',
    }

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

# Libraries to link
extra_libs = {}
for example_program in example_programs:
    extra_libs[example_program] = ['jack']

# TODO: we need to test for READLINE... is that even possible ?
env['HAS_READLINE']=True
if env['HAS_READLINE']:
    example_programs['jack_transport'] = 'transport.c'
    extra_libs['jack_transport'] = ['readline', 'jack', 'ncurses']

# TODO: we need to test for SNDFILE... 
env['HAS_SNDFILE']=True
if env['HAS_SNDFILE']:
    example_programs['jack_rec'] = 'capture_client.c'
    extra_libs['jack_rec'] = ['sndfile', 'jack']

#
# Build/install section
#

if env['BUILD_EXAMPLES']:
    clientenv = env.Copy()
    clientenv.PrependUnique(LIBPATH=env['build_base'])
    for example_program, example_program_source in example_programs.items():
        clientenv.Program(example_program, example_program_source, LIBS=extra_libs[example_program])
        if env['INSTALL_EXAMPLES']:
            clientenv.Install(env['INSTALL_BINDIR'], example_program)
    jack_disconnect_install_path = env['INSTALL_BINDIR'] + '/jack_disconnect'
    env.Command(jack_disconnect_install_path, 'jack_connect', symlinkcmd)
    env.Alias('install', jack_disconnect_install_path)
    for example_lib, example_lib_source in example_libs.items():
        lib = clientenv.SharedLibrary(example_lib, example_lib_source)
        if clientenv['INSTALL_EXAMPLES']:
            env.InstallAs(env['INSTALL_ADDON_DIR'] + '/' + example_lib + '.so', lib)
            env.Alias('install', env['INSTALL_ADDON_DIR'] + '/' + example_lib + '.so')

if env['ENABLE_DBUS']:
    clientenv.Install(env['BINDIR'], 'jack_control')
