import thirdparty
import os
import SCons

def buildFreeimage(context, sourceDir):
    os.chdir(sourceDir)
    make = context.sconf.env['MAKE']
    if context.sconf.env['PLATFORM'] == 'darwin':
	os.spawnl(os.P_WAIT, make, make, '-f', 'Makefile.osx', 'all')
	os.spawnl(os.P_WAIT, make, make, '-f', 'Makefile.osx', 'install', 
		  'PREFIX='+context.sconf.env['THIRDPARTY_STAGEDIR'])
    elif context.sconf.env['PLATFORM'] == 'posix':
	os.spawnl(os.P_WAIT, make, make, '-f', 'Makefile.gnu', 'all')
	os.spawnl(os.P_WAIT, make, make, '-f', 'Makefile.gnu', 'install', 
		  'PREFIX='+context.sconf.env['THIRDPARTY_STAGEDIR'])

def checkFreeimage(context):
    return thirdparty.configureDependency(context,
                                          lambda c: thirdparty.TryCompileAndLink(context,
                                                                                 'FreeImage',
                                                                                 'FREEIMAGE_',
                                                                                 '.c',
                                                                                 headers=['FreeImage.h'],
                                                                                 libraries=['freeimage']),
                                          debianPackage='libfreeimage-dev',
					  downloadSource=('http://interreality.org/~tetron/',
                                                          'FreeImage393.zip',
                                                          'FreeImage',
							  0,
							  ''),
                                          buildFunc=buildFreeimage)

import scripts
scripts.AddCustomTest('CheckFreeimage', checkFreeimage)
