#!/usr/bin/env escript

-define(SELF, 'github_oauth@127.0.0.1').
-define(TARGET, 'delivery@127.0.0.1').
-define(COOKIE, 'delivery').

main(Args) ->
    init_network(),
    [GithubUrl, GithubApiUrl, ClientId, ClientSecret] = parse_args(Args),
    case rpc_call(?TARGET, deliv_oauth_application, insert, [<<"github">>, <<"deliv_github_client">>, GithubUrl, GithubApiUrl, ClientId, ClientSecret]) of
        [_] ->
            io:format("Successfully added new Github OAuth configuration.~n");
        {error, conflict} ->
            perror(io:format("Error: Github OAuth configuration already exists!~n"));
        {error, Why} ->
            perror(io:format("Error: ~p~n", [Why]))
    end.

init_network() ->
    net_kernel:start([?SELF, longnames]),
    erlang:set_cookie(node(), ?COOKIE),
    pong = net_adm:ping(?TARGET).

parse_args([_GithubUrl, _GithubApiUrl, _ClientId, _ClientSecret] = Args) ->
    Args;
parse_args(Other) ->
    perror(io:format("Invalid Arguments: ~p~n", [Other])).

rpc_call(Node, Module, Function, Args) ->
    case rpc:call(Node, Module, Function, Args) of
        {badrpc, Reason} ->
            perror(io_lib:format("RPC call ~p:~p:~p:~p failed with error ~p~n",
                                 [Node, Module, Function, Args, Reason]));
        Res ->
            Res
    end.

perror(Msg) ->
    io:format(Msg),
    erlang:halt(1).
