import os.path
from glob import glob

UnicornVersion = '0.0.1'

env = Environment()
opts = Options('options.py')
opts.Add(PathOption("prefix", "Installation prefix", "/usr"))
opts.Add(PathOption("libDir", "Library installation directory",
                    "${prefix}/lib"))
opts.Add(PathOption("incDir", "C/C++ header installation directory",
                    "${prefix}/include"))
opts.Add(BoolOption("enableDebug", "Compile with debug information", "yes"))

opts.Update(env)

libDir = '${libDir}'
incDir = os.path.join('${incDir}', 'unicorn')

#env.libDir = libDir
#env.incDir = incDir

Help(opts.GenerateHelpText(env))

sources = []
sources += glob(os.path.join("lib", "*.cpp"))

includes = []
includes += glob(os.path.join("lib", "unicorn", "*.h"))

env.ParseConfig('pkg-config --cflags --libs dolfin')


# Build pkg-config file
def build_pkgconfig_file(target, source, env):
    out = file(str(target[0]), 'w')
    out.writelines([
        'prefix=%s\n' % env['prefix'],
        'exec_prefix=${prefix}\n',
        'libdir=${exec_prefix}/lib\n',
        'includedir=${prefix}/include/\n',
        '\n'])

    out.write('Name: ' + 'Unicorn\n')
    out.write('Version: ' + UnicornVersion + '\n')
    out.write('Description: ' +
        'Unified continuum mechanics computation\n')
    out.write('Libs: ' + '-L%s -lunicorn' % env['libDir'] + '\n')
    out.write('Cflags: ' + '-I%s' % env['incDir'] + '\n')

    return None

#print env.items()
    
env.Command('unicorn.pc', 'SConstruct', build_pkgconfig_file)
pkgconf_file = 'unicorn.pc'
pkgconfDir = os.path.join(libDir, 'pkgconfig')

lib = env.StaticLibrary('lib/unicorn', sources,
                        CPPPATH=env['CPPPATH']+['lib'])
libInstall = env.Install(libDir, lib)
incInstall = env.Install(incDir, includes)
pkgconfInstall = env.Install(pkgconfDir, pkgconf_file)
env.Alias('install', [libInstall, incInstall, pkgconfInstall])

