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

from waflib.Context import g_module

def append_sse_flags():
    cpu_model = None
    x86_flags = None
    try:
        for line in open("/proc/cpuinfo"):
            if cpu_model is None:
                if line.startswith("model name"):
                    cpu_model = line.split(":",1)[1].strip()
            elif x86_flags is None:
                if line.startswith("flags"):
                    x86_flags = line.split(":",1)[1].strip()
            else:
                break
    except IOError:
        pass
    if cpu_model is None or x86_flags is None:
        return False
    if "sse2" in x86_flags:
        return True
    else:
        return False

subdirs = [
    'gx_amp.lv2',
    'gx_amp_stereo.lv2',
    'gxautowah.lv2',
    'gxbooster.lv2',
    'gx_chorus.lv2',
    'gx_compressor.lv2',
    'gx_delay.lv2',
    'gxechocat.lv2',
    'gx_echo.lv2',
    'gx_expander.lv2',
    'gx_flanger.lv2',
    'gxmetal_amp.lv2',
    'gxmetal_head.lv2',
    'gx_phaser.lv2',
    #'gxpreamp.lv2',
    #'gxpreamp.lv2/StereoAmp',
    'gx_redeye.lv2',
    'gx_reverb.lv2',
    'gx_studiopre.lv2',
    'gx_studiopre_st.lv2',
    'gxtilttone.lv2',
    'gx_tremolo.lv2',
    'gxts9.lv2',
    'gxtubedelay.lv2',
    'gxtubetremelo.lv2',
    'gxtubevibrato.lv2',
    'gxtuner.lv2',
    'gx_zita_rev1.lv2',
    'gx_mbdistortion.lv2',
    'gx_mbdelay.lv2',
    'gx_mbecho.lv2',
    'gx_mbcompressor.lv2',
    'gx_fuzz.lv2',
    'gx_graphiceq.lv2',
    'gx_duck_delay.lv2',
    'gx_duck_delay_st.lv2',
    'gx_barkgraphiceq.lv2',
    'gx_detune.lv2',
    'gx_shimmizita.lv2',
    'gx_switched_tremolo.lv2',
    'gx_room_simulator.lv2',
    'gx_digital_delay.lv2',
    'gx_digital_delay_st.lv2',
    'gx_livelooper.lv2',
    'gx_mbreverb.lv2',
    'gx_jcm800pre.lv2',
    'gx_jcm800pre_st.lv2',
    'gx_cabinet.lv2',
    #'gx_vibe.lv2',
    'gx_fuzzface.lv2',
    'gx_fuzzfacefm.lv2',
    'gx_hfb.lv2',
    'gx_hornet.lv2',
    'gx_muff.lv2',
    'gx_scream.lv2',
    'gx_susta.lv2',
    'gx_cstb.lv2',
    'gx_fumaster.lv2',
    'gx_oc_2.lv2',
    'gx_mole.lv2',
    'gx_rangem.lv2',
    'gx_hogsfoot.lv2',
    'gx_colwah.lv2',
    'gx_gcb_95.lv2',
    'gx_aclipper.lv2',
    'gx_bmp.lv2',
    'gx_bossds1.lv2',
    'gx_mxrdist.lv2',
    'gxtape.lv2',
    'gxtape_st.lv2',
    'gx_alembic.lv2',
    'gx_w20.lv2',
]

def configure(conf):
    if not conf.env.NOSSE:
        conf.env.SSE2 = append_sse_flags()

    for x in subdirs:
        conf.recurse(x);
    conf.recurse('xputty/resources')
    conf.recurse('xputty');

def build(bld):
    if not bld.env.LV2:
        return

    bld.recurse('faust')
    bld.recurse('xputty/resources')
    bld.add_group()
    bld.recurse('xputty')
    bld.add_group()
    bld.objects(name='GX_CONVOLVER',
                source='DSP/gx_convolver.cc',
                includes='DSP',
                cxxflags=['-fvisibility=hidden','-fPIC','-Wl,--exclude-libs,ALL'],
                use=['ZITA_CONVOLVER','GX_RESAMPLER'],
    )
    bld.objects(name='GX_RESAMPLER',
                source='DSP/gx_resampler.cc',
                cxxflags=['-fvisibility=hidden','-fPIC','-Wl,--exclude-libs,ALL'],
                use=['ZITA_RESAMPLER'],
    )

    lib_ext = '.so'
    if bld.env.MODGUI:
        sub_list = [('@LIB_EXT@', lib_ext)]
    else:
        sub_list = [('@LIB_EXT@', lib_ext),
                    (',\n        <modgui.ttl> ', ' '),
                    (', <modgui.ttl>', ' '),
                    (', <modguis.ttl>', ' '),
                    ]
    for x in subdirs:
        bld(rule     = g_module.sub_file,
            source   = '%s/manifest.ttl.in' % x,
            target   = '%s/manifest.ttl' % x,
            sub_list = sub_list,
            install_path = '${LV2DIR}/%s' % x,
        )
    bld.recurse(subdirs)

   # if bld.env['LV2GUI']:
   #     gx_ampstyledir = bld.path.find_dir( 'GUI' )
   #     bld.install_files(bld.env.GX_LV2_STYLE_DIR, gx_ampstyledir.ant_glob('**/*.png'),cwd=gx_ampstyledir, chmod=0o644, relative_trick=True)
   #     bld.install_files(bld.env.GX_LV2_STYLE_DIR, gx_ampstyledir.ant_glob('**/*.rc'),cwd=gx_ampstyledir, chmod=0o644, relative_trick=True)
