import sys

from buildutils import *

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

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

### Generate customized scripts ###

# 'setup_cantera'
if localenv['layout'] != 'debian' and env['OS'] != 'Windows':

    def copy_var(target, source, env):
        if env['python_prefix'] == 'USER':
            env['python_module_loc_sc'] = ''
        else:
            env['python_module_loc_sc'] = env['python_module_loc']

    for script in ['setup_cantera', 'setup_cantera.csh']:
        target = env.SubstFile(script, script + '.in')
        localenv.AddPreAction(target, copy_var)
        localenv.Depends(target, env['install_python_action'])
        install('$inst_bindir', target)

# Cantera.mak include file for Makefile projects
# cantera.pc for use with pkg-config

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

localenv['mak_corelibs'] = '-lcantera'

localenv['mak_extra_includes'] = ' '.join('-I%s' % s for s in localenv['extra_inc_dirs'])
pc_incdirs.extend(localenv['extra_inc_dirs'])

localenv['mak_extra_libdirs'] = ' '.join('-L%s' % s for s in localenv['extra_lib_dirs'])
pc_libdirs.extend(localenv['extra_lib_dirs'])

localenv['mak_stdlib'] = ''.join('-l' + lib for lib in env['cxx_stdlib'])

if localenv['system_sundials']:
    # Add links to the sundials environment
    localenv['mak_sundials_libs'] = ' '.join('-l%s' % s
                                             for s in localenv['sundials_libs'])
    if localenv['sundials_libdir']:
        localenv['mak_sundials_libdir'] = '-L' + localenv['sundials_libdir']
        pc_libdirs.append(localenv['sundials_libdir'])
    else:
        localenv['mak_sundials_libdir'] = ''

    if localenv['sundials_include']:
        localenv['mak_sundials_include'] = '-I' + localenv['sundials_include']
        pc_incdirs.append(localenv['sundials_include'])
    else:
        localenv['mak_sundials_include'] = ''

if localenv['boost_inc_dir']:
    localenv['mak_boost_include'] = '-I' + localenv['boost_inc_dir']
    pc_incdirs.append(localenv['boost_inc_dir'])
else:
    localenv['mak_boost_include'] = ''

# Handle BLAS/LAPACK linkage
blas_lapack_libs = ' '.join('-l%s' % s for s in localenv['blas_lapack_libs'])
if localenv['blas_lapack_dir']:
    localenv['mak_blas_lapack_libs'] = '-L{} {}'.format(localenv['blas_lapack_dir'],
                                                        blas_lapack_libs)
else:
    localenv['mak_blas_lapack_libs'] = blas_lapack_libs

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

localenv['mak_threadflags'] = localenv['thread_flags']
if '-pthread' in localenv['thread_flags']:
    localenv['mak_fort_threadflags'] = '-lpthread'
    pc_cflags.append('-pthread')
    pc_libs.append('pthread')
else:
    localenv['mak_fort_threadflags'] = ''

mak = build(localenv.SubstFile('Cantera.mak', 'Cantera.mak.in'))
install('$inst_incdir', mak)

# Generate cantera.pc for use with pkg-config
localenv['pc_prefix'] = localenv['prefix']
localenv['pc_libdirs'] = ' '.join('-L' + d for d in pc_libdirs)
localenv['pc_libs'] = ' '.join('-l' + lib for lib in pc_libs)
localenv['pc_incdirs'] = ' '.join('-I' + d for d in pc_incdirs)
localenv['pc_cflags'] = ' '.join(pc_cflags)

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