# 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=['./'])
if 'configure' in COMMAND_LINE_TARGETS:
        env.Exit(0)

env.AppendUnique( ENV = {'PATH' : os.environ['PATH'], 'HOME' : os.environ['HOME']} )

## 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 )

### 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'

	GREEN  ="\033[92m"
	NORMAL ="\033[0m"

	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
	print GREEN+"Writing archive "+ARCHIVE+NORMAL
	os.popen("tar cjf "+ARCHIVE+" "+FOLDER)

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

	env.Default(None)
	env.Exit(0)

