import glob
import os
import sys
platform = sys.platform[:3]

env = Environment()

if platform == 'lin':
    env.Append(CPPFLAGS = "-g")

    env.ParseConfig("pkg-config --cflags --libs ftgl OIS OGRE")

    env.Append(CPPPATH = env.Split('''
        #
        ext/tinyxml
        /usr/include/python2.6 
        '''))
        
    env.Append(LIBS = env.Split('''
        boost_filesystem-mt
        SDL
        SDL_image
        GL
        GLU
        python2.6
        boost_python-mt-py26
        '''))
        
    env.Append(LIBPATH = env.Split('''
        /usr/local/lib
        '''))

elif platform == 'win':
    env.Append(CPPFLAGS = "/EHsc /MDd")
    
    env.Append(CPPPATH = [
        "#",
        "ext/tinyxml",
        "ext/win/include",
        "c:/boost/include/boost-1_41",
        "c:/python25/include",
        ])

    env.Append(LIBS = env.Split('''
        SDL
        SDL_image
        python25
        ftgl
        freetype2311
        opengl
        glu
        glut32
        '''))
        
    env.Append(LIBPATH = [
        "ext/win/lib",
        "c:/boost/lib",
        "c:/python25/libs"
        ])

    env.Install("#", [
        env.Glob("ext/win/lib/*.dll"),
        "c:/boost/lib/boost_python-vc80-mt-gd-1_41.dll"
        ])

def enumerateFiles(path = "."):
    files = glob.glob("%s/*" % path)
    return sum(map(enumerateFiles,filter(os.path.isdir,files)),filter(os.path.isfile,files))

files = enumerateFiles()

src = filter(lambda file: file.endswith(".cpp"),files)
inc = filter(lambda file: file.endswith(".h"),files)

mainSrc = filter(lambda file: 'main' in file.lower(), src)
libSrc = filter(lambda file: file not in mainSrc, src)


if platform == "lin":
    lib = env.SharedLibrary('fhe',libSrc)
    apps = map(lambda file: env.Program(file[:file.rfind('.')].lstrip('./').replace('/','.'), [file,lib]), mainSrc)
else:
    projects = []
    for file in mainSrc:
        name = file[:file.rfind('.')].lstrip('.\\').replace('\\','_')
        program = env.Program(name,[file,libSrc])
        projects.append(env.MSVSProject(target = name + env['MSVSPROJECTSUFFIX'],
                                        srcs = [file] + libSrc,
                                        incs = inc,
                                        buildtarget = program,
                                        variant = 'Debug',
                                        auto_build_solution = 0))
    sln = env.MSVSSolution(target = 'fhe' + env['MSVSSOLUTIONSUFFIX'],
                      projects = projects,
                      variant = 'Debug')
    env.Default([sln,projects])
