import os

def build(ctx):

    libntp_source = [
        "authkeys.c",
        "authreadkeys.c",
        "clocktime.c",
        "decodenetnum.c",
        "dolfptoa.c",
        "getopt.c",
        "initnetwork.c",
        "isc_interfaceiter.c",
        "isc_net.c",
        "macencrypt.c",
        "ntp_endian.c",
        "numtoa.c",
        "refidsmear.c",
        "socket.c",
        "socktoa.c",
        "ssl_init.c",
        "syssignal.c",
    ]

    libntp_source_sharable = [
        "assert.c",
        "clockwork.c",
        "emalloc.c",
        "hextolfp.c",
        "lib_strbuf.c",
        "msyslog.c",
        "ntp_calendar.c",
        "ntp_random.c",
        "prettydate.c",
        "statestr.c",
        "systime.c",
        "timespecops.c",
    ]

    if not ctx.env.HAVE_STRLCAT or not ctx.env.HAVE_STRLCPY:
        libntp_source_sharable += ["strl_obsd.c"]

    # C library
    ctx(
        features="c cstlib",
        includes=[ctx.bldnode.parent.abspath(), "../include"],
        source=libntp_source + libntp_source_sharable,
        target="ntp",
        use="CRYPTO SSL",
    )

    if ctx.env['ntpc'] == 'ffi':
        # Loadable FFI stub
        ctx(
            features="c cshlib",
            install_path='${LIBDIR}/ntp',
            includes=[ctx.bldnode.parent.abspath(), "../include"],
            source=["ntp_c.c", "pymodule-mac.c"] + libntp_source_sharable,
            target="../pylib/ntpc",  # Put the output in the pylib directory
            use="M RT CRYPTO",
            vnum=ctx.env['ntpcver'],
        )
    elif ctx.env['ntpc'] == 'ext':
        # Loadable Python extension
        ctx(
            features="c cshlib pyext",
            install_path='${PYTHONARCHDIR}/ntp',
            includes=[ctx.bldnode.parent.abspath(), "../include"],
            source=["pymodule.c", "pymodule-mac.c"] + libntp_source_sharable,
            target="../pylib/ntpc",  # Put the output in the pylib directory
            use="M RT CRYPTO",
        )
