vars = Variables('config.py')
vars.Add('libpath',
         'Directory/ies where IMP or dependency libraries are installed', None)
vars.Add('cpppath',
         'Directory/ies where IMP or dependency headers are installed', None)
vars.Add('datapath',
         'Directory where IMP data are installed', None)
vars.Add('examplepath',
         'Directory where IMP examples 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('mpi_module', 'Name of the module to load to enable MPI.'
         'Specifying this adds extra tests, of MPI components.', 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)
datapath = env.get('datapath', None)
examplepath = env.get('examplepath', None)
mock = env.get('mock_config', None)
mpi = env.get('mpi_module', 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 paths
if path:
    env['ENV']['PATH'] = path + ':' + env['ENV']['PATH']
if datapath:
    env['ENV']['IMP_DATA'] = datapath
if examplepath:
    env['ENV']['IMP_EXAMPLE_DATA'] = examplepath

# 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_core', 'imp_algebra', 'imp_score_functor',
                            '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)

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

tests = [runcpp, runcpp_rmf, runpy, runpy_rmf, runpy_ihm]

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

# Add MPI-specific tests if requested
if mpi:
    env['ENV']['MOCK_CONFIG'] = mock
    runpy_mpi = env.Command('mpi.out', 'test_mpi.py',
                             "source /etc/profile && module load %s "
                             "&& %s $SOURCES -v > $TARGET" % (mpi, python))
    tests.append(runpy_mpi)

env.Default(tests)
