#!/usr/bin/env python
import os
import subprocess
import shutil
from waflib import Logs
from waflib.extras import autowaf as autowaf

# Variables for 'waf dist'
APPNAME = 'avw.lv2'
VERSION = '0.1.6'

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


def options(opt):
    autowaf.set_options(opt)
    opt.load('compiler_cxx')
    
    
def configure(conf):
    autowaf.configure(conf)
    autowaf.display_header('AVW Configuration')
    
    conf.load('compiler_cxx')
    
    autowaf.check_pkg(conf, 'lv2', uselib_store='LV2', atleast_version='1.2.0')
    autowaf.check_pkg(conf, 'lvtk-plugin-1', uselib_store='LVTK_PLUGIN', atleast_version='1.1.1')

    check = 'Extended Initializer Lists'
    conf.check_cxx(msg         = check,
                   define_name = 'WAF_CXX_FEATURE_' + check.replace(' ', '_').upper(),
                   fragment    = '#include <vector>\n'+
                                 'struct foo {\n'+
                                 '  int i;\n'+
                                 '  int j;\n'+
                                 '  int k;\n'+
                                 '};\n'+
                                 'int main() {\n'+
                                 '  std::vector<foo> v(1);\n'+
                                 '  v[0] = {0, 0, 0};\n'+
                                 '  return 0;\n'+
                                 '}',
                   mandatory   = False)
    
    # Set env['pluginlib_PATTERN']
    pat = conf.env['cxxshlib_PATTERN']
    if pat[0:3] == 'lib':
        pat = pat[3:]
    conf.env['pluginlib_PATTERN'] = pat

    autowaf.display_msg(conf, "LV2 bundle directory", conf.env['LV2DIR'])
    print('')


def build_plugin(bld, bundle, name, source, cxxflags=[], libs=[], add_source=[]):
    penv = bld.env.derive()
    penv['cxxshlib_PATTERN'] = bld.env['pluginlib_PATTERN']
    obj              = bld(features = 'cxx cxxshlib')
    obj.env          = penv
    obj.source       = source + add_source
    obj.includes     = ['.', './src']
    obj.name         = name
    obj.target       = os.path.join(bundle, name)
    if cxxflags != []:
        obj.cxxflags = cxxflags
    if libs != []:
        obj.uselib = libs
    obj.install_path = '${LV2DIR}/' + bundle

    # Install data file
    data_file = '%s.ttl' % name
    bld.install_files('${LV2DIR}/' + bundle, os.path.join(bundle, data_file))


def build_plugin_gui(bld, bundle, name, source, cxxflags=[], libs=[], add_source=[]):
    penv = bld.env.derive()
    penv['cxxshlib_PATTERN'] = bld.env['pluginlib_PATTERN']
    obj              = bld(features = 'cxx cxxshlib')
    obj.env          = penv
    obj.source       = source + add_source
    obj.includes     = ['.', './src']
    obj.name         = name
    obj.target       = os.path.join(bundle, name)
    if cxxflags != []:
        obj.cxxflags = cxxflags
    if libs != []:
        obj.uselib = libs
    obj.install_path = '${LV2DIR}/' + bundle


def build(bld):
    def do_copy(task):
        src = task.inputs[0].abspath()
        tgt = task.outputs[0].abspath()
        return shutil.copy(src, tgt)
    
    """for i in bld.path.ant_glob('avw.lv2/*.ttl'):
        bld(rule   = do_copy,
            source = i,
            target = bld.path.get_bld().make_node('avw.lv2/%s' % i),
            install_path = '${LV2DIR}/avw.lv2')
"""



    plugins = '''
    advenv
    amp
    env
    hztovc
    lfo_tempo
    lfo_freq
    mixer_4ch_audio
    mixer_4ch_cv
    mixer_2ch_audio
    mixer_2ch_cv
    noise2_audio
    noise2_cv
    slew
    vcalin_audio
    vcalin_cv
    vctohz
    cvs
    delay
    absolute
    inv
    ringmodulator_audio
    ringmodulator_cv
    percussiveenv
    samplehold
    vcpanning
    vcswitch_audio
    vcswitch_cv
    mooglpf
    downsampler_mono
    downsampler_stereo
    controltocv
    cvtocontrol
    amplitude
    scope
    '''.split()

    for i in plugins:
        build_plugin(bld, 'avw.lv2', i, ['src/%s.cpp' % i],
                     ['-DPLUGIN_CLASS=%s' % i,
                      '-std=c++11',
                      '-DURI_PREFIX=\"http://avwlv2.sourceforge.net/plugins/avw/\"',
                      '-DPLUGIN_URI_SUFFIX="%s"' % i,
                      '-DPLUGIN_HEADER="src/%s.hpp"' % i],
                     ['LV2', 'LVTK_PLUGIN'],
                     [])




    plugins = '''
    granulator_mono
    granulator_stereo
    beatrepeater_mono
    beatrepeater_stereo
    beatslicer_mono
    beatslicer_stereo
    '''.split()

    for i in plugins:
        build_plugin(bld, 'avw.lv2', i, ['src/%s.cpp' % i],
                     ['-DPLUGIN_CLASS=%s' % i,
                      '-std=c++11',
                      '-DURI_PREFIX=\"http://avwlv2.sourceforge.net/plugins/avw/\"',
                      '-DPLUGIN_URI_SUFFIX="%s"' % i,
                      '-DPLUGIN_HEADER="src/%s.hpp"' % i],
                     ['LV2', 'LVTK_PLUGIN'],
                     ['src/envgen.cpp'])




    plugins = '''
    vco2_audio
    vco2_cv
    ad
    vcf
    vcaexp_audio
    vcaexp_cv
    '''.split()

    for i in plugins:
        build_plugin(bld, 'avw.lv2', i, ['src/%s.cpp' % i],
                     ['-DPLUGIN_CLASS=%s' % i,
                      '-std=c++11',
                      '-DURI_PREFIX=\"http://avwlv2.sourceforge.net/plugins/avw/\"',
                      '-DPLUGIN_URI_SUFFIX="%s"' % i,
                      '-DPLUGIN_HEADER="src/%s.hpp"' % i],
                      ['LV2', 'LVTK_PLUGIN'],
                      ['src/synthdata.cpp'])
      

