# vim: set filetype=python :

# Note: We don't require or recommend running scons examples anymore.
# However, it will still work and will create executable versions of these
# examples in the GalSim/examples_bin directory.

import SCons
import os

Import('env')
AddRPATH = env['_AddRPATH']

libs=['galsim']

env1 = env.Clone(CPPDEFINES=[],LIBS=libs+env['LIBS'])

env1['OBJPREFIX'] = '.obj/'

bin_dir = 'bin'
bin_install_dir = os.path.join(env['INSTALL_PREFIX'], bin_dir)

lib_dir = 'lib'
lib_install_dir = os.path.join(env['FINAL_PREFIX'], lib_dir)

# Include the library location within the executable.
if 'install' in COMMAND_LINE_TARGETS:
    AddRPATH(env1,Dir(lib_install_dir).abspath)
else:
    AddRPATH(env1,Dir('#lib').abspath)

int_example = env1.Program(os.path.join('#examples_bin','int_example'), 'IntExample.cpp')
test_random = env1.Program(os.path.join('#examples_bin','test_random'), 'testRandom.cpp')

targets = [ int_example, test_random ]

# Also build executable versions of the demo scripts:
scripts = [ 'demo1', 'demo2', 'demo3', 'demo4', 'demo5',
            'demo6', 'demo7', 'demo8', 'demo9', 'demo10', 
            'demo11', 'demo12' ]

for script in scripts:
    exec_script = env.ExecScript(os.path.join('#examples_bin',script), script + '.py')
    # SCons isn't clever enough to realize that these need to be rebuilt if the PYTHON
    # value changes.  And since they are quick to build, might as well always do them 
    # whenever someone runs scons examples.
    AlwaysBuild(exec_script)
    targets.append(exec_script)


env.Alias(target='examples',source=targets)

# I think we don't actually want to install the example programs.
# But here is how we would do it for real programs that the user will want
# to have installed.  To actually do this, type `scons install examples`.
if 'install' in COMMAND_LINE_TARGETS:

    installed_bin = env1.Install(dir=bin_install_dir, source=targets)
    env1.Alias(target='install', source=installed_bin)


if 'uninstall' in COMMAND_LINE_TARGETS:
    # There is no env.Uninstall method, we must build our own
    deltarget = Delete("$TARGET")

    binfiles = [os.path.join(bin_install_dir, os.path.basename(str(f[0]))) for f in targets]

    for f in binfiles:
        env1.Alias('uninstall', env1.Command(f, None, deltarget))


