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

def build(bld):
    if not bld.env.STANDALONE:
        return
    import os
    doxy_opt = os.environ.get("RUN_DOXYGEN")
    if doxy_opt is None:
        return
    doxy_opt = doxy_opt.lower()
    def make_doxyfile(task):
        import re
        d = {}
        for inp in task.inputs[1:]:
            with open(inp.abspath()) as fd:
                d[inp.name] = fd.read().strip()
        d['CLANG'] = 'YES' if 'clang' in doxy_opt else 'NO'
        def sub(m):
            return d[m.group(1)]
        with open(task.inputs[0].abspath()) as ifd, \
          open(task.outputs[0].abspath(), "w") as ofd:
            ofd.write(re.sub('@{([^}]+)}', sub, ifd.read()))
    bld.env.DOXY_OPT = doxy_opt
    bld(rule = make_doxyfile,
        vars = ['DOXY_OPT'],
        source = ['Doxyfile.in','../src/gx_head/clang_options.txt',
                  '../src/gx_head/input_gx_head.txt',
                  '../libgxw/gxw/input_libgxw.txt',
                  '../libgxwmm/gxwmm/input_libgxwmm.txt',
                ],
        target = 'Doxyfile',
        )
    if "run" in doxy_opt:
        bld(rule = "doxygen ${SRC}",
            source = 'Doxyfile',
            target = '../../html/index.html',
            always = True,
        )
