import	os
import	re
import	rkConfig
import	rkBuilder
import	rkWalker


rkBuilder.setCommandLine (COMMAND_LINE_TARGETS, ARGUMENTS)

if False :


    if rkConfig.xmllint :
           check = '--check' 
    else : check = ''


    # Construct three builders:
    #
    #	docBuilder
    #		: Generates the KDE-style documentation from the
    #		: DocBook-XML sources.
    #	manBuilder
    #		: Generates the manual files from the DocBook-XML
    #		: sources.
    #   manExtract
    #		: Extracts the manual files from the manual.tgz
    #		: tarball. Used where the platform cannot build the
    #		: manual.
    #
    docBuilder = Builder \
	   (
		action = "echo docBuilder .... ; meinproc %s --cache $TARGET $SOURCE" % (check)
	   )

    manBuilder = Builder \
	   (
		action = 'echo manBuilder .... ; cd %s/doc/rekall ; ./mkman.sh'  % (rkConfig.topDir)
	   )

    manExtract = Builder \
	   (
		action = 'echo manExtract .... ; cd %s/doc/rekall ; rm -rf manual ; tar -xzf manual.tgz ; touch index.xml'  % (rkConfig.topDir)
	   )

    #  If present, meinproc will need the HOME environment variable, but
    #  this may not exist on Windows.
    #
    try    : home = os.environ['HOME']
    except : home = None

    pyEnv      = Environment \
	   (
		ENV		=
			{ 	'PATH'		: os.environ['PATH'],
				'HOME'		: home,
				'KDEDIR'	: rkConfig.prefix
			},
		BUILDERS	=
			{
				'DocBuilder'	: docBuilder,
				'ManBuilder'	: manBuilder,
				'ManExtract'	: manExtract
			}
	   )


    sources = [ 'index.docbook' ]
    for file in os.listdir('.') :
        if re.match('.*\.sgml', file) :
            sources.append (file)



    # If the KDE document processor "meinproc" is available then make
    # 'app5.sgml' from the python interface modules, and the KDE
    # manual from the set of DocBook-XML sources (including 'app5.sgml')
    #
    if rkConfig.meinproc :
        pyEnv.DocBuilder  (target = 'index.cache.bz2', source = sources)


    # If both "meinproc" is avaialble and the python XML libraries are 
    # avaialable (and sufficiently functional) then we can generate the
    # manual. The nominal target is 'index.xml' but the builder will
    # actually generate the complete "manual" directory.
    #
    if rkConfig.meinproc and rkConfig.pythonXML and not rkConfig.quickDocs :
           pyEnv.ManBuilder (target = 'index.xml',    source = sources)
    else : pyEnv.ManExtract (target = 'index.xml',    source = 'manual.tgz')


    ins  = rkBuilder.installer()
    if ins :

        rDocDir = os.path.join (rkBuilder.docDir, rkConfig.appName)

        for [dir, file] in rkWalker.rkWalker('help', '.*\.qt') :
            ins.Alias ('install', pyEnv.Install (os.path.join (rkBuilder.appDir, dir), os.path.join (dir, file)))

        if rkConfig.meinproc :

            ins.Alias ('install', pyEnv.Install (rDocDir, 'index.cache.bz2'))
            ins.Alias ('install', pyEnv.Install (rDocDir, 'index.docbook'  ))

            for [dir, file] in rkWalker.rkWalker('.', '.*\.png') :
                if dir[0: 6] == "./chap" :
                    ins.Alias ('install', pyEnv.Install (os.path.join (rDocDir, dir), os.path.join (dir, file)))
            for [dir, file] in rkWalker.rkWalker('.', '.*\.sgml') :
                if dir[0:12] == "./properties" :
                    ins.Alias ('install', pyEnv.Install (os.path.join (rDocDir, dir), os.path.join (dir, file)))

        for [dir, file] in rkWalker.rkWalker('manual', '.*\.xml') :
            ins.Alias ('install', pyEnv.Install (os.path.join (rkBuilder.appDir, dir), os.path.join (dir, file)))
        for [dir, file] in rkWalker.rkWalker('manual', '.*\.html') :
            ins.Alias ('install', pyEnv.Install (os.path.join (rkBuilder.appDir, dir), os.path.join (dir, file)))
        for [dir, file] in rkWalker.rkWalker('manual', '.*\.png') :
            ins.Alias ('install', pyEnv.Install (os.path.join (rkBuilder.appDir, dir), os.path.join (dir, file)))

        try    : os.unlink  (os.path.join (rDocDir, 'common'))
        except : pass
