import	rkBuilder
import	rkWalker
import	rkConfig
import	os

rkBuilder.setCommandLine (COMMAND_LINE_TARGETS, ARGUMENTS)


def build_dictionaries (target, source, env) :

    import string
    out = open(str(target[0]), 'w')
    out.write ("<?xml version = '1.0' ?>\n")
    out.write ("<classes>")
    for s in source :
        for l in open(str(s)).readlines() :
            if string.find(l, '<?xml') < 0 :
                out.write (l)
    out.write ("</classes>")
    return None



def build_documentation (target, source, env) :

    out = open(str(target[0]), 'w')
    out.write ('<?xml version = \'1.0\' ?>\n')
    out.write ('<!DOCTYPE label [\n')
    out.write ('    <!ENTITY rekall "<emphasis>Rekall</emphasis>">\n')
    out.write (']>\n')
    out.write ('<classes>')

    for s in [ \
		'kb_basic.man',
		'kb_pyvalue.man',
		'kb_pyobject.man',
		'kb_pybutton.man',
		'kb_pylabel.man',
		'kb_pyitem.man',
		'kb_pyblock.man',
		'kb_pyformblock.man',
		'kb_pyreportblock.man',
		'kb_pyform.man',
		'kb_pyfield.man',
		'kb_pychoice.man',
		'kb_pycheck.man',
		'kb_pylistbox.man',
		'kb_pylinktree.man',
		'kb_pylink.man',
		'kb_pyframer.man',
		'kb_pycontainer.man',
		'kb_pytabber.man',
		'kb_pytabberpage.man'
	    ] :

        import string
        for l in open('script/python/doc/' + str(s)).readlines() :
            if string.find(l, '<?xml') < 0 :
                out.write (l)
    out.write ("</classes>")
    return None



def build_methods (target, source, env) :

     import string
     text = open(str(source[0])).read()
     text = string.replace (text, '<emphasis>',  '&lt;i&gt;' )
     text = string.replace (text, '</emphasis>', '&lt;/i&gt;')
     text = string.replace (text, '<emphasis/>', '&lt;i/&gt;')
     text = string.replace (text, '<para>',  '&lt;p&gt;' )
     text = string.replace (text, '</para>', '&lt;/p&gt;')
     text = string.replace (text, '<para/>', '&lt;p/&gt;')
     open(str(target[0]), 'w').write(text)

#  ------------------------------------------------------------------

docBuilder = Builder \
	     (
		action = build_documentation, suffix = '.xml',  src_suffix = '.man'
	     )
dictBuilder= Builder \
	     (
		action = build_dictionaries,  suffix = '.xml',  src_suffix = '.dict'
	     )
methBuilder= Builder \
	     (
		action = build_methods,       suffix = '.dict', src_suffix = '.xml'
	     )

if rkConfig.pythonBuild :

    pyEnv  = Environment \
	     (
		BUILDERS =
			 {
				'Documentation' : docBuilder,
				'Dictionaries'	: dictBuilder,
				'Methods'	: methBuilder
			 }
	     )

    doc	   = pyEnv.Documentation ('doc.xml',	  source = rkBuilder.listAllMatching('kb_.*\.man' ))
    dict   = pyEnv.Dictionaries  ('dict.xml',	  source = rkBuilder.listAllMatching('kb_.*\.dict'))
    meth   = pyEnv.Methods       ('methods.dict', source = 'doc.xml')

    ins	   = rkBuilder.installer()
    env	   = Environment ()

    if ins :
        rkWalker.install (ins, env, '.', 'dict', 'kb.*\.dict')
        ins.Alias ('install', env.Install (rkBuilder.appDir + '/dict/py', meth))
        ins.Alias ('install', env.Install (rkBuilder.appDir + '/dict/py', 'time.dict'))
