import os.path

Import('env', 'version')

if not env['mingw']:
	# If the tiff include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['tiff_include_path'] == "":
		env.Replace(tiff_include_path = env.Dir('#/../win32libs/libtiff/include'))
	if env['tiff_lib_path'] == "":
		env.Replace(tiff_lib_path = env.Dir('#/../win32libs/libtiff/lib/vc6'))

	if env['debug']:
		env.Replace(tiff_lib = 'libtiff_d')
	else:
		env.Replace(tiff_lib = 'libtiff')

	# If the zlib include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['zlib_include_path'] == "":
		env.Replace(zlib_include_path = env.Dir('#/../win32libs/zlib/include'))
	if env['zlib_lib_path'] == "":
		env.Replace(zlib_lib_path = env.Dir('#/../win32libs/zlib/lib/vc6'))

	if env['debug']:
		env.Replace(z_lib = 'zlibstat_d')
	else:
		env.Replace(z_lib = 'zlibstat')

	# If the jpeg include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['jpeg_include_path'] == "":
		env.Replace(jpeg_include_path = env.Dir('#/../win32libs/jpeg/include'))
	if env['jpeg_lib_path'] == "":
		env.Replace(jpeg_lib_path = env.Dir('#/../win32libs/jpeg/lib/vc6'))

	if env['debug']:
		env.Replace(jpeg_lib = 'libjpeg_d')
	else:
		env.Replace(jpeg_lib = 'libjpeg')

	if env['debug']:
		env.AppendUnique(CCFLAGS=['/GX', '/MDd', '/GR', '/Zm200', '/Od', '/Zi'])
		env.AppendUnique(CPPDEFINES=[('_DEBUG', 1)])
		env.AppendUnique(LINKFLAGS=['/DEBUG'])
	else:
		env.AppendUnique(CCFLAGS=['/GX', '/MD', '/GR', '/Zm200', '/O2'])

	env.AppendUnique(CPPDEFINES=['NO_SYSLOG', 'WIN32', 'PLUGINS', 'USE_WIN32_FILEIO'])

	env.Replace(fltk_lib = 'fltk')
	env.Replace(exr_lib = 'Iex')
	env.Replace(exr2tif_libs = ['IlmImf', 'Half', 'Iex', 'Imath'])
	env.Replace(display_libs = ['ws2_32', 'user32', 'gdi32', 'ole32', 'shell32', 'comctl32', '$fltk_lib'])
	env.AppendUnique(CPPPATH = ['$exr_include_path/Iex', '$exr_include_path/IlmImf', '$exr_include_path/Imath'])
else:
	#env.AppendUnique(SHLINKFLAGS=['-Wl,-enable-auto-import', '-Wl,-enable-runtime-pseudo-reloc'])
	env.AppendUnique(SHLINKFLAGS=['-Wl,-disable-auto-import'])
	env.AppendUnique(LINKFLAGS=['-Wl,-disable-auto-import'])
	if env['debug']:
		env.AppendUnique(CCFLAGS=['-g', '-O0', '-fno-common'])
	else:
		env.AppendUnique(CCFLAGS=['-O1', '-fno-common'])

	env.AppendUnique(CPPPATH = [env.Dir('#/aqsistypes/win32')])
	env.AppendUnique(CPPDEFINES=[('NDEBUG', '1'), ('PLUGINS', '1'), 'WIN32', 'NO_SYSLOG'])

	# If the tiff include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['tiff_include_path'] == "":
		env.Replace(tiff_include_path = env.Dir('#/../win32libs/libtiff/include'))
	if env['tiff_lib_path'] == "":
		env.Replace(tiff_lib_path = env.Dir('#/../win32libs/libtiff/lib/mingw'))

	env.Replace(tiff_lib = 'tiff')

	# If the zlib include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['zlib_include_path'] == "":
		env.Replace(zlib_include_path = env.Dir('#/../win32libs/zlib/include'))
	if env['zlib_lib_path'] == "":
		env.Replace(zlib_lib_path = env.Dir('#/../win32libs/zlib/lib/mingw'))

	env.Replace(z_lib = 'z')

	# If the jpeg include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['jpeg_include_path'] == "":
		env.Replace(jpeg_include_path = env.Dir('#/../win32libs/jpeg/include'))
	if env['jpeg_lib_path'] == "":
		env.Replace(jpeg_lib_path = env.Dir('#/../win32libs/jpeg/lib/mingw'))

	env.Replace(jpeg_lib = 'jpeg')

	# If the openexr include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['exr_include_path'] == "":
		env.Replace(exr_include_path = env.Dir('#/../win32libs/OpenEXR/include'))
	if env['exr_lib_path'] == "":
		env.Replace(exr_lib_path = env.Dir('#/../win32libs/OpenEXR/lib/mingw'))

	env.Replace(exr_lib = 'Iex')

	# If the fltk include path isn't specified as an option, choose the default, of the 
	# win32libs project from CVS checked out to the same folder as 'aqsis'
	if env['fltk_include_path'] == "":
		env.Replace(fltk_include_path = env.Dir('#/../win32libs/FLTK/include'))
	if env['fltk_lib_path'] == "":
		env.Replace(fltk_lib_path = env.Dir('#/../win32libs/FLTK/lib/mingw'))

	env.Replace(fltk_lib = 'fltk')

	env.Replace(exr2tif_libs = ['$tiff_lib', 'IlmImf', 'Half', 'Iex', 'Imath', 'IlmThread', '$z_lib'])
	env.Replace(exr_libs = ['IlmImf', 'Half', 'Iex', 'Imath', 'IlmThread', '$z_lib'])
	env.Replace(display_libs = ['ws2_32', 'user32', 'gdi32', 'ole32', 'shell32', 'comctl32', 'uuid', '$fltk_lib'])

	env.Replace(slparse_ccflags = ['-O0'])
	env.AppendUnique(CPPDEFINES=['NO_SYSLOG', 'WIN32', 'PLUGINS'])


# Add the GnuWin32 bin folder to the PATH, so that bison 2.1 can find m4.exe
env.AppendENVPath('PATH', env.Dir('#/../win32libs/GnuWin32/bin').abspath)
# Prepend the aqsistypes build path to the PATH so that aqsl.exe can find the .dll during building shaders.
env.PrependENVPath('PATH', env.Dir('$build_prefix/aqsistypes').abspath)
env.AppendUnique(CPPPATH = ['#/aqsistypes/win32'])

env.Replace(YACC = '${bison}')
env.Replace(LEX = '${flex}')
env.Replace(YACCFLAGS = '--no-lines -d')
env.Replace(XSLTPROC = env.File('#/../win32libs/GnuWin32/bin/xsltproc'))

env.Replace(aqsis_libs = ['ws2_32', 'user32'])
env.Replace(libaqsis_libs = ['ws2_32', 'user32'])

def PostInstallSharedLibrary(env, dir, source):
	basename = os.path.join('%s', '%s') %(dir, source[1])
	env.Command('%s' %(basename), source[1], Copy('$TARGET', '$SOURCE'))
	basename = os.path.join('%s', '%s') %(dir, source[2])
	env.Command('%s' %(basename), source[2], Copy('$TARGET', '$SOURCE'))

env.PostInstallSharedLibrary = PostInstallSharedLibrary
