#!/usr/bin/env escript
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92-*-
%% ex: ts=4 sw=4 et

%% Copyright: Copyright (c) 2012 Opscode, Inc.
%% License: Apache License, Version 2.0
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%    http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

%% TODO: The cookie used by erchef should be part of config
-define(SELF, 'reindexer@127.0.0.1').
-define(ERCHEF, 'erchef@127.0.0.1').
-define(ERCHEF_COOKIE, 'erchef').

%% NOTE: This is just for Open Source at the moment.
-define(OSC_ORG_ID, <<"00000000000000000000000000000000">>).
-define(OSC_ORG_NAME, 'open-source-chef').

%% @doc Perform server-side reindexing of an Open Source Chef Server.  Pre-Chef 11 servers
%% provided a REST API endpoint to perform reindexing via knife, but this is more properly
%% thought of as an administrative task, rather than a "user" task.
%%
%% Currently, three command arguments are accepted:
%%
%%     drop: Removes all entries from the index.  Information in the database is untouched.
%%     reindex: Sends information stored in the database to the index again.
%%     complete: same as "drop" followed by "reindex"
%%
%% Supplying either no argument or a wrong argument are errors.
main(Args) ->
    init_code_path(),
    init_network(),
    case Args of
        [] ->
            io:format("You didn't specify a command!  Use either 'drop', 'reindex', or 'complete'"),
            halt(1);
        ["reindex"] ->
            io:format("Reindexing everything...~n"),
            reindex();
        ["drop"] ->
            io:format("Removing all index entries...~n"),
            drop();
        ["complete"] ->
            io:format("Removing all index entries and reindexing everything...~n"),
            ok = drop(),
            reindex();
        [Command] ->
            io:format("Unrecognized command ~p~n", [Command]),
            halt(1)
    end.

drop() ->
    rpc:call(?ERCHEF, chef_solr, delete_search_db, [?OSC_ORG_ID]).

reindex() ->
    rpc:call(?ERCHEF, chef_reindex, reindex, [?OSC_ORG_NAME]).

%%------------------------------------------------------------------------------
%% Internal Functions
%%------------------------------------------------------------------------------

%% These are borrowed from the bootstrap-erchef-server script.  It would be good to extract
%% / templatize these somehow, for use across multiple server-side scripts

init_code_path() ->
    AllTheEbins = filename:join(filename:dirname(escript:script_name()), "../lib/*/ebin"),
    [code:add_path(CodePath) || CodePath <- filelib:wildcard(AllTheEbins)].

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