from os.path import join as pjoin, relpath
from buildutils import *

Import('env', 'install', 'buildSample')
localenv = env.Clone()

# (program name, [source files])
samples = [('demo', ['demo.c'])]

for programName, sources in samples:
    buildSample(localenv.Program, programName, sources,
                CPPPATH=['#include'],
                LIBS=env['cantera_shared_libs'],
                LIBPATH=env['extra_lib_dirs'] + ['#build/lib'])

    # Generate SConstruct files to be installed
    linkflags = ["-g", localenv["thread_flags"]]
    incdirs = [localenv["ct_incroot"]]
    libdirs = [localenv["ct_libdir"]] + localenv["extra_lib_dirs"]
    if not localenv["package_build"]:
        linkflags.append(f"-Wl,-rpath,{localenv['ct_shlibdir']}")
        libdirs.extend(localenv["extra_lib_dirs"])

    localenv["tmpl_compiler_flags"] = repr(localenv["CCFLAGS"])
    localenv['tmpl_cantera_incdirs'] = repr([x for x in incdirs if x])
    localenv['tmpl_cantera_libs'] = repr(localenv['cantera_shared_libs'])
    localenv['tmpl_cantera_libdirs'] = repr([x for x in libdirs if x])
    localenv['tmpl_cantera_linkflags'] = repr([x for x in linkflags if x])
    localenv['tmpl_cantera_frameworks'] = repr(localenv['FRAMEWORKS'])

    localenv['tmpl_progname'] = programName
    localenv['tmpl_sourcename'] = programName + '.c'

    sconstruct = localenv.SubstFile('SConstruct', 'SConstruct.in')
    install("$inst_sampledir/clib", sconstruct)

    # Generate CMakeLists.txt to be installed
    localenv['cmake_cantera_incdirs'] = ' '.join(quoted(x) for x in incdirs if x)
    localenv['cmake_cantera_libs'] = ' '.join(localenv['cantera_shared_libs'])
    localenv['cmake_cantera_libdirs'] = ' '.join(quoted(x) for x in libdirs if x)
    cmakelists = localenv.SubstFile('CMakeLists.txt', 'CMakeLists.txt.in')
    install("$inst_sampledir/clib", cmakelists)
