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

import Options, types, sys, Runner
from time import gmtime, strftime, timezone

# the following two variables are used by the target "waf dist"
VERSION='1.0'
APPNAME='spawn-fcgi'

# these variables are mandatory ('/' are converted automatically)
srcdir = '.'
blddir = 'build'

def set_options(opt):
	opt.tool_options('compiler_cc')
	opt.add_option('--without-limits', action='store_false', help='without /etc/security/limits.conf support', dest = 'limits', default = True)

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

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 configure(conf):
	opts = Options.options

	conf.check_tool('compiler_cc')

	if opts.limits:
		conf.define("USE_LIMITS", 1)
	conf.env['USE_LIMITS'] = opts.limits

	conf.define("PACKAGE_NAME", APPNAME)
	conf.define("PACKAGE_VERSION", VERSION)
	conf.define("PACKAGE_BUILD_DATE", strftime("%b %d %Y %H:%M:%S UTC", gmtime()));

	common_ccflags = [
		'-std=gnu99', '-Wall', '-g', '-Wshadow', '-W', '-pedantic',
		'-fPIC',
		'-DHAVE_CONFIG_H', '-D_GNU_SOURCE',
		]
	conf.env['CCFLAGS_spawnfcgi'] += common_ccflags

	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('config.h')

main_source = '''
	spawn-fcgi.c
'''

limits_source = '''
	pam_limits.c
'''

def build(bld):
	env = bld.env

	spawnfcgi = bld.new_task_gen('cc', 'program')
	spawnfcgi.name = 'spawn-fcgi'
	spawnfcgi.source = main_source
	if env['USE_LIMITS']:
		spawnfcgi.source += limits_source
	spawnfcgi.target = 'spawn-fcgi'
	spawnfcgi.uselib += 'glib spawnfcgi'
	spawnfcgi.includes = '.'
