#! /usr/bin/env python
# encoding: utf-8

# wscript

VERSION='0.0.1'
APPNAME='icfp08'

srcdir = '.'
blddir = 'build'

###########################
import types

def tolist(x):
	if type(x) is types.ListType:
		return x
	return [x]

def env_mod(conf, use):
	types = [ 'LIB', 'STATICLIB', 'LIBPATH', 'CPPPATH', 'CXXDEFINES', 'CCFLAGS', 'CXXFLAGS', 'LINKFLAGS' ]
	bak = {}
	for t in types:
		bak[t] = conf.env[t]
		for u in use:
			conf.env[t] = tolist(conf.env[t]) + tolist(conf.env['%s_%s' % (t, u)])
	return bak

def env_mod_revert(conf, bak):
	for (k,v) in bak.items():
		conf.env[k] = v

def CHECK_INCLUDE_FILES(conf, header, define, uselib = '', path = None, mandatory = 0, use = []):
	envbak = env_mod(conf, use)
	hconf = conf.create_header_configurator()
	hconf.mandatory = mandatory
	hconf.name = header
	hconf.uselib_store = uselib
	hconf.define = define
	if path: hconf.path += path
	res = hconf.run()
	env_mod_revert(conf, envbak)
	return res

def PKGCONFIG(conf, name, uselib = None, define = '', version = '', mandatory = 0):
	if not uselib: uselib = name
	hconf = conf.create_pkgconfig_configurator()
	hconf.name = name
	hconf.version = version
	hconf.uselib_store = uselib
	hconf.define = define
	hconf.mandatory = mandatory
	res = hconf.run()
	return res

###########################






def set_options(opt):
	opt.tool_options('compiler_cc')
	#opt.tool_options('ragel', tooldir = '.')

def configure(conf):
	conf.check_tool('compiler_cc')
	#conf.check_tool('ragel', tooldir = '.')
	
	common_ccflags = [
		'-std=gnu99', '-Wall', '-g', '-Wshadow', '-W', '-pedantic',
		#'-O2',
#		'-fPIC', '-D_GNU_SOURCE',
		]
	conf.env['CCFLAGS'] += common_ccflags
	conf.env['LIB'] += ['m']
	
	PKGCONFIG(conf, "glib-2.0", uselib = 'glib', mandatory = 1)
	incdir = conf.env['CPPPATH_glib'][0]
	conf.env['CPPPATH_glib'] += [ incdir+'/glib-2.0/', incdir + '/glib-2.0/include/' ]
	CHECK_INCLUDE_FILES(conf, "glib.h", "HAVE_GLIB_H", uselib = 'glib', use = ['glib'], mandatory = 1)
	
	conf.write_config_header('src/config.h')


def build(bld):
	bld.add_subdirs('src')