import os

Import('env')

# 
# generate gstmarshal
#
marshal_h = '#gst/gstmarshal.h'
marshal_c = '#gst/gstmarshal.c'
marshal_list = 'gstmarshal.list'

env.Command(marshal_h, marshal_list, [
	'glib-genmarshal --header --prefix=gst_marshal $SOURCE > $TARGET',
])
env.Command(marshal_c, marshal_list, [
	'echo #include "gst_private.h" > $TARGET',
	'echo #include "glib-object.h" >> $TARGET',
	'echo #include "gstmarshal.h" >> $TARGET',
	'glib-genmarshal --body --prefix=gst_marshal $SOURCE >> $TARGET',
])

#
# copy config header
#
config_h = '#gst/config.h'
gstconfig_h = '#gst/gstconfig.h'
common_config_h = '#$COMMON_PATH/config.h'
common_gstconfig_h = '#$COMMON_PATH/gstconfig.h'

env.Command(config_h, common_config_h, [
	Copy('$TARGET', '$SOURCE')
])
env.Command(gstconfig_h, common_gstconfig_h, [
	Copy('$TARGET', '$SOURCE')
])

#
# copy gstenumtypes.h
#
enumtypes_h = '#gst/gstenumtypes.h'
enumtypes_c = '#gst/gstenumtypes.c'
common_enumtypes_h = '#$COMMON_PATH/gstenumtypes.h'
common_enumtypes_c = '#$COMMON_PATH/gstenumtypes.c'

env.Command(enumtypes_h, common_enumtypes_h, [
	Copy('$TARGET', '$SOURCE')
])
env.Command(enumtypes_c, common_enumtypes_c, [
	Copy('$TARGET', '$SOURCE')
])

#
# copy gstversion.h
#
version_h = '#gst/gstversion.h'
common_version_h = '#$COMMON_PATH/gstversion.h'

env.Command(version_h, common_version_h, [
	Copy('$TARGET', '$SOURCE')
])

#
# Alias
#

env.Alias('prepare', [marshal_h, marshal_c, config_h, gstconfig_h, enumtypes_h, enumtypes_c, version_h])

#
# create source list, and libraries list
#
sourceList = [marshal_c, enumtypes_c]
sourceList += Split("""
	gst.c
	gstobject.c
	gstbin.c
	gstbuffer.c
	gstbus.c
	gstcaps.c
	gstchildproxy.c
	gstclock.c
	gstdebugutils.c
	gstelement.c
	gstelementfactory.c
	gsterror.c
	gstevent.c
	gstfilter.c
	gstformat.c
	gstghostpad.c
	gstindex.c 
	gstindexfactory.c 
	gstinfo.c
	gstinterface.c
	gstiterator.c
	gstmessage.c
	gstminiobject.c
	gstpad.c
	gstpadtemplate.c
	gstparamspecs.c
	gstpipeline.c
	gstplugin.c
	gstpluginfeature.c
	gstpoll.c
	gstpreset.c             
	gstquark.c
	gstquery.c
	gstregistry.c
	gstsegment.c
	gststructure.c
	gstsystemclock.c
	gsttaglist.c
	gsttagsetter.c
	gsttask.c
	gsttypefind.c
	gsttypefindfactory.c
	gsturi.c
	gstutils.c
	gstvalue.c
	gstparse.c
""")

librariesList = [
	env['GLIB_LIB'],
	env['GOBJECT_LIB'],
	env['GTHREAD_LIB'],
	env['GMODULE_LIB'],
	env['XML_LIB'],
	env['WS2_32_LIB'],
]

if not env['GST_DISABLE_LOADSAVE']:
	sourceList += ['gstxml.c']
	
if not env['GST_DISABLE_REGISTRY']:
	if env['USE_BINARY_REGISTRY']:
		sourceList += ['gstregistrybinary.c']
	else:
		sourceList += ['gstregistryxml.c']

if not env['GST_DISABLE_TRACE']:
	sourceList += ['gsttrace.c']
	
if not env['GST_DISABLE_PLUGIN']:
	sourceList += ['gstplugin.c']

if not env['GST_DISABLE_PARSE']:
	grammar_c, lex_parse_yy = SConscript(['parse/SConscript'], exports='env')
	sourceList += ['gstparse.c', grammar_c, lex_parse_yy]
	
#
# create targets
#

# What? You said this is not platform independed?
# Yes, it is, I have no choice, I have no idea how to merge two path such as "abc;def" and ['ijk']
# For example, I put it as ["abc;def", 'ijk] , SCons do not spilt "abc;def" into ['abc', 'def'] automatically
# So what appear in option for compiler will be "/Iabc;def", msvc seems can't reconize that
# This is my first time use SCons, I have no idea is this behavior correct.
# And is there any better way to solve this problem, if somebody knows how to do, just fix it. Thanks.
includePaths = os.environ.get('INCLUDE', '').split(';')

release = env.SharedLibrary(
	target='gstreamer', 
	source=sourceList, 
	LIBS=librariesList,
	LIBPATH=os.environ.get('LIB', ''),
	CPPPATH=includePaths + env['CPPPATH'],
)

env.Depends(release, 'prepare') 

# set release as default target
env.Default(release)

env.InstallAs('#$OUT_DIR/gstreamer.dll', release)
env.Alias('install', '#$OUT_DIR')