# create build environment and options
env = Environment()
opts = Options()

# OPTION debug
opts.Add(BoolOption('debug', 'Set to yes (or 1) to build for debug', 'no'))
# OPTION strip
opts.Add(BoolOption('strip', 'Set to no (or 0) to avoid stripping binaries', 'yes'))
# OPTION warning
opts.Add(BoolOption('warnings', 'Set to yes (or 1) to print all warnings', 'yes'))
# OPTION with_opengl
opts.Add(BoolOption('with_opengl', 'Set to no (or 0) if you do not want to build OpenGL version of Mars, Land of No Mercy', 'yes'))

env = Environment(options = opts)


# OPTION no_mingw
if env['PLATFORM'] == 'cygwin':
	opts.Add(BoolOption('no_mingw', 'Set to yes (or 1) to build without mingw', 'no'))
	env = Environment(options = opts)

# PLATFORM mingw under cygwin
if env['PLATFORM'] == 'cygwin' and not int(env['no_mingw']):
	# add mingw specific options
	opts.AddOptions(
		PathOption('mingw_cygdir', 'where your mingw installation is located under cygwin', '/mingw'),
		PathOption('mingw_windir', 'where your mingw installation is located under cygwin', 'F:/Programmi/Dev-Cpp')
    )
	env = Environment(options = opts)

	mingw_cygdir = env['mingw_cygdir']
	mingw_windir = env['mingw_windir']

	env.Replace(CXX = [mingw_cygdir + '/bin/g++'])
	env.Replace(LINK = [mingw_cygdir + '/bin/g++'])
	env.Append(CXXFLAGS = ['-I"' + mingw_windir + '/include/SDL"'])
	env.Append(CXXFLAGS = ['-O2', '-D_REENTRANT', '-mwindows'])
	env.Append(LIBS = ['mingw32', 'SDLmain', 'SDL'])
	env.Append(LINKFLAGS = ['-mwindows'])

# PLATFORM posix or pure cygwin
else:
	# determine compiler and linker flags for SDL
	env.ParseConfig('sdl-config --cflags')
	env.ParseConfig('sdl-config --libs')
	# add additional compiler flags
	if not int(env['debug']):
		env.Append(CXXFLAGS = ['-O2'])


# generate help for options
Help(opts.GenerateHelpText(env))

# build for debug
if int(env['debug']):
	env.Append(CXXFLAGS = ['-g'])
	# of course no stripping if debug
	env['strip'] = 0

# strip binaries
if int(env['strip']):
	env.Append(LINKFLAGS = ['-s'])

# print all warnings
if int(env['warnings']):
	env.Append(CXXFLAGS = ['-Wall'])

# check for libraries and headers (if not cleaning)
if not env.GetOption('clean'):
	print ":: Checking for libs"
	conf = Configure(env)
	if not conf.CheckLibWithHeader('libSDL', 'SDL.h', 'c', 'SDL_Init(SDL_INIT_VIDEO);', autoadd = 0):
		print 'Did not find libSDL, exiting!'
		Exit(1)
	if not conf.CheckLibWithHeader('libSDL_image', 'SDL_image.h', 'c', 'IMG_GetError();'):
		print 'Did not find libSDL_image, exiting!'
		Exit(1)
	if not conf.CheckLibWithHeader('libSDL_ttf', 'SDL_ttf.h', 'c', 'TTF_Init();'):
		print 'Did not find libSDL_ttf, exiting!'
		Exit(1)
	if int(env['with_opengl']):
		if not conf.CheckLibWithHeader('libGL', 'GL/gl.h', 'c', 'glLoadIdentity();'):
			print 'Did not find libGL, Mars is going to be compiled without GL support!'
			env['with_opengl'] = 0
		else:
			env.Append(CXXFLAGS = ['-DWITH_OPENGL'])
	env = conf.Finish()

#env.Append(CXXFLAGS = ['-DWIN32'])


# print compile/clean message
if not env.GetOption('clean'):
	if env['PLATFORM'] == 'cygwin' and not int(env['no_mingw']):
		print ":: Compiling for mingw under cygwin"
	elif env['PLATFORM'] == 'cygwin':
		print ":: Compiling for cygwin"
	else:
		print ":: Compiling for posix"
else:
	print ":: Cleaning"


# export variables and run sub scripts
Export('env', 'opts')
SConscript('src/SConscript', build_dir='build', duplicate=0)

