%% -*- erlang -*-
%%
IsRebar3Bare = (nomatch =/= re:run(init:get_plain_arguments(), "rebar3bare")).

CONFIG1 =
case os:getenv("TGT") of
    false ->
        CONFIG;
    [] ->
        CONFIG;
    Tgt ->
        io:fwrite("Tgt = ~p~n", [Tgt]),
        {_, Opts} = lists:keyfind(edoc_opts, 1, CONFIG),
        {_, {F,URL}} = lists:keyfind(top_level_readme, 1, Opts),
        Name = lists:last(string:tokens(URL, "/")),
        io:fwrite("Name = ~s~n", [Name]),
        NewURL = re:replace(URL, "[^/]+/" ++ Name, Tgt ++ "/" ++ Name,
                            [{return, list}]),
        C1 =
            lists:keyreplace(
              edoc_opts, 1,
              CONFIG,
              {edoc_opts, lists:keyreplace(top_level_readme, 1, Opts,
                                           {top_level_readme, {F, NewURL}})}),
        C1
end.

%% %% It seems `rebar3 bare compile` (mix) fails if the following subst is made.
%% case erlang:function_exported(rebar3, main, 1) andalso (not IsRebar3Bare) of
%%     true ->
%%         lists:keydelete(post_hooks, 1, CONFIG1)
%%             ++ [{provider_hooks, [{post, [{compile, escriptize}]}]}];
%%     _ ->
%%         CONFIG1
%% end.
