# SConstruct file for Shield
#
# This is the equivalent of a Makefile, but for use with the SCons
# build system
#
# Author: Axel Liljencrantz
# Copyright: FreeCode AS
# License: GPLv3

# Construct the main environment
#
# Add the base build directory to cdpath. That way, #includes can all
# prepend the directory in which they are located, and everything will
# work just spiffy!

import glob
import os
import sys
import glob
from stat import *

env = Environment(ENV = os.environ)
Export('env')

# Add . to list of include directories
env.Append(CPPPATH=[Dir('.')])

# Respect various defaults from the environment
for name in ['CXX','CC','CFLAGS','CXXFLAGS', 'CPPFLAGS']:
	if os.getenv(name) != None:
		env[name] = os.getenv(name)

#if os.getenv('CPPFLAGS'):
#	env.Append(CPPPATH=[Dir(os.getenv('CPPFLAGS'))])


if os.getenv('LDFLAGS') != None:
	env['LINKFLAGS'] += os.getenv('LDFLAGS')


# Implementation of the 'OracleCheck' custom check
def CheckOracleInclude(context, locs):
	'''Check for a suitable oracle include directory
	'''	
	for loc in locs:
		path = os.path.join(loc,'public')
		if os.path.isdir(path):
			context.env.Append(CPPPATH=[Dir(path)])
			return True	

def CheckOracleLink(context, locs):
	'''Check for a suitable oracle link directory
	'''	
	for loc in locs:
		path = os.path.join(loc,'lib')
		if os.path.isdir(path):
			context.env.Append(LIBPATH=[Dir(path)])
			return True	
		
def CheckOracle(context):
	'''Check for presense of needed Oracle libraries. If Oracle can be found, the include and link paths are modified to incorporate oracle and True is returned. Otherwise False is returned.
	'''	
	context.Message('Checking for Oracle installation files')
	locs_wc = ['/usr/lib/oracle/xe/app/oracle/product/*/server/rdbms', '/usr/lib/oracle/xe/app/oracle/product/*/client/rdbms', '/usr/lib/oracle/xe/app/oracle/product/*/server', '/usr/lib/oracle/xe/app/oracle/product/*/client']
	locs=[]
	for loc in locs_wc:
		locs = locs+glob.glob(loc)

	result=True	
	if not CheckOracleInclude(context, locs):
		result=False
	if not CheckOracleLink(context, locs):
		result=False
	context.Result(result)
	return result

def CheckGxx(context):
	'''Check if the c++ compiler is g++. Return True if yes, false otherwise.
	'''
	context.Message('Checking if the compiler is g++')
	result = context.env['CXX'][0:3] == 'g++'
	context.Result(result)
	return result

conf = Configure(env, custom_tests = {'CheckOracle' : CheckOracle, 'CheckGxx' : CheckGxx})

#
# Check for presence of various headers
#
for header in ['getopt.h', 'sys/select.h']:
	if conf.CheckCHeader(header):
		var_name = header.upper().replace('.','/').replace('/','_')
		conf.env['CXXFLAGS']+=' -DHAVE_'+var_name+'=1'
	
#
# Check for presence of getopt.h header
#
if conf.CheckFunc('getopt_long'):
	conf.env['CXXFLAGS']+= ' -DHAVE_GETOPT_LONG=1'
	

#
# A custom Oracle check. Disabled, because there are som any places wher Oracle
# installs itself that trying to find it is nearly pointless. User will have to set
# up correct flags.
#
#if not conf.CheckOracle():
#	print 'Oracle OCCI library files are not installed!'
#	print 'They can be found in e.g. the Oracle-XE client libraries,'
#	print 'which can be downloaded from http://www.oracle.com.'
#	Exit(1)

#  Add gcc-specifig options, mostly warnings 
#
# Ideally, I'd like to add more warnings, but the oracle headers and
# the autogenerated code (felx/bison) creates huge numbers of
# warnings, making the real ones dissapear in the crowd.
#
#
# The rdynamic flag makes it possible to print slightly nicer stack
# traces on errors.

if conf.CheckGxx():
	conf.env['CXXFLAGS']+=' -pipe -Woverloaded-virtual -Wnon-virtual-dtor -Wformat -Wimplicit-int -Wimplicit-function-declaration -Wmissing-braces -Wunused -Wreturn-type -Wwrite-strings -Wall'
	conf.env['LINKFLAGS']+=' -rdynamic'


env = conf.Finish()

#
# Build the src subdirectory with a separate build dirctory called 'build'
#
SConscript('src/SConscript', build_dir='build', duplicate=0)

# Build the actual programs
SConscript('bin/SConscript')

env.Default('bin')



env = Environment(TARFLAGS = '-c -z')
env.Tar('shield.tar.gz', glob.glob('src/*.c') + glob.glob('src/*.cc')  + glob.glob('src/*.y')+ glob.glob('include/*.h') + glob.glob('include/*.hh') + glob.glob('joomla/*.php') + [ 'shield_package.sql','sysv/shield','bin/SConscript', 'SConstruct', 'build/SConscript'])

env.Install('/usr/local/bin',['bin/shield','bin/shield_multiplex'])
Alias('install', '/usr/local/bin')