# -*- python -*-

import Params
Params.g_autoconfig = True

VERSION = '2.22.3'
APPNAME = 'gnome-python'
srcdir = '.'
blddir = 'build'

import misc
import os
import shutil
import glob
import sys
import types

def dist_hook():
    for html_file in glob.glob(os.path.join('..', 'build', 'default', 'docs', 'gnomevfs', 'html', '*')):
        shutil.copy2(html_file, os.path.join('docs', 'gnomevfs', 'html'))
    ## Copy WAF to the distdir
    assert os.path.basename(sys.argv[0]) == 'waf'
    shutil.copy(sys.argv[0], '.')

def set_options(opt):
    opt.tool_options('compiler_cc')
    opt.tool_options('gnome')

    opt.add_option('--enable-modules',
                   help=('Enable only the specified modules.'),
                   type="string", default='all', metavar="MODULES_LIST (comma separated)",
                   dest='enable_modules')


def configure(conf):
    conf.check_tool('misc')
    conf.check_tool('compiler_cc')
    conf.check_tool('gnome')
    conf.check_tool('python')
    conf.check_python_version((2,4))
    conf.check_python_headers()
    conf.define('VERSION', VERSION)

    version = [int(s) for s in VERSION.split('.')]
    conf.define('GNOME_PYTHON_MAJOR_VERSION', version[0])
    conf.define('GNOME_PYTHON_MINOR_VERSION', version[1])
    conf.define('GNOME_PYTHON_MICRO_VERSION', version[2])

    # Define pygtk required version, for runtime check
    pygtk_version = [2, 10, 3]
    conf.define('PYGTK_REQUIRED_MAJOR_VERSION', pygtk_version[0])
    conf.define('PYGTK_REQUIRED_MINOR_VERSION', pygtk_version[1])
    conf.define('PYGTK_REQUIRED_MICRO_VERSION', pygtk_version[2])

    values = conf.check_pkg('pygtk-2.0', destvar='PYGTK', vnum='.'.join([str(x) for x in pygtk_version]),
                            pkgvars=['defsdir'], mandatory=True)
    conf.env['PYGTK_DEFSDIR'] = values['PYGTK_DEFSDIR']

    if not conf.find_program('pygobject-codegen-2.0', var='CODEGEN'):
        if not conf.find_program('pygtk-codegen-2.0', var='CODEGEN'):
            Params.fatal("Could not find pygobject/pygtk codegen")

    conf.env.append_value('CCDEFINES', 'HAVE_CONFIG_H')

    conf.env['ENABLE_MODULES'] = Params.g_options.enable_modules.split(',')
    
    conf.sub_config('bonobo')
    conf.sub_config('gnome')
    conf.sub_config('gnomecanvas')
    conf.sub_config('gconf')
    conf.sub_config('gnomevfs')
    conf.sub_config('docs/gnomevfs')

    for module in conf.env['ENABLE_MODULES']:
        if module == 'all':
            continue
        if module not in conf.env['MODULES_AVAILABLE']:
            Params.warning("Requested module %r is not available in this source tree." % module)
    if conf.env['MODULES_TO_BUILD']:
        print "** The following modules will be built:"
        for m in conf.env['MODULES_TO_BUILD']:
            print "\t%s" % m

    not_built = list(conf.env['MODULES_AVAILABLE'])
    for mod in conf.env['MODULES_TO_BUILD']:
        not_built.remove(mod)
    if not_built:
        print "** The following modules will NOT be built:"
        for m in not_built:
            print "\t%s" % m

    conf.write_config_header('config.h')


def build(bld):
    ## cater for WAF API change between 1.3 and 1.4
    waf_version = [int (s) for s in Params.g_version.split('.')]
    if waf_version >= [1,4]:
        def create_pyext(bld):
            return bld.create_obj('cc', 'shlib', 'pyext')
    else:
        def create_pyext(bld):
            return bld.create_obj('cc', 'plugin', 'pyext')
    bld.create_pyext = types.MethodType(create_pyext, bld)

    ## generate and install the .pc file
    obj=bld.create_obj('subst')
    obj.source = 'gnome-python-2.0.pc.in'
    obj.target = 'gnome-python-2.0.pc'
    obj.dict = {
        'VERSION': VERSION,
        'prefix': bld.env()['PREFIX'],
        'exec_prefix': bld.env()['PREFIX'],
        'libdir': bld.env()['LIBDIR'],
        'includedir': os.path.join(bld.env()['PREFIX'], 'include'),
        'datadir': bld.env()['DATADIR'],
        'datarootdir': bld.env()['DATADIR'],
        }
    obj.fun = misc.subst_func
    obj.inst_var = 'LIBDIR'
    obj.inst_dir = 'pkgconfig'

    ## subdirs
    bld.add_subdirs('bonobo')
    bld.add_subdirs('gnome')
    bld.add_subdirs('gnomecanvas')
    bld.add_subdirs('gconf')
    bld.add_subdirs('gnomevfs')
    bld.add_subdirs('docs/gnomevfs')

