# -*- python -*-
# Copyright (C) 2013, Zhang Yang.
# Author:  Zhang Yang <zy.netsec@gmail.com>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
import os
Import('*')

MI_DATAFILES = []

def MI_pymod(srcdir, target, sources):
    depPyModule(env, 'root.pyext', srcdir, target, sources)

def _cp(env, alias, filelist, reldir):
    '''
    1. filelist<str> is a source file path, reldir<str> is the relative file path of 
    the target installation path.
    2. filelist<list> is a list contains of source files, reldir<str> is the
    relative directory path of the target installation position.
    '''
    inst_flist = []
    if type(filelist) is str:
        fpath = os.path.abspath(filelist)
        destfile = reldir
        inst_flist.append(fpath)
        depInstallAs(env, alias, destfile, fpath)
        return inst_flist

    files = []
    dirs = []
    for f in filelist:
        f = os.path.abspath(f)
        if os.path.isdir(f):
            dirs.append(f)
        else:
            files.append(f)

    if dirs:
        inst_flist.extend(depInstallDir(env, alias, reldir, dirs))

    if files:
        inst_flist.extend(files)
        depInstall(env, alias, reldir, files)

    return inst_flist

def MI_data(filelist, reldir):
    flist = _cp(env, 'root_src', filelist, '$destdir$root_datadir/%s' % reldir)
    MI_DATAFILES.extend(flist)

def MI_pymod_cp(filelist, reldir):
    _cp(env, 'root.pyext', filelist, '$pyextdir$root_libdir/%s' % reldir)

Export('MI_pymod', 'MI_pymod_cp', 'MI_data')
Export('MI_DATAFILES')

## MI main program
SConscript('mi/SConscript')

## MI configuration files
SConscript('etc/SConscript')

# Pack src/mi and src/etc into root.src.step1.tar.gz

t0 = env.Command('$TOPDIR/bindir/root.src.step1.tar.gz', [env.Alias('root_src'), env.Alias('root.src.etc')],
            #['tar c -C $destdir . | gzip -9 > $TARGET'])
            ['tar czf $TARGET -C $destdir .'])
### TARGET: root.src.step1
env.Alias('root.src.step1', t0)
### Maybe need some c compile, so include it in eng mode.
if TARGET_BUILD_VARIANT == 'eng':
    ## MI need some third part libs
    SConscript('py3rd/SConscript')
    
    ## MI need some self libs
    SConscript('pyext/SConscript')
    
    # There will compile the src/pyext (python modules for magicinstaller)
    # Do it at develmode ,because we won't compile the python modules 
    # every time (especially at dist mode).
    t1 = env.Command('$TOPDIR/bindir/root.src.pyext.tar.gz', env.Alias('root.pyext'),
                #['tar c -C $pyextdir . | gzip -9 > $TARGET'])
                ['tar czf $TARGET -C $pyextdir .'])
    ### TARGET: root.src.pyext
    env.Alias('root.src.pyext', t1)

SConscript('SConscript-mi')

