%% -*- erlang -*-
%%
%% This helper script checks if doc is being built, otherwise removes edoc dep.
%% To build docs, call `rebar get-deps doc`
%% Assumes that the rebar config is bound to CONFIG
[_|Args] = init:get_plain_arguments().  % rebar 'commands' and options
case lists:member("edown=true", Args) of
    false ->
	{ok,C1} = file:script(filename:join(filename:dirname(SCRIPT),
					    "remove_deps.script"),
			      [{'CONFIG', CONFIG}, {'DEPS', [edown]}]),
	C1;
    true ->
	%% We actually only need to start inets if we have a doc path with http URIS
	application:start(crypto),
	application:start(asn1),
	application:start(public_key),
	application:start(ssl),
	application:start(inets),
        case code:lib_dir(edown) of
            {error, bad_name} ->
                D = {edown, ".*",
                     {git, "git://github.com/uwiger/edown.git", "HEAD"}},
                Deps = case lists:keyfind(deps, 1, CONFIG) of
                           false -> [D];
                           {_, Ds} ->
                               case lists:keymember(edown, 1, Ds) of
                                   true -> Ds;
                                   false -> [D|Ds]
                               end
                       end,
                lists:keystore(deps, 1, CONFIG, {deps, Deps});
            _ ->
                CONFIG
        end
end.
