#!/usr/bin/env python
# encoding: utf8

import os.path

import Utils

import Task
from TaskGen import feature
from Tools.ccroot import get_target_name

env = bld.env
sub = Utils.subst_vars

objects = 'backend editor main xiphos_html'
objects += ' %s' % 'webkit'

# GTKHTML
gtkhtml = 'GTKHTML'
if env['HAVE_GTKHTML_EDITOR']:
    gtkhtml += ' %s' % 'GTKHTML_EDITOR'

# on WIN32 compile Xiphos icon into executable
winresource = ''
staticlib = []
if env['IS_WIN32']:
    winresource = '../../win32/res/res.rc'
    x11_include = '../../win32/include'
    # staticlib = 'sword'.split()
else:
    x11_include = ' '

# DATADIR direct in config.h - conflict in win32 (mingw)
defines = sub('''
PREFIX="${PREFIX}"
SYSCONFDIR="${SYSCONFDIR}"
DATADIR="${DATAROOTDIR}"
LIBDIR="${LIBDIR}"
SHARE_DIR="${DATAROOTDIR}/${PACKAGE}"
''', env)


# generating locale.c

loc = bld.new_task_gen(
    source = ['make-locale-set.sh', '../../po/LINGUAS'],
    target = 'locale_set.c',
    rule = 'sh ${SRC[0].abspath()} ${SRC[1].abspath()} > ${TGT}',
    )

lib = bld.new_task_gen(
    features = 'cxx cc cprogram',
    includes = '. ../.. ../../src ../../src/editor %s %s' % (bld.env['GECKO_INCLUDE'], x11_include), # ../../ for config.h
    defines = defines,
    source = '%s' % winresource,
    target = 'xiphos',
    #uselib = 'GNOMEUI GECKONSPR MAGICK GECKONSS GTK GLADE GLIB GMODULEEXP UPRINT SWORD  %s %s' % (gtkhtml, gecko),
    #uselib = 'WSOCK GNOMEUI GECKONSPR MAGICK GECKONSS GTK GLADE GLIB GMODULEEXP UPRINT SWORD  %s %s' % (gtkhtml, gecko),
    uselib= bld.env['ALL_LIBS'],
    staticlib = staticlib, # like cflag -lsword
    add_objects = objects, # used instead of libbackend.a, libgecko.a, libeditor.a, libmain.a
    install_path= '${BINDIR}',
    strip = bld.env['TO_STRIP']
)

lib.allnodes.append(bld.path.find_or_declare('locale_set.c'))

# DBUS generate binding and marshallers
if env['HAVE_DBUS']:
    lib.add_dbus_file('ipc-interface.xml', 'ipc_object', 'glib-server')
    lib.add_marshal_file('marshal.list', 'ipc_marshal')
    lib.find_sources_in_dirs('.', excludes=['marshal.c'])
else:
    lib.find_sources_in_dirs('.', excludes=['ipc.c', 'marshal.c'])


@feature('cc')
def process_strip(self):
	if getattr(self, 'strip', None):
		tsk = self.create_task('strip')
		tsk.set_inputs(self.path.find_or_declare(get_target_name(self)))
		tsk.set_outputs(self.path.find_or_declare(get_target_name(self)))

Task.simple_task_type('strip', "${STRIP} ${SRC}", color='BLUE', after='cxx_link')
