## -*- mode: python -*-
##  $Id$
##
##  Flexlay - A Generic 2D Game Editor
##  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
##
##  This program is free software; you can redistribute it and/or
##  modify it under the terms of the GNU General Public License
##  as published by the Free Software Foundation; either version 2
##  of the License, or (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

import os
import re
import glob

Import('clanLib_env')

opts = Variables('custom.py')
opts.Add('CXX', 'The C++ compiler.', 'g++')
opts.Add('CXXFLAGS', 'The C++ compiler flags.', ["-g", "-O2", "-Wall", "-fPIC"])
opts.Add('CPPPATH', 'additional header path', '')
opts.Add('LIBPATH', 'additional library path', '')

opts.Add('DESTDIR', 'destination directory which gets appended to PREFIX', '')
opts.Add('PREFIX',  'PREFIX, ie. /usr/local', '/usr/local')

env = Environment(SHLIBPREFIX='',
                  ENV = os.environ,
                  options=opts)
Help(opts.GenerateHelpText(env))

def gen_i_depends(path, filename):
    print(os.getcwd())
    lst = []
    regex = re.compile("^%include +\"(.*)\".*")
    f = file(filename)
    for i in f.readlines():
        i = i[:-1]
        if regex.match(i):
            lst.append(path + "/" + regex.sub("\\1", i))
    f.close()
    return lst

#Depends('../lib/flexlay_wrap.i', gen_i_depends(Dir("../lib").abspath, File("../lib/flexlay_wrap.i").abspath))
#env.Command('flexlay_ruby_wrap.cpp', '../lib/flexlay_wrap.i',
#            "swig -c++ -ruby -o $TARGET $SOURCE")

env.ParseConfig("pkg-config --cflags --libs ruby-2.7")
libflexlay_ruby_env = env.Clone()
libflexlay_ruby_env.Append(CXXFLAGS=["-std=c++11", "-Wno-uninitialized", "-Wno-unused-variable", "-Wno-unused-but-set-variable"])
flexlay_ruby_lib = libflexlay_ruby_env.SharedLibrary(
    target = "flexlay_wrap.so",
    source = [
        "../lib/flexlay_wrap.i",
        "ruby_functor.cpp",
        "ruby_meta_data.cpp",
        "ruby_sexpr_parser.cpp",
        "ruby_object.cpp",
        "ruby_converter.cpp"
    ],
    CPPPATH=[".",'$USER_CPPPATH', '../lib/'] + clanLib_env['CPPPATH'] + env['CPPPATH'],
    LINKFLAGS = clanLib_env['LINKFLAGS'],
    LIBPATH=['../lib', '.'] + clanLib_env['LIBPATH'],
    LIBS=['flexlay'] + clanLib_env['LIBS'],
    SWIGFLAGS="-ruby -c++")

# install_ruby = env.Alias('install_headers', Install(env['DESTDIR'] + env['PREFIX'] + '/include/flexlay', flexlay_headers))
# env.Alias('install', [install_ruby])

# EOF #
