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

%%% This file is part of RefactorErl.
%%%
%%% RefactorErl is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU Lesser General Public License as published
%%% by the Free Software Foundation, either version 3 of the License, or
%%% (at your option) any later version.
%%%
%%% RefactorErl is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%% GNU Lesser General Public License for more details.
%%%
%%% You should have received a copy of the GNU Lesser General Public License
%%% along with RefactorErl.  If not, see <http://plc.inf.elte.hu/erlang/>.
%%%
%%% The Original Code is RefactorErl.
%%%
%%% The Initial Developer of the Original Code is Eötvös Loránd University.
%%% Portions created  by Eötvös Loránd University and ELTE-Soft Ltd.
%%% are Copyright 2007-2025 Eötvös Loránd University, ELTE-Soft Ltd.
%%% and Ericsson Hungary. All Rights Reserved.


%%% RefactorErl command line interface (prototype)
%%%
%%% Setup:
%%% - Redefine referlpath() and referlnode()
%%% - chmod +x RefactorErl
%%% - You may also set up a system-level alias
%%%   (e.g. alias RefactorErl='/pathtorefactorerl/bin/RefactorErl')

-define(DefaultDatabase, mnesia).

-define(Usage, "
Usage:
------
RefactorErl start
    starts a singleton RefactorErl instance
RefactorErl stop
    stops the RefactorErl instance
RefactorErl fun
    remotely calls ri:fun()
RefactorErl mod fun
    remotely calls mod:fun()
RefactorErl mod fun param1 param2 ... paramN
    remotely calls mod:fun(param1, param2, ..., paramN)

Note that terms with spaces should be enclosed by double quote marks.
    E.g.: RefactorErl mod fun \"{a1, 2}\"

").


referlpath() -> filename:dirname(filename:dirname(escript:script_name())).
referlnode() -> refactorerl@localhost.


main([]) ->
    format({format, ?Usage});
main(Args) ->
    Result =
        try
            rpc_call([parse(A) || A <- Args])
        catch
            Class:Reason -> format({error, Class, Reason})
        end,
    format(Result).

parse(Str) ->
    try
        {ok, Tokens, _} = erl_scan:string(string:concat(Str, ".")),
        {ok, Term}      = erl_parse:parse_term(Tokens),
        Term
    catch
        _:_ -> Str
    end.

%% -----------------------------------------------------------------------------

rpc_call(start) ->
    rpc_call([start, ?DefaultDatabase]);
rpc_call([start, Database]) ->
    case net_adm:ping(referlnode()) of
        pong -> {format, "RefactorErl is already running at ~w.~nRun `RefactorErl stop' to stop it.~n",
                 [referlnode()]};
        pang -> start_refactorerl(Database)
    end;
rpc_call([Fun])               -> rpc_call(Fun);
rpc_call([help | Params])     -> rpc_call(ri, help, Params);
rpc_call([Mod, Fun | Params]) -> rpc_call(Mod, Fun, Params);
rpc_call(Fun) when is_atom(Fun) -> rpc:call(referlnode(), ri, Fun, []).
rpc_call(Mod, Fun, Params)      -> rpc:call(referlnode(), Mod, Fun, Params).

start_refactorerl(Database) ->
    os:cmd(referlpath() ++ "/bin/starttool " ++ referlpath() ++ " -server -db " ++ atom_to_list(Database) ++ " &"),
    erlang:set_cookie(node(), 'secretwookie'),
    ok.

%% -----------------------------------------------------------------------------

format(SqArgs) when is_list(SqArgs)->
    io:format("~s", [SqArgs]);
format({error, Class, Reason}) ->
    io:format("[CLI error] ~w: ~w~n", [Class, Reason]);
format({error, Reason}) when is_list(Reason) ->
    io:format("[CLI error] ~s~n", [Reason]);
format({format, F}) ->
    format({format, F, []});
format({format, F, Params}) ->
    io:format(F, Params);
format({badrpc,{'EXIT',{E, R}}}) ->
    erlang:raise(error, E, R);
format({badrpc, nodedown}) ->
    format({format, "RefactorErl is not running at ~w.~nRun `RefactorErl start <database>' to start with a specific database (mnesia, nif, kcmini).~nRun `RefactorErl start' to start with the default database (currently ~p).~n", [referlnode(), ?DefaultDatabase]});
format(Params) ->
    io:format("~w~n", [Params]).
