import os
import sys
package='yafray'
version='0.0.6'
config_file='config.h'


def test_lib(lib):
	f=open('dummy.cc','w')
	f.write("int main() { return 0 }\n");
	f.close()
	if Program(target='test',source='dummy.cc',LIBS=[lib]) :
		os.remove('dummy.cc');
		return 1;
	else:
		os.remove('dummy.cc');
		return 0;


have_jpeg=1
have_pthread=1
have_zlib=1
prefix=ARGUMENTS.get('prefix','/usr/local')
debug=ARGUMENTS.get('debug',0)

lib_path=prefix+"/lib"
plugin_path=prefix+"/lib/yafray"
bin_path=prefix+"/bin"
conf_path=prefix+"/etc"
print "Stuff will be installed under:"
print prefix
print bin_path
print lib_path
print plugin_path
print conf_path

yafray_namespace='yafray'
double_coords=0
if double_coords :
	min_raydist="0.0000000000001"
else:
	min_raydist="0.000005"
	

if os.path.exists(config_file):
	print "Using config file: "+config_file
else:
	#print "\nChecking libraries ..."
	#print "Checking for jpeg ...";
	#have_jpeg=test_lib('jpeg')
	#if have_jpeg :
	#	print "YES"
	#else:
	#	print "NO"
	print "Creating config file:"+config_file
	config=open(config_file,'w')
	config.write("//Config file header created by scons\n\n")
	config.write("#ifndef __CONFIG_H\n")
	config.write("#define __CONFIG_H\n")
	config.write("#define HAVE_JPEG %d\n"%(have_jpeg))
	config.write("#define HAVE_PTHREAD %d\n"%(have_pthread))
	config.write("#define HAVE_ZLIB %d\n"%(have_zlib))
	config.write("#define LIBPATH \"%s\"\n"%(lib_path))
	config.write("#define MIN_RAYDIST %s\n"%(min_raydist))
	config.write("#define PACKAGE \"%s\"\n"%(package))
	config.write("#define VERSION \"%s\"\n"%(version))
	config.write("\n")
	config.write("#define __BEGIN_YAFRAY namespace "+yafray_namespace+" {\n");
	config.write("#define __END_YAFRAY }\n")
	config.write("\n")
	config.write("__BEGIN_YAFRAY\n");
	config.write("typedef float CFLOAT;\n");
	config.write("typedef float GFLOAT;\n");
	if double_coords:
		config.write("typedef double PFLOAT;\n");
	else:
		config.write("typedef float PFLOAT;\n");
	config.write("__END_YAFRAY\n");
	config.write("#endif\n");
	config.close()

cxxflags='-O3 -ffast-math -Wall -DHAVE_CONFIG_H -D_PTHREADS'
if debug:
	cxxflags+=' -ggdb'
else:
	cxxflags+=' -fomit-frame-pointer'


if sys.platform == 'darwin':
	sharedlib_env=Environment(SHLINKFLAGS='-dynamic -dynamiclib',SHLIBSUFFIX='.dylib');
	plugin_env=Environment(SHLINKFLAGS='-Wall -bundle');
	program_env=Environment();
elif sys.platform == 'cygwin':
	sharedlib_env=Environment(SHLINKFLAGS='-shared -Wl,--enable-auto-import');
	plugin_env=Environment(SHLINKFLAGS='-shared -Wl,--export-all -Wl,--enable-auto-import');
	program_env=Environment();
	cxxflags='-O3 -fomit-frame-pointer -ffast-math -Wall -DHAVE_CONFIG_H -D_PTHREADS -DPIC'
elif sys.platform == 'sunos5':
	sharedlib_env=Environment(CXX='g++')
	plugin_env=Environment(CXX='g++')
	program_env=Environment(CXX='g++')
else:
	sharedlib_env=Environment()
	plugin_env=Environment()
	program_env=Environment()

if sys.platform == 'cygwin':
	dynload_lib=''
else:
	dynload_lib='dl'


if sys.platform == 'sunos5':
	threads_lib= [ 'pthread' , 'rt' ]
else:
	threads_lib= [ 'pthread' ]


#sharedlib_env.SourceSignatures('timestamp')
#plugin_env.SourceSignatures('timestamp')
#program_env.SourceSignatures('timestamp')


Export('cxxflags')
Export('prefix')
Export('bin_path')
Export('lib_path')
Export('plugin_path')
Export('conf_path')
Export('sharedlib_env')
Export('plugin_env')
Export('program_env')
Export('dynload_lib')
Export('threads_lib')

SConscript(['src/SConscript'])

Alias('install',['install_core',
								 'install_lights',
								 'install_shaders',
								 'install_backgs',
								 'install_interface',
								 'install_loader'])

