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

import Options
import Utils
import misc


def set_options(opt):
    opt.add_option('--with-glade', action='store_true',
                   dest='glade', default=False,
                   help='Installs the Glade catalog for the ' \
                        'desktop-agnostic GTK widgets.')


def configure(conf):
    conf.env['GLADE_SUPPORT'] = Options.options.glade
    if conf.env['GLADE_SUPPORT']:
        conf.check_cfg(package='gladeui-1.0', uselib_store='GLADEUI',
                       mandatory=True, args='--cflags --libs')
        pkgconfig = 'pkg-config --variable catalogdir gladeui-1.0'
        conf.env['GLADEUI_CATALOGDIR'] = \
                Utils.cmd_output(pkgconfig, silent=1).strip()


def build(bld):
    pc = bld.new_task_gen('subst')
    pc.source = 'desktop-agnostic.pc.in'
    pc.target = 'desktop-agnostic.pc'
    pc.dict = {
        'API_VERSION': bld.env['API_VERSION'],
        'LIBDIR': bld.env['LIBDIR'],
        'VERSION': bld.env['VERSION'],
        'prefix': bld.env['PREFIX'],
        'datarootdir': bld.env['DATADIR'],
        }
    pc.fun = misc.subst_func
    pc.install_path = '${LIBDIR}/pkgconfig'

    ini = bld.new_task_gen('subst')
    ini.source = 'desktop-agnostic.ini.in'
    ini.target = 'desktop-agnostic.ini'
    ini.dict = {
        'CONFIG_BACKEND': bld.env['BACKENDS_CFG'][0],
        'VFS_BACKEND': bld.env['BACKENDS_VFS'][0],
        'DESKTOP_ENTRY_BACKEND': bld.env['BACKENDS_DE'][0],
    }
    ini.fun = misc.subst_func
    ini.install_path = '${SYSCONFDIR}/xdg/libdesktop-agnostic'

    if bld.env['GLADE_SUPPORT']:
        bld.install_files('${GLADEUI_CATALOGDIR}', 'desktop-agnostic.xml')
