import	rkConfig
import	rkBuilder
import	rkConfig
import	rkWalker
import	os
import	string

rkBuilder.setCommandLine (COMMAND_LINE_TARGETS, ARGUMENTS)

#  indir :
#	If set, only SConscript directories in the specified directory
#	are included.
#
#  only  :
#	Can be set to either 'test', 'full' or 'rt' then filters out
#	SConscript files which to not have that extension.
#
indir	= rkBuilder.argument('indir'  )
only	= rkBuilder.argument('only'   )
test	= rkBuilder.argument('test'   )
harness	= rkBuilder.argument('harness')


if only is not None :
    only = string.split (only, ',')

for [ dir, tgt, file ] in rkWalker.rkWalker('.', 'SConscript.*') :

    if string.find (dir,   '.hg') >= 0 :
        continue

    if string.find (dir, 'build') >= 0 :
        continue

    if string.find (dir, 'test' ) >= 0 :
        if test == 'no'  :
            continue
        if not rkConfig.tests :
            continue

    if string.find (dir, 'test' ) <  0 :
        if test == 'yes' :
            continue

    if string.find (dir, 'harness' ) >= 0 :
        if harness == 'no'  :
            continue
        if not rkConfig.harness :
            continue

    if string.find (dir, 'harness' ) <  0 :
        if harness == 'yes' :
            continue

    if file == 'SConscript_full' :
        if not rkConfig.full :
            continue

    if file == 'SConscript_rt'   :
        if not rkConfig.rt   :
             continue

    if dir.find('NonGPL') >= 0 :
        if not rkConfig.nongpl   :
             continue

    if only is not None :
        bits = string.split (file, '_')
        if len(bits) == 1 : bits.append ('base')
        if not bits[1] in only :
            continue

    # By default, we do not duplicate files from the source directory
    # into the build tree. Where this is necessary, the a file named
    # "Duplicate" flags that duplication should be performed.
    #
    try :
        os.stat (os.path.join(dir, 'Duplicate'))
        duplicate = 1
    except :
        duplicate = 0

    # Similarly, where the build should occur in the source tree, use a
    # file "Local". This is used where there is no actual build, such
    # the icons and picture directories.
    #
    try :
        os.stat (os.path.join(dir, 'Local'))
        local     = 1
    except :
        local     = 0

    suffix = ''
    if file == 'SConscript_rt'   :
        suffix = '_rt'

    if indir is not None and indir != dir :
        continue

    try    :
        os.stat (os.path.join(dir, 'Manual'))
        if indir is None :
            continue
    except :
        pass

    if local :
           SConscript (os.path.join(dir, file))
    else : SConscript (os.path.join(dir, file), build_dir = rkBuilder.fixSourcePath (dir + suffix), duplicate = duplicate)


SConsignFile	('scons-signatures-' + rkConfig.mode)

if rkConfig.scache is not None :
    CacheDir	(os.path.join (rkConfig.scache, rkConfig.mode))
