import thirdparty
import os
import SCons
import subprocess

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']

    cmakecall = [cmake, 
                 '..', 
                 '-DCMAKE_INSTALL_PREFIX=' + context.sconf.env['THIRDPARTY_STAGEDIR'], 
                 '-DBUILD_OSG_WRAPPERS=1'
                 ]

    if context.sconf.env['PLATFORM'] == 'darwin':
        cmakecall.append('-DOSG_GLU_TESS_CALLBACK_TRIPLEDOT=1')

    subprocess.call(cmakecall)
    subprocess.call([make, make, 'all'])
    subprocess.call([make, make, 'install'])

def osgConfig(context):
    return thirdparty.TryCompileAndLink(context, 'Open Scene Graph', 'OSG_', '.cc',
                                        headers=['osg/Node'],
                                        libraries=['osg', 'OpenThreads'])
    
def checkOSG(context):
    return thirdparty.configureDependency(context,
                                          'osg',
                                          osgConfig,
                                          downloadSource=('http://interreality.org/static/software/current/',
                                                          'OpenSceneGraph-2.6.1.zip',
                                                          'OpenSceneGraph-2.6.1',
                                                          5849143,
                                                          '0955d92af64e48d2bdda464103962f92'),
                                          buildFunc=buildOSG)

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