from buildutils import *

Import('env', 'build', 'install', 'buildSample')

# (subdir, program name, [source extensions])
samples = [
    ('combustor', 'combustor', ['cpp']),
    ('flamespeed', 'flamespeed', ['cpp']),
    ('kinetics1', 'kinetics1', ['cpp']),
    ('NASA_coeffs', 'NASA_coeffs', ['cpp']),
    ('rankine', 'rankine', ['cpp']),
    ('LiC6_electrode', 'LiC6_electrode', ['cpp'])
]

for subdir, name, extensions in samples:
    localenv = env.Clone()
    buildSample(localenv.Program, pjoin(subdir, name),
                mglob(localenv, subdir, *extensions),
                CPPPATH=['#include'],
                LIBS=env['cantera_libs'])

    # Note: These Makefiles and SConstruct files are automatically installed
    # by the "RecursiveInstall" that grabs everything in the cxx directory.

    incdirs = (localenv['ct_incroot'], localenv['sundials_include'],
               localenv['boost_inc_dir']) + tuple(localenv['extra_inc_dirs'])
    libdirs = ((localenv['ct_libdir'], localenv['sundials_libdir'],
               localenv['blas_lapack_dir']) + tuple(localenv['extra_lib_dirs']))
    localenv['tmpl_compiler_flags'] = repr(localenv['CCFLAGS'] + localenv['CXXFLAGS'])
    localenv['tmpl_cantera_frameworks'] = repr(localenv['FRAMEWORKS'])
    localenv['tmpl_cantera_incdirs'] = repr([x for x in incdirs if x])
    localenv['cmake_cantera_incdirs'] = ' '.join(quoted(x) for x in incdirs if x)
    localenv['tmpl_cantera_libs'] = repr(localenv['cantera_libs'])
    localenv['cmake_cantera_libs'] = ' '.join(localenv['cantera_libs'])
    localenv['tmpl_cantera_libdirs'] = repr([x for x in libdirs if x])
    localenv['cmake_cantera_libdirs'] = ' '.join(quoted(x) for x in libdirs if x)
    localenv['tmpl_cantera_linkflags'] = repr(localenv['LINKFLAGS'])
    localenv['tmpl_progname'] = name
    localenv['tmpl_sourcename'] = name + '.cpp'
    env_args = []

    ## Generate SConstruct files to be installed
    if localenv['TARGET_ARCH'] is not None:
        env_args.append('TARGET_ARCH={0!r}'.format(localenv['TARGET_ARCH']))
    if 'MSVC_VERSION' in localenv:
        env_args.append('MSVC_VERSION={0!r}'.format(localenv['MSVC_VERSION']))
    localenv['tmpl_env_args'] = ', '.join(env_args)

    sconstruct = localenv.SubstFile(pjoin(subdir, 'SConstruct'), 'SConstruct.in')
    install(pjoin('$inst_sampledir', 'cxx', subdir), sconstruct)

    ## Generate CMakeList.txt files to be installed
    cmakelists = localenv.SubstFile(pjoin(subdir, 'CMakeLists.txt'), 'CMakeLists.txt.in')
    install(pjoin('$inst_sampledir', 'cxx', subdir), cmakelists)

    ## Generate Makefiles to be installed
    mak_path = pjoin(localenv['ct_incroot'], 'cantera', 'Cantera.mak')
    localenv['mak_compiler_flags'] = ' '.join(localenv['CCFLAGS'] + localenv['CXXFLAGS'])
    if ' ' in mak_path:
        # There is no reasonable way to handle spaces in Makefile 'include'
        # statement, so we fall back to using the relative path instead
        mak_path = os.path.relpath(mak_path, pjoin(localenv['ct_sampledir'], 'cxx', subdir))
    localenv['make_Cantera_dot_mak'] = mak_path

    makefile = build(localenv.SubstFile(pjoin(subdir, 'Makefile'), 'Makefile.in'))
    install(pjoin('$inst_sampledir', 'cxx', subdir), makefile)
