#! /usr/bin/env python
# encoding: utf-8
import re
import os

# these variables are mandatory ('/' are converted automatically)
srcdir = '.'
blddir = 'build'

def init():
	pass


def set_options(opt):
	opt.sub_options('src')
	opt.tool_options('compiler_cxx')
	

def configure(conf):
	
	conf.check_tool('compiler_cxx')	
	conf.check_tool('g++')	
        conf.env['CXXFLAGS']='-g -O2 -Wall'


	conf.check_tool(['boost'])
	
        # put string tables into C file
        writeStringTable()

	conf.check_cfg(atleast_pkgconfig_version='0.0.0')
	conf.check_cfg(package='gtk+-2.0', args='--cflags --libs', uselib_store='GTK', atleast_version='2.6.0')
	conf.check_cfg(package='gtkextra-2.0', args='--cflags --libs', uselib_store='GTKEXTRA',atleast_version='2.0')


	#conf.check_cxx(lib='atlas',uselib_store='ATLAS')
	#conf.check_cxx(lib='ptcblas', uselib='ATLAS', uselib_store='CBLAS')

	conf.check_cxx(lib='lapack',uselib_store='LAPACK',mandatory=1)
	conf.check_cxx(lib='dl',uselib_store='DL',mandatory=1)
	conf.check_cxx(lib='boost_filesystem',uselib_store='BOOST',mandatory=1)
	conf.check_cxx(lib='boost_regex',uselib_store='BOOST',mandatory=1)
	conf.check_cxx(lib='boost_serialization',uselib_store='BOOST',mandatory=1)
	conf.check_cxx(lib='boost_thread-mt',uselib_store='BOOST',mandatory=1)
	conf.check_cxx(lib='boost_program_options',uselib_store='BOOST',mandatory=1)
	conf.check_cxx(lib='boost_system',uselib_store='BOOST',mandatory=1)
        # X11
	conf.check_cxx(lib='Xtst',uselib_store='GTK',mandatory=1)
	conf.check_cxx(lib='X11',uselib_store='GTK',mandatory=1)
	conf.sub_config('src')

	return

def build(bld):
	bld.add_subdirs('src')
	bld.install_files(os.getenv("HOME")+"/.cebl/conf/","conf/channels-19.conf")
	bld.install_files(os.getenv("HOME")+"/.cebl/conf/","conf/string-table-en.txt")
	bld.install_files(os.getenv("HOME")+"/.cebl/conf/","conf/gui-string-table-en.txt")
        print "Don't forget to run './waf install --plugins' to install the plugins."
	
def install():
        pass
	
def run():
	os.system("build/default/src/cebl")

def writeStringTable():
        table_names = ["string-table-en","gui-string-table-en"]
        header_file = open("src/CompiledStrings.hpp","w")
        c_file = open("src/CompiledStrings.cpp","w")

        header_file.write("#ifndef COMPILEDSTRINGS_H\n")
        header_file.write("#define COMPILEDSTRINGS_H\n")
        c_file.write("#include \"CompiledStrings.hpp\"\n")
        newline_str = "STR_TABLE_LINEBREAK";

        header_file.write("extern const char * str_table_linebreak_str;\n")
        c_file.write("const char * str_table_linebreak_str = \""+newline_str+"\";\n")


        for table_name in table_names:
                str_table_lines = open("conf/"+table_name+".txt").read().split("\n");
                str_table = ""
                for str in str_table_lines:
                        str_table += str + newline_str;
                str_table = str_table.replace('"','\\"');
                str_table = str_table.replace("\\n","\\\\n");
                # wrte it to the string table header
                header_file.write("extern const char * " +
                                  table_name.replace("-","_")+";\n");
                c_file.write("const char * " +
                             table_name.replace("-","_") +
                             " = \"" +str_table + "\";\n");
        header_file.write("#endif")
        header_file.close();
        c_file.close();
