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

import sys, os, TaskGen, ctypes, wscript_helper

def configure(conf):
    pass

def build(bld):
    bundle = 'gx_amp_stereo.lv2'
    
    src = ['gxamp_stereo.cpp',
           'gx_amp_stereo.cc',
           'gx_tonestack_stereo.cc',
           '../DSP/gx_resampler.cc',
           '../DSP/gx_convolver.cc'
           ]
    incl = ['../faust','./', '../DSP', '../DSP/tube_tables']
    lib = []
    if sys.platform.startswith("linux"):
        lib.append('dl')
    uselib = ['LV2CORE','GLIBMM']
    if not bld.env['ZITA_CONVOLVER']:
        if not bld.env['CONVOLVER_FFMPEG']:
            uselib.append('FFTW3')
    wscript_helper.add_zita_convolver(bld, uselib, src, incl, base="../..")
    wscript_helper.add_zita_resampler(bld, uselib, src, incl, base="../..")
    cxxflag =[]
    ldscript = bld.path.find_resource('gxamp_stereo.lds')
    mapfile = bld.path.find_or_declare("gxamp_stereo.map")
    if not bld.env['OPT'] and bld.env['SSE2']:
        cxxflag = [ "-msse2", "-mfpmath=sse"]
    lv2_plugin = bld(
        features='cxx cshlib ',
        includes = incl,
        lib = lib,
        uselib = uselib,
        obj_ext  = '_13.o',
        cxxflags = cxxflag,
        defines  = ["LV2_SO"],
        target   = 'gx_amp_stereo',
        source   = src,
        install_path = '${LV2DIR}/%s' % bundle,
        chmod = 0o755,
        ldflags = ['-Wl,%s,-Map=%s' % (ldscript.srcpath(),mapfile.bldpath(bld.env))],
        )
    bld.add_manual_dependency(bld.path.find_or_declare(lv2_plugin.target), ldscript)
    lv2_plugin.env['shlib_PATTERN'] = '%s.so'
    
    uselib_local1 = []
    libpath1 = []
    lib1 = []
    incl = ['../../../libgxwmm','../../../libgxw']
    if sys.platform.startswith("linux"):
        lib1.append('dl')
    if bld.env["GX_LIB_SHARED"]:
        lib1 += ['gxwmm','gxw']
        libpath1 += [bld.path.find_dir("../../../libgxw/gxw").bldpath(bld.env),
                    bld.path.find_dir("../../../libgxwmm/gxwmm").bldpath(bld.env)]
    else:
        uselib_local1 += ['gxwmm','gxw']
    
    lv2_plugin_gui = bld(
        features='cxx cshlib ',
        includes = incl,
        lib = lib1,
        uselib = 'LV2CORE GTKMM',
        libpath = libpath1,
        uselib_local = uselib_local1,
        linkflags = '-Wl,-z,nodelete',
        defines  = ["LV2_GUI"],
        target   = 'gx_amp_stereo_gui',
        source   = 'widget_stereo.cpp gxamp_stereo_gui.cpp',
        install_path = '${LV2DIR}/%s' % bundle,
        chmod = 0o755,
        )
    lv2_plugin_gui.env['shlib_PATTERN'] = '%s.so'

    
    install_path = '${LV2DIR}/%s' % bundle,
    bld.install_files('${LV2DIR}/gx_amp_stereo.lv2', 'manifest.ttl')
    bld.install_files('${LV2DIR}/gx_amp_stereo.lv2', 'gx_amp_stereo.ttl')

