import platform
import re
import os.path

APPNAME = "jes"
VERSION = "0.0.1"

def options(opt):
    opt.load("compiler_c compiler_cxx")

def configure(conf):
    conf.load("compiler_c compiler_cxx")
    conf.check(header_name = "getopt.h", global_define = False)
    conf.check_cfg(package = "freetype2", uselib_store = "FreeType2", args = "--cflags --libs")
    if platform.system() == "Linux":
        conf.check(header_name = "X11/Xlib.h", global_define = False)
    elif platform.system() == "Windows":
        conf.check(header_name = "windows.h", global_define = False)

def build(bld):
    defines = ['APPNAME="%s"' % APPNAME]
    defines += ['VERSION="%s"' % VERSION]
    includes = [p for p in bld.path.ant_glob("src/**", dir = True) if os.path.isdir(p.abspath())]
    includes += ["libs/glcxx/include"]
    libs = []
    if platform.system() == "Linux":
        defines += ["PLATFORM_LINUX"]
        defines += ["JTK_X"]
        libs += ["dl", "GL", "X11"]
    elif platform.system() == "Windows":
        defines += ["PLATFORM_WINDOWS"]
        defines += ["JTK_WINDOWS"]
        libs += ["opengl32"]
    defines += ['GLCXX_GL_INCLUDE="gl3w.h"']
    sources = bld.path.ant_glob(["src/**/*.cc", "src/**/*.c", "libs/glcxx/src/glcxx/*"])
    bld(features = "c cprogram cxx cxxprogram",
        target = APPNAME,
        source = sources,
        includes = includes,
        defines = defines,
        cxxflags = ["-Wall", "-std=gnu++14", "-O2", "-Wno-switch"],
        lib = libs,
        uselib = ["FreeType2"])

    test_libs = libs + []
    if platform.system() == "Linux":
        test_libs += ["pthread"]
    elif platform.system() == "Windows":
        pass
    import sys
    test_sources = bld.path.ant_glob(["src/*/**/*.cc", "src/*/**/*.c", "libs/glcxx/src/glcxx/*"])
    test_sources += bld.path.ant_glob("test/src/**/*.cc")
    test_sources += ["libs/googletest/src/gtest-all.cc"]
    test_includes = includes + ["libs/googletest/include", "libs/googletest"]
    test_defines = defines + ["ENABLE_TESTING"]
    bld(features = "cxx cxxprogram",
        target = "tests",
        source = test_sources,
        includes = test_includes,
        defines = test_defines,
        lib = test_libs,
        cxxflags = ["-Wall", "-std=gnu++14", "--coverage", "-Wno-switch", "-include", "iostream"],
        linkflags = ["--coverage"],
        uselib = ["FreeType2"])
