vars = Variables('config.py')
vars.Add('libpath', 'Directory/ies where IMP libraries are installed', None)
vars.Add('cpppath', 'Directory/ies where IMP headers are installed', None)
vars.Add('cxxflags', 'C++ compile flags', '')
vars.Add('linkflags', 'Link flags', '')
vars.Add('mock_config', 'Name of the mock config used to build IMP RPMs. '
         'Specifying this adds extra RPM/.deb and OS-specific tests.', None)
vars.Add(PathVariable('pypath',
                      'Directory/ies where IMP Python wrappers are installed',
                      None, PathVariable.PathAccept))
vars.Add(PathVariable('path',
                      'Directory/ies where IMP binaries are installed',
                      None, PathVariable.PathAccept))
vars.Add('python', 'The Python executable to use to execute IMP Python scripts',
         'python')

env = Environment(variables=vars)
Help(vars.GenerateHelpText(env))

libpath = env.get('libpath', None)
if libpath is not None:
    libpath = libpath.split(';')
cpppath = env.get('cpppath', None)
if cpppath is not None:
    cpppath = cpppath.split(';')
pypath = env.get('pypath', None)
path = env.get('path', None)
mock = env.get('mock_config', None)
cxxflags = env.get('cxxflags', '').split(' ')
linkflags = env.get('linkflags', '').split(' ')
python = env['python']

# Set dynamic library search path on all platforms
env['ENV']['LD_LIBRARY_PATH'] = libpath     # Linux/Sun
env['ENV']['DYLD_LIBRARY_PATH'] = libpath   # Mac
env['ENV']['LIBPATH'] = libpath             # AIX

# Set Python search path
env['ENV']['PYTHONPATH'] = '%s:%s' % (pypath, libpath)

# Set path
if path:
    env['ENV']['PATH'] = path + ':' + env['ENV']['PATH']

# Test compiling and running a C++ program that links against IMP
testcpp = env.Program('test.cpp', CPPPATH=cpppath, LIBPATH=libpath,
                      CXXFLAGS=cxxflags, LINKFLAGS=linkflags,
                      LIBS=['imp_base', 'imp_core', 'imp_algebra',
                            'imp_example', 'imp_container', 'imp_kernel'])
runcpp = env.Command('cpp.out', testcpp, "./$SOURCES > $TARGET")

# Test compiling and running a C++ program that links against RMF
testcpp_rmf = env.Program('test_rmf.cpp', CPPPATH=cpppath, LIBPATH=libpath,
                          CXXFLAGS=cxxflags, LINKFLAGS=linkflags,
                          LIBS=['RMF', 'boost_system'])
runcpp_rmf = env.Command('cpp_rmf.out', testcpp_rmf, "./$SOURCES > $TARGET")

# Test running a Python unittest program that uses IMP
runpy = env.Command('py.out', 'test.py', "%s $SOURCES -v > $TARGET" % python)

# Test running a Python unittest program that uses RMF
runpy_rmf = env.Command('py_rmf.out', 'test_rmf.py',
                        "%s $SOURCES -v > $TARGET" % python)

tests = [runcpp, runcpp_rmf, runpy, runpy_rmf]

# Add extra RPM and OS-specific tests if requested
if mock:
    env['ENV']['MOCK_CONFIG'] = mock
    runpy_mock = env.Command('mock.out', 'test_mock.py',
                             "%s $SOURCES -v > $TARGET" % python)
    tests.append(runpy_mock)

env.Default(tests)
