#!/bin/env python
"""Build DAOS Control Plane"""
#pylint: disable=too-many-locals
from SCons.Script import Literal
import os

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

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

    denv = env.Clone()
    prereqs.require(denv, 'spdk')

    # GOPATH for SCons builds in the build directory
    gopath = Dir('.').abspath
    # Version-controlled DAOS Go source directory src/control
    gosrc = Dir('.').srcnode().abspath
    # Repository path shared by DAOS Go package import paths
    repopath = "github.com/daos-stack/daos"

    # Link the version-controlled DAOS Go source directory src/control into
    # GOPATH/src/REPOPATH/src/control in the build directory.
    path = "%s/src/%s/src" % (gopath, repopath)
    if os.path.isdir(path) is False:
        os.makedirs(path)
    path += "/control"
    if os.path.isdir(path) is False:
        os.symlink(gosrc, path)

    denv.Append(GOPATH=[gopath])
    denv.AppendENVPath('GOPATH', denv['GOPATH'])
    denv['ENV']['GOBIN'] = "%s/bin" % gopath

    agentbin = "%s/bin/agent" % gopath
    agentsrc = "%s/agent/daos_agent.go" % gosrc
    denv.Command(agentbin, agentsrc,
                 "go install %s/src/control/agent" % repopath)
    denv.InstallAs("$PREFIX/bin/daos_agent", agentbin)

    gospdkpath = "%s/vendor/github.com/daos-stack/go-spdk/spdk" % gosrc

    # CGO prerequisite libs for nvme
    denv.AppendUnique(CPPPATH=['%s/include' % gospdkpath])
    nvmecontrol = denv.StaticLibrary(
        "nvme_control",
        '%s/src/nvme_control.c' % gospdkpath,
        LIBS=['spdk'])
    nc_installed = denv.Install('$PREFIX/lib', nvmecontrol)

    # CGO shell env vars.
    denv.AppendENVPath(
        "CGO_LDFLAGS",
        denv.subst("-L$SPDK_PREFIX/lib -L$PREFIX/lib $_RPATH"))
    denv.AppendENVPath(
        "CGO_CFLAGS",
        denv.subst("-I$SPDK_PREFIX/include"))

    # install management server db
    modulemgmt = "%s/mgmt" % gosrc
    mgmtinitdb = "mgmtinit_db.json"
    denv.InstallAs(
        os.path.join("$PREFIX/share/control", mgmtinitdb),
        os.path.join(modulemgmt, mgmtinitdb))
    denv.Install(
        "$PREFIX/share/spdk/fio_plugin",
        [os.path.join(modulemgmt, n) for n in
         ['fio/example_config.fio', 'fio/full_bench.fio']])

    serverbin = "%s/bin/server" % gopath
    serversrc = "%s/server/daos_server.go" % gosrc
    denv.Command(serverbin, [serversrc, nc_installed],
                 "go install %s/src/control/server" % repopath)
    denv.InstallAs("$PREFIX/bin/daos_server", serverbin)

    shellbin = "%s/bin/dmg" % gopath
    shellsrc = "%s/dmg/daos_shell.go" % gosrc
    denv.Command(shellbin, shellsrc, "go install %s/src/control/dmg" % repopath)
    denv.InstallAs("$PREFIX/bin/daos_shell", shellbin)

    drpcbin = "%s/bin/drpc_test" % gopath
    drpcsrc = "%s/drpc_test/drpc_test_server.go" % gosrc
    denv.Command(drpcbin, drpcsrc,
                 "go install %s/src/control/drpc_test" % repopath)
    denv.InstallAs("$PREFIX/bin/drpc_test_server", drpcbin)

    AlwaysBuild([agentbin, serverbin, shellbin, drpcbin])
if __name__ == "SCons.Script":
    scons()
