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            

function path=herbert_on(non_default_path)
% The function intended to swich herbert on and
% return the path were herbert is resided or 
% empty string if herbert has not been found
%
%
% The function has to be present in Matlab search path 
% and modified for each machine to know default herbert location
%
%Usage:
%>>path=herbert_on(); 
%       enables herbert and initiates herbert default search path
%>>path=herbert_on('where'); 
%       reports current location of herbert or empty if not found
%
%>>path=herbert_on('a path'); 
%       initiates herbert on non-default search path
%
%
%
%
her_default_path='c:/Users/wkc26243/Documents/work/svn/Herbert';
%
if exist('non_default_path','var') && (strcmpi(non_default_path,'where') || strcmpi(non_default_path,'which'))
    path = find_her_default_path(her_default_path);   
    return;
end
if nargin==1 
    start_herbert(non_default_path);    
else
    start_herbert(her_default_path);    
end
path = fileparts(which('herbert_init.m'));

% set up multiusers compiter specific settings,
% namely settings wich are common for all new users of the specific computer
% e.g.:
hec = herbert_config()
if hec.is_default
% hec.use_mex_C = true;
end


function start_herbert(path)

try
    herbert_off;
catch
end
addpath(path);
herbert_init;

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

