################ maybe needed fixme recheck, os is surely needed
import os
import sys

################ global config values
version = '0.0.19'

################ base paths (needed for non-unix OS)
sdlbase = 'd:\\sdl-devel\\SDL'
sdlimagebase = 'd:\\sdl-devel\\SDL_image'
sdlnetbase = 'd:\\sdl-devel\\SDL_net'
sdlmixerbase = 'd:\\sdl-devel\\SDL_mixer'
fftw3base = 'd:\\fftw3'

################ change base of dirs (unix OS)
installbindir = '/usr/local/bin'
installdatadir = '/usr/local/share/dangerdeep'
if ARGUMENTS.get('installbindir', 0):
	installbindir = ARGUMENTS.get('installbindir', 0)
if ARGUMENTS.get('installdatadir', 0):
	installdatadir = ARGUMENTS.get('installdatadir', 0)


################ set environment
if sys.platform == 'win32':
	print "Compiling for Win32 Environment"
	env = Environment(ENV = os.environ, tools = ['mingw'])
	env.Append(CPPPATH = [sdlbase + '\\include\\SDL', sdlimagebase + '\\include', sdlnetbase + '\\include', sdlmixerbase + '\\include', fftw3base])
	libpath = [sdlbase + '\\lib', sdlimagebase + '\\lib', sdlnetbase + '\\lib', sdlmixerbase + '\\lib', fftw3base]
	gllibs = ['opengl32', 'glu32']
	sdllibs = ['SDL', 'SDL_image']
	env.Append(CCFLAGS = '-Wall -O2 -D_REENTRANT') # for mingw!
	datadir = './data'
else:
	print "Compiling for Unix/Posix/Linux Environment"
	env = Environment(ENV = os.environ)
	env.Append(CPPPATH = ['/usr/include/SDL'])
	libpath = []
	gllibs = ['GL', 'GLU']
	sdllibs = ['SDL', 'SDL_image']
	env.Append(CCFLAGS = '-Wall -g -O2 `sdl-config --cflags`')
	datadir = installdatadir

###### optionally change install and data dirs
if ARGUMENTS.get('datadir', 0):
	datadir = ARGUMENTS.get('datadir', 0)

print 'Install binary path: ' + installbindir
print 'Install data path: ' + installdatadir
print 'Using data dir: ' + datadir

build_base_dir = 'build'
source_base_dir = 'src'

env.Append(CPPDEFINES = [('VERSION', '\\"' + version + '\\"')])

################ configure
conf = Configure(env)
if not conf.CheckCHeader('GL/gl.h'):
	print 'GL library must be installed!'
	Exit(1)
if not conf.CheckCHeader('GL/glu.h'):
	print 'GLU library must be installed!'
	Exit(1)
if not conf.CheckCHeader('SDL.h'):
	print 'SDL library must be installed!'
	Exit(1)
if not conf.CheckCHeader('SDL_image.h'):
	print 'SDL_image library must be installed!'
	Exit(1)
if not conf.CheckCHeader('SDL_net.h'):
	print 'SDL_net library is needed for network code!'
	Exit(1)
if not conf.CheckCHeader('SDL_mixer.h'):
	print 'SDL_mixer library is needed for sound support!'
	Exit(1)
if not conf.CheckCHeader('fftw3.h'):
	print 'fftw3 library must be installed!'
	Exit(1)
env = conf.Finish()

######################### source file lists
gfxlibs = ['oglext'] + gllibs + sdllibs
alllibs = ['dftdmedia', 'tinyxml'] + gfxlibs + ['SDL_mixer', 'SDL_net', 'fftw3']

################ helper functions
# taken from bombyx build system
def SelectBuildDir(build_dir, platform=None):
	# if no platform is specified, then default to sys.platform
	if not(platform):
		platform = sys.platform

	print "Looking for build directory for platform '%s'" % platform

	# setup where we start looking at first
	test_dir = build_dir + os.sep + platform
	default_dir = build_dir + os.sep + 'default'

	# we look for a directory named exactly after the
	# platform so that very specific builds can be done
	if os.path.exists(test_dir):
		# make sure it is a directory
		target_dir = test_dir
	else:
		print "Exact match not found, finding closest guess"
		
		# looks like there isn't an exact match
		# find the closest matching directory
		dirs = os.listdir(build_dir)
		found_match = 0
		for dir in dirs:
			if platform.find(dir) != -1:
				# found a match (hopefully the right one)
				target_dir = build_dir + os.sep + dir
				found_match = 1
				break
		if not(found_match):
			print "No match found, looking for 'default' directory"
			# looks like this platform isn't available
			# try the default target
			if os.path.exists(default_dir):
				target_dir = default_dir
			else:
				# bad, nothing is available, tell the user
				print "No build directories found for your platform '%s'" % platform
				return None
			
	print "Found directory %s, will build there" % target_dir
	return target_dir


def all_entries_that_start_with(list, prefix):
	list2 = []
	for i in list:
		if i.startswith(prefix):
			list2 += [i]
	return list2


################ build
selected_build_dir = SelectBuildDir(build_base_dir)
target_dir = '#' + selected_build_dir
libpath = libpath + [target_dir]

f = open('files.txt')
allcvsfilesr = f.readlines()
# remove return code '\n' from end of line
allcvsfiles = []
for i in allcvsfilesr:
	allcvsfiles += [i[:len(i)-1]]

datafiles = all_entries_that_start_with(allcvsfiles, './data')

Export('env', 'gfxlibs', 'alllibs', 'libpath', 'installbindir', 'installdatadir', 'datadir', 'datafiles')

SConscript(source_base_dir + os.sep + 'SConscript', build_dir = target_dir, duplicate = 0)

BuildDir(target_dir, source_base_dir, duplicate=0)

############### option so that "scons tarball" builds a tarball of source and data
#fixme: change that so that e.g. "scons tarball" does that only, and it
#isn't done everytime we build the program

allcvsfiles2 = ''
for i in allcvsfiles:
	allcvsfiles2 += i + ' '

#env.Append(TARFLAGS = '-z ' + problematicfiless)
#env.Append(TARSUFFIX = '.gz')
#tarball = env.Alias('tarball', env.Tar('dangerdeep-'+version, filestopack))
#### we have an own tar version here...
if not(sys.platform == 'win32'):
	env.Alias('tarball', env.Command('dangerdeep-' + version + '.tar.gz', '', ['tar -zcf ' + 'dangerdeep-' + version + '.tar.gz ' + allcvsfiles2]))

#also env.Zip is possible...

############# eof
