function path=horace_on(non_default_path)
%  safely switches horace on
%  horace_on()                         -- calls horace with default settings
%  horace_on(non_default_horace_path)  -- calls horace with non-default horace folder;
%
%
% $Revision: 1207 $ ($Date: 2016-03-08 11:04:47 +0000 (Tue, 08 Mar 2016) $) 
%
%
default_horace_path ='/usr/local/mprogs/Horace';
default_herbert_path='/usr/local/mprogs/Herbert';
%
if exist('non_default_path','var') && (strcmpi(non_default_path,'where') || strcmpi(non_default_path,'which'))
    path = find_default_path(default_horace_path);   
    return;
end

warn_state=warning('off','all');    % turn of warnings (so dont get errors if remove non-existent paths)
try
   horace_off();
catch
end
warning(warn_state);    % return warnings to initial state

herbert_initated=~isempty(which('herbert_init.m'));

% if herbert is not initated, try to init it. 
if ~herbert_initated
  try
    try_herbert_on(default_herbert_path);
  catch
    error('HORACE_ON:wrong_dependencies','Can not initiate Herbert');         
  end
else % reinitialize herbert on where it is now.
    her_path = fileparts(which('herbert_init.m'));
    herbert_on(her_path);
end

% init horace 
if nargin==1 
  start_app(non_default_path);
else
  start_app(default_horace_path);
end
path = fileparts(which('horace_init.m'));

hc = hor_config()
if hc.is_default
    %set up multiusers compiter specific settings, e.g.:
    %hc.use_mex = true;
end

function start_app(path)
addpath(path);
horace_init;

function path =find_default_path(her_default_path)
path = which('horace_init.m');
if isempty(path)
    path = her_default_path;
    if ~exist(fullfile(path,'horace_init.m'),'file')
        path='';
    end
else
    path=fileparts(path);
end

function try_herbert_on(default_herbert_path)

if exist(default_herbert_path,'var')
    if ~isempty(default_herbert_path)
        herbert_on(default_herbert_path);
    else
        herbert_on();        
    end
else
    herbert_on();            
end            

