#!/usr/bin/env escript
%% -*- erlang -*-
%%! -sname edts-helper

main(["release", ReleaseConfig]) ->
	{ok, Conf} = file:consult(ReleaseConfig),
	{ok, Spec} = reltool:get_target_spec(Conf),
	reltool:eval_target_spec(Spec, code:root_dir(), "rel");

main(["eunit", Ebin|Deps]) ->
    Beams = filelib:wildcard(filename:join(Ebin, "*.beam")),
    [true = code:add_path(D) || D <- [Ebin|Deps]],
    [test_one(B) || B <- Beams];

main(Args) ->
    io:format("Unknown Args: ~p~n", [Args]),
    io:format("release ReleaseConfig~n"),
    io:format("eunit EbinPathToTest Deps0 Deps1 Deps2~n"),
    halt(1).

test_one(Path0) ->
    Mod = list_to_atom(filename:basename(Path0, ".beam")),
    MI = Mod:module_info(exports),
    case proplists:lookup(test, MI) of
        {test, 0} ->
            io:format("Eunit ~s:~n", [Path0]),
            case eunit:test(Mod) of
                ok -> ok;
                error ->
                    halt(2)
            end;
        none ->
            ok
    end.
