-module(freeswitch_app).
-author('fig@marketelsystems.com').

-behaviour(supervisor).

-export([start/2, stop/1, init/1, go/0]).

-define(MAX_RESTART,         5).
-define(MAX_TIME,           60).

%%----------------------------------------------------------------------
%% Application behaviour callbacks
%%----------------------------------------------------------------------
start(_Type, _Args) ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, [freeswitch_monitor]).

stop(_S) ->
    ok.

go() ->
    Apps = [tcp_server, figure, storage],
    lists:foreach(fun (App) ->
			  application:stop(App)
		  end,
		  lists:reverse(Apps)),
    lists:foreach(fun (App) ->
			  application:start(App)
		  end,
		  Apps).

%%----------------------------------------------------------------------
%% Supervisor behaviour callbacks
%%----------------------------------------------------------------------
init([Module]) ->
    {ok,
        {_SupFlags = {one_for_one, ?MAX_RESTART, ?MAX_TIME},
            [
              % FreeSWITCH supervisor.
              {   Module,                                  % Id       = internal id
                  {Module,start_link,[]},       % StartFun = {M, F, A}
                  permanent,                               % Restart  = permanent | transient | temporary
                  15000,                                    % Shutdown = brutal_kill | int() >= 0 | infinity
                  worker,                                  % Type     = worker | supervisor
                  [Module]                                 % Modules  = [Module] | dynamic
              }
            ]
        }
    }.
