# vi: syntax=python
import glob

"""
Runs this as:

scons -f utils/SConstruct [editor=1] [server=1]

from within the wesnoth toplevel directory. It expects to have all kinds of
windows dependencies in the parent directory, like this:

../wesnoth/(you are here)
../win-deps/...

and it will create/use a build directory in the parent folder as well:

../build/...

For details see the cross-compiling instructions on the Wiki.

"""
editor = ARGUMENTS.get("editor", "")
wesnothd = ARGUMENTS.get("server", "")

#sources = glob.glob("src/*.cpp") +\
#          glob.glob("src/widgets/*.cpp") +\
#          glob.glob("src/serialization/*.cpp") +\
#          glob.glob("src/sdl_ttf/*.c")
#sources.remove("src/loadscreen_empty.cpp")
#sources.remove("src/animated_editor.cpp")

def parse_automake(filename, variable):
    """Crude helper function to parse an automake file."""
    sources = []
    am = file(filename).read()
    x = am.find(variable)
    while 1:
        done = False
        while am[x] != '\\':
            if am[x] == '\n':
                done = True
                break
            x += 1
        if done: break
        x += 1
        while am[x].isspace(): x += 1
        name = ""
        while not am[x].isspace():
            name += am[x]
            x += 1
        sources.append("src/" + name)
    return sources

# read the sources from the automake file
sources = parse_automake("src/Makefile.am", "libwesnoth_core_a_SOURCES")
sources += parse_automake("src/Makefile.am", "wesnoth_SOURCES")
# there shouldn't be doubles, but well, let's remove them :)
sources = list(set(sources))

# read editor sources from automake file
editor_sources = parse_automake("src/Makefile.am", "libwesnoth_core_a_SOURCES")
editor_sources += parse_automake("src/Makefile.am", "wesnoth_editor_SOURCES")
editor_sources = list(set(editor_sources))

# read the server sources from the automake file
wesnothd_sources = parse_automake("src/Makefile.am", "libwesnoth_core_a_SOURCES")
wesnothd_sources += parse_automake("src/Makefile.am", "wesnothd_SOURCES")
wesnothd_sources = list(set(wesnothd_sources))

env = Environment()

# The cross compiler to use
env["CC"] = "i586-mingw32msvc-gcc"
env["CXX"] = "i586-mingw32msvc-g++"

# Dependencies: SDL, SDL_net, SDL_mixer, SDL_image, freetype, libintl, python
# This is where I put the dependency headers
env.Append(CPPPATH = ["../win-deps/include"])

# This is where I put the python headers.
env.Append(CPPPATH = ["../win-deps/include/python24"])

# This is where I put the dependency libs
env.Append(LIBPATH = ["../win-deps/lib"])

# Compilation settings
env.Append(CCFLAGS = ["-O2", "-mthreads", "-DHAVE_PYTHON"])
env.Append(LINKFLAGS = ["-s", "-mwindows", "-lmingwthrd"])
env.Append(CPPPATH = ["src", "src/widgets"])
env.Append(LIBS = ["mingw32", "SDLmain", "SDL", "SDL_net", "SDL_mixer", "SDL_image",
                   "intl", "wsock32", "freetype", "python24", "boost-iostreams", "z"])

# Scons stuff
env.BuildDir("../build", "src")
env.SConsignFile("sconsign")

# Windows stuff
env["PROGSUFFIX"] = ".exe"

# Compile it!
env.Program("wesnoth", ["../build/" + x[4:] for x in sources])
if editor:
	env.Program("wesnoth_editor", ["../build/" + x[4:] for x in editor_sources])
if wesnothd:
	env.Program("wesnothd", ["../build/" + x[4:] for x in wesnothd_sources])

