import thirdparty
import os
import SCons

def buildOSG(context, sourceDir):
    os.chdir(sourceDir)
    if not os.path.exists('build'):
        os.mkdir('build')
    os.chdir('build')
    if not 'CMAKE' in context.sconf.env.Dictionary():
	return
    cmake = context.sconf.env['CMAKE']
    make = context.sconf.env['MAKE']
    os.spawnl(os.P_WAIT, cmake, cmake, '..', '-DCMAKE_INSTALL_PREFIX=' + context.sconf.env['THIRDPARTY_STAGEDIR'])
    os.spawnl(os.P_WAIT, make, make, 'all')
    os.spawnl(os.P_WAIT, make, make, 'install')

def osgConfig(context):
    return thirdparty.TryCompileAndLink(context, 'Open Scene Graph', ['osg/Node'], 'OSG_', '.cc',
                                        libraries=['osg', 'OpenThreads'])
    
def checkOSG(context):
    return thirdparty.configureDependency(context,
                                          osgConfig,
                                          downloadSource=('http://interreality.org/~tetron/',
                                                          'OpenSceneGraph-2.0.zip',
                                                          'OpenSceneGraph',
                                                          4871475,
                                                          '9e8d8311868f2acce377a6d7d69c26c2'),
                                          buildFunc=buildOSG)

import scripts
scripts.AddCustomTest('CheckOpenSceneGraph', checkOSG)
