# -*- python -*-
# encoding: utf-8

import os
import Common

def configure(conf):
    conf.env['ENABLE_GNOMEPRINT'] = bool(conf.check_pkg(
            'libgnomeprint-2.2 >= 2.2.0 pygobject-2.0 ',
            destvar='GNOMEPRINT', mandatory=False))

    conf.env['ENABLE_GNOMEPRINTUI'] = bool(conf.check_pkg(
            'libgnomeprintui-2.2 >= 2.2.0 pygobject-2.0 ',
            destvar='GNOMEPRINTUI', mandatory=False))

def codegen(bld, module):
    cmd = bld.create_obj('command-output')
    cmd.command = 'pygtk-codegen-2.0'
    cmd.command_is_external = True
    cmd.prio = 5
    cmd.argv = [
        '--py_ssize_t-clean',
        '--load-types', cmd.input_file('print-arg-types.py'),
        '--register', os.path.join(cmd.env['PYGTK_DEFSDIR'], 'pango-types.defs'),
        '--register', os.path.join(cmd.env['PYGTK_DEFSDIR'], 'gdk-types.defs'),
        '--register', os.path.join(cmd.env['PYGTK_DEFSDIR'], 'gtk-types.defs'),
        '--prefix', 'py'+module,
        '--override', cmd.input_file("%s.override" % module),
        cmd.input_file("%s.defs" % module),
        ]
    cmd.stdout = '%s.c' % module
    return cmd


def build(bld):
    
    if bld.env()['ENABLE_GNOMEPRINT']:
        codegen(bld, 'print')
        pyext = bld.create_obj('cc', 'plugin', 'pyext')
        pyext.source = 'printmodule.c print.c art-gtype.c'
        pyext.target = '_print'
        pyext.uselib = 'GNOMEPRINT'
        pyext.install_var = 'PYTHONDIR'
        pyext.install_subdir = os.path.join('gtk-2.0', 'gnomeprint')
        pyext.env.append_value('CPPPATH', '../gnomeprint') # work around possible WAF bug

    if bld.env()['ENABLE_GNOMEPRINTUI']:
        codegen(bld, 'printui')
        pyext = bld.create_obj('cc', 'plugin', 'pyext')
        pyext.source = 'printuimodule.c printui.c'
        pyext.target = 'ui'
        pyext.uselib = 'GNOMEPRINTUI'
        pyext.install_var = 'PYTHONDIR'
        pyext.install_subdir = os.path.join('gtk-2.0', 'gnomeprint')
        pyext.env.append_value('CPPPATH', '../gnomeprint') # work around possible WAF bug

    py = bld.create_obj('py')
    py.inst_dir = os.path.join('gtk-2.0', 'gnomeprint')
    py.source = "__init__.py"

    defsdir = Common.install_files('DATADIR', os.path.join('pygtk', '2.0', 'defs'),
                                   ['print.defs', 'printui.defs'])

