import os

from buildutils import *

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

# Copy man pages
if env['INSTALL_MANPAGES']:
    install('$inst_mandir', multi_glob(localenv, '#platform/posix/man', '*'))

### Generate customized scripts ###

# cantera.pc for use with pkg-config

pc_libs = list(localenv['cantera_shared_libs'])
pc_libdirs = []
pc_incdirs = []
pc_cflags = list(localenv['CXXFLAGS'])

if not localenv["package_build"]:
    pc_incdirs.extend(localenv["extra_inc_dirs"])
    pc_libdirs.extend(localenv["extra_lib_dirs"])

    if localenv['system_sundials'] and localenv["sundials_include"]:
        pc_incdirs.append(localenv['sundials_include'])

    if localenv["boost_inc_dir"]:
        pc_incdirs.append(localenv['boost_inc_dir'])

    if localenv["use_hdf5"]:
        pc_incdirs.append(localenv["hdf_include"])

if 'Accelerate' in localenv['FRAMEWORKS']:
    pc_cflags.append('-framework Accelerate')

if '-pthread' in localenv['thread_flags']:
    pc_cflags.append('-pthread')
    pc_libs.append('pthread')

if localenv["package_build"]:
    # Remove sysroot flags in templated output files. This only applies to the
    # conda package for now.
    # Users should compile against their local SDKs, which should be backwards
    # compatible with the SDK used for building.
    excludes = (
        "-isysroot", "-mmacosx", "-march", "-mtune", "-fdebug-prefix-map")
    localenv["CCFLAGS"] = compiler_flag_list(localenv["CCFLAGS"], excludes)
    localenv["CXXFLAGS"] = compiler_flag_list(localenv["CXXFLAGS"], excludes)

# Generate cantera.pc for use with pkg-config
localenv["pc_prefix"] = localenv["prefix"]
localenv["pc_libdirs"] = " ".join(f"-L{dir}" for dir in pc_libdirs)
localenv["pc_libs"] = " ".join(f"-l{lib}" for lib in pc_libs)
localenv["pc_incdirs"] = " ".join(f"-I{dir}" for dir in pc_incdirs)
localenv["pc_cflags"] = " ".join(pc_cflags)

pc = build(localenv.SubstFile("cantera.pc", "cantera.pc.in"))
install("$inst_libdir/pkgconfig", pc)
