# scons script for building kdissert (top-level directory)
# Thomas Nagy, 2004 <tnagy2^8@yahoo.fr>

import os

env = Environment(TARGS=COMMAND_LINE_TARGETS, ARGS=ARGUMENTS, tools=['default', 'generic', 'kde'], toolpath=['./'])

#env['HOME'] = os.environ['HOME']
# TODO : remove
env.AppendUnique( ENV = os.environ )

## The target make dist requires the python module shutil which is in 2.3
env.EnsurePythonVersion(2, 3)

## Bksys requires scons 0.96
env.EnsureSConsVersion(0, 96)

Export( "env" )

# Cache directory
env.CacheDir('cache')

# Avoid spreading .sconsign files everywhere
env.SConsignFile('signatures')

### we are not using rcs or sccs, avoid scanning
### especially if on network file systems
paths = [ '.', './src', './src/templates', './src/kdissert', './src/kdissert/datastruct',
    './src/kdissert/canvasview', './src/kdissert/treelistview',
    './src/kdissert/generator', './src/kdissert/gui', './src/kdissert/shell' ]
for dir in paths:
    env.SourceCode( dir, None )

### another assume foo.moc is always included foo.cpp files (make sure no file is forgetted)
env['NOMOCSCAN'] = True

#KDEaddpaths(paths, env)

### important, the name of our app
env['APPNAME'] = 'kdissert'

### important, export the environment
Export("env")

### subdirectories
SConscript("src/kdissert/SConscript")
SConscript("src/templates/SConscript")
SConscript("doc/SConscript")
SConscript("po/SConscript")
SConscript("src/appdata/SConscript")
SConscript("src/pics/SConscript")

### to make a tarball of your masterpiece
if 'dist' in COMMAND_LINE_TARGETS:

    APPNAME = 'kdissert'
    VERSION = os.popen("cat VERSION").read().rstrip()
    FOLDER  = APPNAME+'-'+VERSION
    ARCHIVE = FOLDER+'.tar.bz2'

    def tarball(env, target, source):
        """
        Function called to make a tarball of the sources
        Make sure to have python 2.3 for the shutil module
        """
        import shutil
        import glob

        ## check if the temporary directory already exists
        if os.path.isdir(FOLDER):
           shutil.rmtree(FOLDER)

        ## create a temporary directory
        startdir = os.getcwd()
        shutil.copytree(startdir, FOLDER)

        ## remove the unnecessary files
        os.popen("find "+FOLDER+" -name \"{arch}\" | xargs rm -rf")
        os.popen("find "+FOLDER+" -name \".arch-ids\" | xargs rm -rf")
        os.popen("find "+FOLDER+" -name \".arch-inventory\" | xargs rm -f")
        os.popen("find "+FOLDER+" -name \"sconsign*\" | xargs rm -f")
        os.popen("find "+FOLDER+" -name \"*cache*\" | xargs rm -rf")
	os.popen("find "+FOLDER+" -name \"kdiss*-data\" | xargs rm -rf")
	os.popen("find "+FOLDER+" -name \"*.pyc\" | xargs rm -f")
        os.popen("rm -f "+FOLDER+"/config.py*")

        ## make the tarball
        os.popen("tar cjf "+ARCHIVE+" "+FOLDER)

        ## remove the temporary directory
        if os.path.isdir(FOLDER):
            shutil.rmtree(FOLDER)

    ## Make an alias so that 'scons dist' builds the archive
    env.Command(ARCHIVE, 'VERSION', tarball)
    env.Alias('dist', ARCHIVE )

    ## Tell scons to rebuild the archive everytime
    env.AlwaysBuild(ARCHIVE)

