import sys

from buildutils import *

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

### Generate customized scripts ###

# 'setup_cantera'
if localenv['layout'] != 'debian':
    v = sys.version_info
    localenv['python_module_loc'] = pjoin(localenv['python_prefix'], 'lib',
                                          'python%i.%i' % v[:2], 'site-packages')
    target = build(localenv.SubstFile('setup_cantera', 'setup_cantera.in'))
    install('$inst_bindir', target)

# 'mixmaster'
if env['python_package'] == 'full':
    target = build(localenv.SubstFile('mixmaster', 'mixmaster.in'))
    inst = install('$inst_python_bindir', target)
    install(localenv.AddPostAction, inst, Chmod('$TARGET', 0755))

# Cantera.mak include file for Makefile projects

if env['use_sundials'] == 'n':
    localenv['mak_sundials_libs'] = '-lcvode'
    localenv['mak_sundials_libdir'] = ''
    localenv['mak_sundials_include'] = ''
else:
    #
    # Add links to the sundials environment
    #
    localenv['mak_sundials_libs'] = ' '.join('-l%s' % s
                                         for s in localenv['sundials_libs'])
    localenv['mak_sundials_libdir'] = ('-L' + localenv['sundials_libdir']
                                   if localenv['sundials_libdir'] else '')
    localenv['mak_sundials_include'] = ('-I' + localenv['sundials_include']
                                   if localenv['sundials_include'] else '')

localenv['mak_boost_include'] = ('-I' + localenv['boost_inc_dir']
                                 if localenv['boost_inc_dir'] else '')
localenv['mak_boost_libdir'] = ('-L' + localenv['boost_lib_dir']
                                if localenv['boost_inc_dir'] and
                                localenv['use_boost_libs'] else '')
localenv['mak_boost_libs'] = ' '.join('-l%s' % s
                                      for s in localenv['boost_libs'])

#
# Handle blas lapack linkage
#
localenv['mak_have_blas_lapack_dir'] = '1' if localenv['blas_lapack_dir'] else '0'
if localenv['blas_lapack_dir']:
    localenv['mak_blas_lapack_libs'] = ' '.join('-l%s' % s
                                                for s in localenv['blas_lapack_libs'])
else:
    localenv['mak_blas_lapack_libs'] = ('-L' + '$inst_libdir' + ' -lctlapack -lctblas')

localenv['mak_threadflags'] = localenv['thread_flags']
if '-pthread' in localenv['thread_flags']:
    localenv['mak_fort_threadflags'] = '-lpthread'
else:
    localenv['mak_fort_threadflags'] = ''

#
#  Handle f2c Linkage
#
localenv['mak_have_f2c_lib'] = '1' if localenv['build_with_f2c'] else '0'
# print 'make_have-f2c_lib = ', localenv['mak_have_f2c_lib']

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

# @deprecated The install_tsc script is unused and should be removed
if localenv['layout'] != 'debian':
    install('$inst_bindir', '#platform/posix/bin/install_tsc')
