
NifSharedSources = ["c_src/termbox2_nif.c"].
NifStaticSources = NifSharedSources.
CFlagsDefault = "$CFLAGS -Os ".
DrvLdFlagsDefault = "-shared -ltermbox2".

DrvLdFlags =
    case os:getenv("TERMBOX2_CFLAGS") of
        NotDefined when NotDefined == false; NotDefined == [] -> DrvLdFlagsDefault;
        DrvLdFlagsOverride -> DrvLdFlagsOverride
    end.

{NifSources, LdEnv, CFlagsExt} =
    case os:getenv("TERMBOX2_USE_SYSTEM") of
        NotDefined when NotDefined == false; NotDefined == [] -> {NifStaticSources, [], " -Ic_src/termbox2"};
        _Defined -> {NifSharedSources, [{"DRV_LDFLAGS", DrvLdFlags}], []}
    end.

CFlags =
    case os:getenv("TERMBOX2_CFLAGS") of
        NotDefined when NotDefined == false; NotDefined == [] -> CFlagsDefault ++ CFlagsExt;
        CFlagsOverride -> CFlagsOverride
    end.

[
    {minimum_otp_vsn, "22.0"},

    {erl_opts, [debug_info]},

    {xref_checks, [undefined_function_calls,
                   undefined_functions,
                   locals_not_used,
                   deprecated_function_calls,
                   deprecated_functions]},


    {port_env, [
        %% Default darwin ldflags causes loading of system sqlite. Removed -bundle flag.
        {"darwin", "DRV_LDFLAGS",
            "-flat_namespace -undefined dynamic_lookup -dynamiclib $ERL_LDFLAGS"},

        {"solaris", "CFLAGS",
            "$CFLAGS -std=c99"},

        {"linux", "CFLAGS",
            "$CFLAGS -std=c11"},

        {"CFLAGS", CFlags}
    ] ++ LdEnv},
    {port_specs, [
        {"priv/termbox2_nif.so", NifSources}
    ]},
    {pre_hooks,
  	[{"(linux|darwin|solaris)", compile, "make -C c_src"},
   	 {"(freebsd)", compile, "gmake -C c_src"}]},
    {post_hooks,
  	[{"(linux|darwin|solaris)", clean, "make -C c_src clean"},
   	 {"(freebsd)", clean, "gmake -C c_src clean"}]}, 

    {plugins, [pc]},

    {provider_hooks,
        [{post,
        [{compile, {pc, compile}},
            {clean, {pc, clean}}]}]},

    {dialyzer, [
        {warnings, [
            unmatched_returns,
            error_handling,
            race_conditions,
            underspecs,
            unknown
        ]}
    ]},

    {edoc_opts, [{preprocess, true},
                 {stylesheet, "style.css"},
                 {sort_functions, false}]},

    {hex, [{doc, edoc}]}

].
