#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import SCons
import shutil

from build import util

Import('build')
Import('mixxxminimal_depends')

mixxxminimal_sources = [
	"base/Pitch.cpp",
	"dsp/chromagram/Chromagram.cpp",
	"dsp/chromagram/ConstantQ.cpp",
	"dsp/chromagram/CQprecalc.cpp",
	"dsp/keydetection/GetKeyMode.cpp",
	"dsp/onsets/DetectionFunction.cpp",
	"dsp/onsets/PeakPicking.cpp",
	"dsp/phasevocoder/PhaseVocoder.cpp",
	"dsp/rateconversion/Decimator.cpp",
	"dsp/signalconditioning/DFProcess.cpp",
	"dsp/signalconditioning/Filter.cpp",
	"dsp/signalconditioning/FiltFilt.cpp",
	"dsp/signalconditioning/Framer.cpp",
	"dsp/tempotracking/DownBeat.cpp",
	"dsp/tempotracking/TempoTrack.cpp",
	"dsp/tempotracking/TempoTrackV2.cpp",
	"dsp/tonal/ChangeDetectionFunction.cpp",
	"dsp/tonal/TCSgram.cpp",
	"dsp/tonal/TonalEstimator.cpp",
	"dsp/transforms/FFT.cpp",
	"ext/kissfft/kiss_fft.c",
	"ext/kissfft/kiss_fftr.c",
	"maths/Correlation.cpp",
	"maths/KLDivergence.cpp",
	"maths/MathUtilities.cpp",
	"plugins/BarBeatTrack.cpp",
	"plugins/KeyDetect.cpp",
	"plugins/BeatTrack.cpp",
	"plugins/TonalChangeDetect.cpp",
	"plugins/MixxxBpmDetection.cpp",
	"libmain.cpp",
]

#Tell SCons to build the plugin
#=========================
if int(build.flags['vamp']):
    env = build.env.Clone()
    conf = Configure(env, custom_tests = { 'CheckForPKGConfig' : util.CheckForPKGConfig,
                                       'CheckForPKG' : util.CheckForPKG })

    # If there is no system vamp-sdk is installed or if the version
    # of the installed vamp-sdk is less than the bundled version,
    # then we'll directly link the bundled vamp-sdk.
    if not conf.CheckLib('vamp-sdk') or not conf.CheckForPKG('vamp-plugin-sdk', '2.7.1'):
        INTERNAL_VAMP_PATH = '#lib/vamp'
        # For header includes
        env.Append(CPPPATH=[INTERNAL_VAMP_PATH])
        sdk_src_path = '%s/src/vamp-sdk' % INTERNAL_VAMP_PATH
        mixxxminimal_sources.extend(path % sdk_src_path for path in
                                    ['%s/PluginAdapter.cpp',
                                     '%s/RealTime.cpp'])

    for depend in mixxxminimal_depends:
        depend = depend()
        # Hack to get rid of EngineBufferScaleST since the SoundTouch dependence
        # includes this.
        depend_source = [x for x in depend.sources(build) if 'enginebufferscalest' not in x]
        mixxxminimal_sources.extend(depend_source)

    env = conf.Finish()
    results = []
    if build.platform_is_linux:
        env["LINKFLAGS"].append("-Wl,--version-script=vamp-plugins/vamp-plugin.map")
    if build.platform_is_windows:
        env["LINKFLAGS"].remove("/entry:mainCRTStartup")
        env["LINKFLAGS"].append("/EXPORT:vampGetPluginDescriptor")
        #this will reduce DLL dependencies; no need to depend on MSVCRT.dll, etc
        #env["LINKFLAGS"].remove("/nodefaultlib:LIBCMT.lib")
        #env["LINKFLAGS"].remove("/nodefaultlib:LIBCMTd.lib")
        #env["LINKFLAGS"].append("/NODEFAULTLIB:MSVCRT.lib")
        #env["LINKFLAGS"].append("/DEFAULTLIB:LIBCMT.lib")
        #env["LINKFLAGS"].append("/DEFAULTLIB:LIBCPMT.lib")
        mixxxminimal_bin = env.SharedLibrary('libmixxxminimal', mixxxminimal_sources)
        results.append("mixxxminimal_bin")
        if build.bundle_pdbs:
            mixxxminimal_pdb = env.SideEffect('libmixxxminimal.pdb', mixxxminimal_bin)
            results.append("mixxxminimal_pdb")

    else:
        mixxxminimal_bin = env.SharedLibrary('mixxxminimal', mixxxminimal_sources)
        results.append("mixxxminimal_bin")
    #Pass this library object file back to the SConscript above us.
    Return(results)
else:
    Return("")
