# vi: syntax=python
# encoding: utf8

import os

env = Environment()

debug = ARGUMENTS.get("debug", "")
static = ARGUMENTS.get("static", "")
gl = ARGUMENTS.get("gl", "")
al = ARGUMENTS.get("al", "")

if debug:
    libname = "fudgefontd"
else:
    libname = "fudgefont"

target = static and "static" or "shared"
target += "/"
target += debug and "debug" or "release"

try: os.mkdir("build")
except OSError: pass
env.SConsignFile("build/sconsign")

CPPPATH = ["src"]
LIBS = []
CCFLAGS = ["-W", "-Wall", "-g"]

EXAMPLES = ["example1", "example2", "example3"]

if gl:
    NAME_ADD = "_gl"
    CCFLAGS.append(["-DALLEGROGL"])
    target += "gl/"
elif al:
    NAME_ADD = ""
    target += "al/"

BUILD = "build/%s" % target
env.BuildDir(BUILD, "src", duplicate = False)

#env["PLATFORM"] = "mingw"

if env["PLATFORM"] == "posix":
    if debug:
        if static:
            env.ParseConfig("allegro-config --static debug")
        else:
            env.ParseConfig("allegro-config --shared debug")
        if gl:
            LIBS.extend(["agld", "GL", "GLU"])
    else:
        if static:
            env.ParseConfig("allegro-config --static release")
        else:
            env.ParseConfig("allegro-config --shared release")
        if gl:
            LIBS.extend(["agl", "GL", "GLU"])
    env.ParseConfig("freetype-config --libs")
    if static: env.Append(CCFLAGS = ["-DALLEGRO_STATICLINK"])
    CPPPATH.append("/usr/local/include/freetype2")
    LIBDIR = "/usr/local/lib"
    INCLUDEDIR = "/usr/local/include"

    library = env.SharedLibrary(libname, BUILD + "/fudgefont.c")
else:
    print "Platform «%s» is not supported, simply mail me and I will add it." % env["PLATFORM"]
    LIBDIR = os.path.join(os.environ["MINGDIR"], "lib")
    INCLUDEDIR = os.path.join(os.environ["MINGDIR"], "include") 
    #env.Exit()

    if crosscompile:
        env["CC"] = "i586-mingw32msvc-gcc"
        env["AR"] = "i586-mingw32msvc-ar"
        env["RANLIB"] = "i586-mingw32msvc-ranlib"
        env.Append(CCFLAGS = ["-DALLEGRO_STATICLINK"])
        env.Append(CPPPATH = ["/home/elias/prog/al/land/dependencies/include"])
        library = env.StaticLibrary("fudgefont", BUILD + "/fudgefont.c")

env.Append(CCFLAGS = CCFLAGS)
env.Append(CPPPATH = CPPPATH)
env.Append(LIBS = LIBS)

if gl or al:
    examples = []
    for EXAMPLE in EXAMPLES:
        examples += env.Program(EXAMPLE + NAME_ADD, [BUILD + "/" + EXAMPLE + ".c", BUILD + "/fudgefont.c"])
else:
    examples = []


env.Install(LIBDIR, library)
env.Install(INCLUDEDIR, "src/fudgefont.h")
env.Alias("install", [LIBDIR, INCLUDEDIR])

env.Alias("all", [library] + examples)
env.Default("all")
