#
# Copyright (c) 2006-2007, Reid Nichol
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#     * Redistributions of source code must retain the above
#       copyright notice, this list of conditions and the following
#       disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials
#       provided with the distribution.
#     * Neither the name of Fireflylabs.ca nor the names of its
#       contributors may be used to endorse or promote products
#       derived from this software without specific prior written
#       permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

import os, sys

# get where we are
platform = sys.platform

# SUPPORTED PLATFORMS
#
supported_platforms = ['darwin', 'openbsd4']

if platform not in supported_platforms:
	print "Platform '" + platform + "' not supported"
	Exit(1)


# now that we know we belong here, we build the env, etc
if platform == 'win32':
	env = Environment(ENV=os.environ)
else:
	env = Environment()

# for win32 we prefer mingw
if platform == 'win32':
	Tool('mingw')(env)


# SECTION: OPTIONS
#
# platform should NEVER be used outside this section
options = Options()

# our build type.  When adding another platform, it must be
# added in the list.
options.Add('build_type', 'Type of build to perform - defaults to platform (macosx|darwin|openbsd4)', platform)

# setup the install location
#
# darwin:  /opt/local
# macosx:  /Applications
# openbsd: /usr/local
# win32:   C:\\
options.Add('prefix', 'Location to install')


options.Update(env)
#Help(helpText(options, env))


# SECTION: VAR CREATION

# start with an empty sourcefile list
source_files = []
# an empty libpath
libpath      = []
# an empty libs list
libs         = []
# an empty frameworks list
frameworks   = []
# an empty cpppath
cpppath      = []
# an empty cflags
cflags       = []
# an empty ldflags
ldflags      = []


# for macosx
if env['build_type'] == 'macosx':
	frameworkpathbase = '/System/Library/Frameworks'
	cflags.append(['-D_MACOSX'])
elif env['build_type'] == 'darwin':
	# we use the OSX system Python as MacPorts uses site.py
	# from that as well.
	frameworkpathbase = '/System/Library/Frameworks'
	cflags.append(['-D_DARWIN'])


# SECTION: SOURCE
source_files.append(['main.c'])

# the DB files
commonDir = os.curdir + os.sep + os.pardir + os.sep + os.pardir + os.sep + "common"
DBDir     = commonDir + os.sep + "dbapi"

source_files.append([DBDir + os.sep + "db_common.c"])

# the DB includes
cpppath.append([DBDir])

# for everyone we want
cflags.append(['-Wall', '-O2'])


# SECTION: SETUP LIB/INCLUDE/FRAMEWORKS/etc

# construct our general include/lib dir
if env['build_type'] == 'darwin':
	libpath.append(['/opt/local/lib'])
	cpppath.append(['/opt/local/include'])
	# /opt/local doesn't have this, but it's isolated
	# so it should be fine
	cpppath.append(['/usr/include/malloc'])

elif env['build_type'] == 'macosx':
	# because macosx has both frameworks and libs
	# we need this as well
	libpath.append(['/usr/lib'])
	libpath.append(['/usr/local/lib'])
	cpppath.append(['/usr/include'])
	cpppath.append(['/usr/local/include'])
	cpppath.append(['/usr/include/malloc'])

elif env['build_type'] == 'openbsd4':
	libpath.append(['/usr/lib'])
	libpath.append(['/usr/local/lib'])
	cpppath.append(['/usr/local/include'])
	cpppath.append(['/usr/include'])

#elif env['build_type'] == 'win32':
#	# ???
#	continue

# now we add the directories where the DB's reside
# NOTE: this should be put into a function avalible
#       to all.
#env.AppendUnique(CCFLAGS = [])


if env['build_type'] == 'macosx':
	# we build a universal binary for OSX
	cflags.append(['-isysroot/Developer/SDKs/MacOSX10.4u.sdk'])
	ldflags.append(['-isysroot/Developer/SDKs/MacOSX10.4u.sdk'])
	cflags.append(['-arch', 'i386', '-arch', 'ppc'])
	ldflags.append(['-arch', 'i386', '-arch', 'ppc'])

# SECTION: LIBS
env.AppendUnique(LIBS = ['sqlite3', 'ncurses'])


# SECTION: SET VARS
env.AppendUnique(LIBPATH    = libpath)
env.AppendUnique(CPPPATH    = cpppath)
env.AppendUnique(LIBS       = libs)
env.AppendUnique(FRAMEWORKS = frameworks)
env.AppendUnique(CCFLAGS    = cflags)
env.AppendUnique(LINKFLAGS  = ldflags)


# SECTION: BUILD

# right now we're always just making a program.  Will be change
# later for Mac OSX Bundles.
env.Program('mapEdit', source_files)
