"""Build pydaos client"""
import sys


def build_shim_module():
    """Build PyDAOS shim module for the specified python version"""

    if GetOption('help'):
        return

    version = f'{sys.version_info.major}.{sys.version_info.minor}'

    Import('base_env')

    new_env = base_env.Clone()
    new_env.ParseConfig(f'pkg-config --cflags --libs python-{version}')

    new_env.Replace(LIBS=['daos', 'duns'])
    new_env.AppendUnique(LIBPATH=[Dir('../dfs')])
    new_env.AppendUnique(LIBPATH=[Dir('../api')])

    new_env['CC'] = 'gcc'
    new_env.AppendUnique(CCFLAGS='-pthread')

    new_env.compiler_setup()

    obj = new_env.SharedObject('pydaos_shim', 'pydaos_shim.c',
                               SHLINKFLAGS=[],
                               SHLIBPREFIX="")
    base = new_env.d_library(target='pydaos_shim', source=[obj],
                             install_off="../../../..",
                             SHLINK='gcc -pthread -shared',
                             SHLINKFLAGS=[],
                             SHLIBPREFIX="",
                             SHLIBSUFFIX='.so')
    install_path = f'$PREFIX/lib64/python{version}/site-packages/pydaos'
    new_env.Install(install_path, base)
    # install new wrappers too
    new_env.Install(install_path, "__init__.py")
    new_env.Install(install_path, "pydaos_core.py")
    # install raw wrappers
    install_path += "/raw"
    new_env.Install(install_path, "raw/__init__.py")
    new_env.Install(install_path, "raw/daos_api.py")
    new_env.Install(install_path, "raw/conversion.py")
    new_env.Install(install_path, "raw/daos_cref.py")


if __name__ == "SCons.Script":
    build_shim_module()
