"""Build FUSE client"""


def configure_lustre(denv):
    """Do Lustre configure checks"""
    if GetOption('help') or GetOption('clean'):
        return denv
    # If Lustre installed build a Lustre-aware libduns
    conf = Configure(denv)
    gotversion = False
    if not conf.CheckLibWithHeader('lustreapi', 'linux/lustre/lustre_user.h',
                                   'c'):
        print("No installed Lustre version detected")
    else:
        print("Installed Lustre version detected")
        if not conf.CheckFunc('llapi_unlink_foreign'):
            print("Lustre version is not compatible")
        else:
            print("Lustre version is compatible")
            gotversion = True
    if gotversion is True:
        print("Building with Lustre bindings.")
        denv.AppendUnique(CCFLAGS=['-DLUSTRE_INCLUDE'])
    else:
        print("Not building with Lustre bindings.")
    return conf.Finish()


def scons():
    """Execute build"""
    Import('env')

    env.d_add_build_rpath()

    denv = env.Clone()

    configure_lustre(denv)

    libraries = ['daos_common', 'daos', 'uuid', 'gurt']

    dfs_src = ['dfs.c', 'dfs_sys.c', 'dfs_internal.c']
    dfs = denv.d_library('dfs', dfs_src, LIBS=libraries)
    denv.Install('$PREFIX/lib64/', dfs)

    libraries = libraries + ['dfs']

    denv.AppendUnique(LIBPATH=[Dir('.')])

    duns = denv.d_library('duns', 'duns.c', LIBS=libraries)
    denv.Install('$PREFIX/lib64/', duns)


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