% PROJECT_STARTUP initialisation of matlab/octave environment for PROJECT project

%+
%
% .. _project_startup.m:
%
% =================
% project_startup.m
% =================
%
% DESCRIPTION
% ===========
%
% initialisation of matlab/octave environment for PROJECT project
%
% Define :
%  - path for matlab/octave commands used in PROJECT project
%  - IO directories in global variables
%
% This file can be either run classicaly when current directory
% is ${PROJECT}::
%
%  project_startup
%
%
% If linked or copy to ./startup.m, it will be automaticaly
% run by matlab at start.
%
% If linked or copy to ${HOME}/.octaverc, it will be automaticaly
% run by octave at start.
%
% SEE ALSO
% ========
%
% :ref:`project_profile.sh`
%
% TODO
% ====
%
% Minimal version octave : 3.0 still true ?!
%
% intwarning error
%
% intwarning octave
%
% EVOLUTIONS
% ==========
%
% $Id: project_startup.m 325 2011-08-04 15:30:01Z pinsard $
%
% $URL: svn+ssh://pinsard@forge.ipsl.jussieu.fr/ipsl/forge/projets/project/svn/trunk/src/project_startup.m $
%
% - fplod 20120424
%
%   * creation
%
%-
%
%clear all
%
matlab_minimal_version_required = '7.4';
octave_minimal_version_required = '3.0.2';
%
%disp('iii : adding path to PROJECT matlab tools')
%
% test if PROJECT environment set
global PROJECT;
PROJECT=getenv('PROJECT');
%if ( isempty(PROJECT) )
% error('eee : PROJECT environment not set');
%else
% disp(['iii : PROJECT =' PROJECT])
%end
%
% add path
addpath(PROJECT);
%
% IO directories
% test if PROJECT_ID environment set

global PROJECT_ID;
PROJECT_ID=getenv('PROJECT_ID');
%if ( isempty(PROJECT_ID) )
% error('eee : PROJECT_ID environment not set');
%end
%
% test if PROJECT_OD environment set
global PROJECT_OD;
PROJECT_OD=getenv('PROJECT_OD');
%if ( isempty(PROJECT_OD) )
% error('eee : PROJECT_OD environment not set');
%end
%
global application;
global application_version;
%
% detect if either octave or matlab running
if exist('matlabpath','builtin')
    application='matlab';
    tmp=ver(application);
    application_version=tmp.Version;
    clear tmp;
    if verLessThan('matlab', matlab_minimal_version_required)
        disp(['matlab_minimal_version_required = ', matlab_minimal_version_required]);
        disp(['application_version =', application_version]);
        warning('www : you have trouble running an older matlab version');
    end
end
if exist('OCTAVE_VERSION','builtin')
    application='octave';
    application_version=version; %% WARNING : do not add () because of matlab 6
end
%
global netcdf_open_available
netcdf_open=which('netcdf.open');
if (isempty(netcdf_open))
     netcdf_open_available=0;
else
     netcdf_open_available=1;
end
clear netcdf_open
%
% set up terminal
switch application
     case {'matlab'}
     case {'octave'}
          if ismac()
             setenv('GNUTERM','X11');
          end
     otherwise
         error(['unknown application version running ', application , ' ' ,application_version]);
end

% severe warning and errors
switch application
     case {'matlab'}
        switch application_version
           case {'7.4'}
              intwarning('on')
           case {'7.10','7.11'}
                disp([ 'non intwarning when matlab is running']);
           otherwise
              error(['unknown application version running ', application , ' ' ,application_version]);
        end

        s = warning('on', 'MATLAB:intConvertNaN');
        % turn on MATLAB:divideByZero
        s = warning('on', 'MATLAB:divideByZero');
        % show warnings
        %++debug warning('query','all');
        %++debug intwarning('query')
     case {'octave'}
        switch application_version
           case {'3.0.2'}
                disp([ 'non intwarning when octave is running']);
           case {'3.2.3','3.2.4'}
                intwarning('on')
           case {'3.4.0'}
                disp([ 'no more intwarning when octave is running']);
           otherwise
              error(['unknown application version running ', application , ' ' ,application_version]);
        end
        s = warning('on', 'Octave:divideByZero');
        % show warnings
        %++ debug warning('query','all')
        %++ 33 s=warning('query','all')
        %++ 33 s.identifier
        %++ 33 s.state
     otherwise
        error('unknown application running');
end
more on
