require "bundler"
Bundler.setup(:default)

require "rake/clean"
require "rscons"

NAME = "jes"
GTEST_VERSION = "1.7.0"

base_env = Rscons::Environment.new do |env|
  env.build_root = "build"
  env["CCFLAGS"] += ["-Wall", "-O2"]
  env["CFLAGS"] << "-std=gnu99"
  env["CXXFLAGS"] << "-std=gnu++11"
  env.parse_flags!("!sdl2-config --cflags --libs")
  env.parse_flags!("!freetype-config --cflags --libs")
  env.parse_flags!("!pkg-config --cflags --libs ruby-1.9")
  env["LIBS"] += ["dl"]
  if RUBY_PLATFORM =~ /mingw/
    env["LIBS"] += ["opengl32"]
  else
    env["LIBS"] += ["GL"]
  end
end

task :default do
  base_env.clone(clone: :all) do |env|
    env.Program("bin/jes", Dir["src/**/*.{cc,c}"])
  end
end

task :test do
  base_env.clone(clone: :all) do |env|
    env.build_root = "build/test"
    env["CPPPATH"] << "src"
    env["CPPPATH"] << "gtest-#{GTEST_VERSION}/include"
    env["CPPPATH"] << "gtest-#{GTEST_VERSION}"
    sources = Dir["gtest-#{GTEST_VERSION}/src/gtest-all.cc",
                  "gtest-#{GTEST_VERSION}/src/gtest_main.cc",
                  "src/**/*.{cc,c}",
                  "test/src/**/*.cc"] - ["src/main.cc"]
    if RUBY_PLATFORM =~ /linux/
      env["LIBS"] << "pthread"
    end
    env.Program("build/tests", sources)
  end
  system("./build/tests")
end

task :all => :default

task :clean do
  Rscons.clean
end
