# -*- python -*- =======================================================
# FILE:         SConstruct
# CONTENTS:     scons build script for scved
# AUTHOR:       nesciv AT gmail DOT com
# AUTHOR:       alex AT x37v DOT info (followed the example of scvim)
# AUTHOR:       sk AT k-hornz DOT de (some of this SConstruct taken from sk's SConstruct for SuperCollider)
# ======================================================================

import os
import re
import pwd

PLATFORM = os.uname()[0].lower()

SC_FILE_RE = re.compile('.*\.sc$')

PY_FILE_RE = re.compile('.*\.py$')
LANG_FILE_RE = re.compile('.*\.lang$')
XML_FILE_RE = re.compile('.*\.xml$')
PLUGIN_FILE_RE = re.compile('.*\.gedit-plugin$')

ANY_FILE_RE = re.compile('.*')
HOME_DIR_RE = re.compile(os.environ.get('HOME') + '.*')

DEFAULT_PREFIX = '/usr/local/'
#DEFAULT_SC_HELP = os.path.join(DEFAULT_PREFIX, 'share/SuperCollider/Help/')


def pkg_data_dir(prefix, *args):
    if PLATFORM == 'darwin':
        base = '/Library/Application Support'
        if is_home_directory(prefix):
            base = os.path.join(prefix, base)
    else:
        base = os.path.join(prefix, 'share')
    return os.path.join(base, 'SuperCollider', *args)

def pkg_extension_dir(prefix, *args):
    return pkg_data_dir(prefix, 'Extensions', *args)

SWAP_FILE_MATCH = re.compile('.*.swp')

def install_dir(env, src_dir, dst_dir, filter_re, strip_levels=0):
    nodes = []
    for root, dirs, files in os.walk(src_dir):
        src_paths = []
        dst_paths = []
        if 'CVS' in dirs: dirs.remove('CVS')
        if '.svn' in dirs: dirs.remove('.svn')
        for d in dirs[:]:
            if filter_re.match(d):
                src_paths += flatten_dir(os.path.join(root, d))
                dirs.remove(d)
        for f in files:
			  #ditch swap files
            if not SWAP_FILE_MATCH.match(f) and filter_re.match(f):
                src_paths.append(os.path.join(root, f))
        dst_paths += map(
            lambda f:
            os.path.join(
            dst_dir,
            *f.split(os.path.sep)[strip_levels:]),
            src_paths)
        nodes += env.InstallAs(dst_paths, src_paths)
    return nodes

#def install_file(env, src_file, dst_dir, strip_levels=0):
    #nodes = []
    #dst_path = map(
        #lambda f:
        #os.path.join(
        #dst_dir,
        #*f.split(os.path.sep)[strip_levels:])
		#,src_file)
    #nodes += env.InstallAs(dst_path, src_file)
    #print src_file
    #print nodes
    #return nodes

def bin_dir(prefix):
    return os.path.join(prefix, 'bin')

def lib_dir(prefix):
    return os.path.join(prefix, 'lib')

def share_dir(prefix):
    return os.path.join(prefix, 'share')

def in_home_directory(dir):
	return HOME_DIR_RE.match(dir)

##build help, the help depends on having the home directory enviroment variable.. that could change
#build_help = Builder(action =
		#'export HOME=' + os.environ.get('HOME') + ' && ruby ' + os.getcwd() + '/bin/scvim_make_help -f -s $SOURCE -d $TARGET')

#create our options
opts = Options()
opts.Add(PathOption('PREFIX', 'Specify the prefix for the bin and share files',
	DEFAULT_PREFIX, PathOption.PathIsDir))

#create our enviroment, with our options and custom builders
env = Environment(options = opts)
#, BUILDERS = {'BuildHelp' : build_help})

#generate the help options
Help(opts.GenerateHelpText(env))


#get our variables from the options
PREFIX = env.get('PREFIX')

install_scedfiles = env.Alias('install-scedfiles', [
	#install_file( env, 'data/supercollider.lang', os.path.join( share_dir(PREFIX), 'gtksourceview-2.0/language-specs' ), 0 ),
	#install_file( env, 'data/supercollider.xml', os.path.join( share_dir(PREFIX), 'mime/packages' ), 0 ),
	#install_file( env, 'data/sced.gedit-plugin', os.path.join( lib_dir(PREFIX), 'gedit-2/plugins' ), 0 ),
	install_dir( env, 'data/', os.path.join( share_dir(PREFIX), 'gtksourceview-2.0/language-specs' ), LANG_FILE_RE, 1 ),
	install_dir( env, 'data/', os.path.join( share_dir(PREFIX), 'mime/packages' ), XML_FILE_RE, 1 ),
	install_dir( env, 'data/', os.path.join( lib_dir(PREFIX), 'gedit-2/plugins' ), PLUGIN_FILE_RE, 1 ),
	install_dir( env, 'sced/', os.path.join( lib_dir(PREFIX), 'gedit-2/plugins' ), PY_FILE_RE, 0 )
	])


update_mime = Builder(action =
		'update-mime-database ' + os.path.join( share_dir(PREFIX), 'mime/packages' ) )

env.BUILDERS = update_mime

#install_doc = env.Alias('install-doc', env.Install(DOC_DIR, ['SCVim.scd']))
#build_help = env.Alias('build-help', env.BuildHelp(target = Dir(DOC_DIR),
	#source = Dir(SUPERCOLLIDER_HELP_DIR)))
#AlwaysBuild(Dir(DOC_DIR))
#Depends(build_help, install_doc)

env.Alias('install', [
	install_scedfiles,
])

#this should be commonly run by the user who wishes to use it if the CACHE_DIR
# and VIMFILE_DIR is in the user's homedir
#env.Alias('install-user', [install_vimfiles, install_doc])
#this should normally be run with sudo privileges
#env.Alias('install-system', [install_bin, install_rc, install_classes])

#for x in env.Glob('*', ondisk = False, strings = False, source = False):
#	print x.get_abspath()
