function tsg_preferences(hTsgGUI, app_name, DEFAULT_PATH_FILE)
% tsg_preferences(S1,S2) returns structure tsg saved in S1.mat file
% and with S2 internal version number.
% If S1.mat dosn't exist, call tsg_initialisation to create it in
% prefdir directory
%
% Input
% -----
% hTsgGUI    ............ Handle to the main user interface
% app_name   ............ application name
%
% Output
% ------
% none
%
% $Id$
%

% global variable VERSION, update revision number when tsg structure change
% -------------------------------------------------------------------------
global VERSION CHAR_VERSION

% Construct config file path
% --------------------------
config_file = [prefdir, filesep, app_name, '.mat'];

% Open config file
% ----------------

% read preference mat file
% ------------------------
fid = fopen( config_file, 'r' );

% test fopen return value
% -----------------------
switch fid

  % preference file is missing
  % --------------------------
  case -1

    % create new tsg structure and save it to appdata
    % -----------------------------------------------
    new_config;
  
  % file exist and is open, fid > 2
  % -------------------------------
  otherwise

    % Pointer set to watch during loading preference file
    % ---------------------------------------------------
    set( hTsgGUI, 'Pointer', 'watch' );

    % Initialize loading preference file waitbar
    % ------------------------------------------
    wb = waitbar(0, 'Loading preference file. Please  wait...');

    % display waitbar
    % ---------------
    waitbar( 1/10, wb, 'loading structure tsg' );

    try

      % if file exist, load preference struct to workspace 
      % --------------------------------------------------
      load( config_file, 'preference' );

    catch

      % display warning
      % ---------------
      warning('tsgqc_GUI:tsg_preferences', ...
        'Can''t load preference file ''%s''', config_file);

      % quit program
      % ------------
      quitProgram(DEFAULT_PATH_FILE, hTsgGUI);

    end

    % If there no field version or wrong version, initialize
    % -------------------------------------------------------
    if ~(exist('preference','var') && ...
        isfield( preference, 'char_version') && ...
        strcmp(preference.char_version, CHAR_VERSION))

      % call new config private function
      % --------------------------------
      preference = new_config;

    end

    % call default tsg_initialisation
    % -------------------------------
    tsg_initialisation(hTsgGUI);
    
    % Get the tsg struct from the application GUI
    % -------------------------------------------
    tsg = getappdata( hTsgGUI, 'tsg_data');
    
    % add preference struct to tsg struct
    % -----------------------------------
    tsg.preference = preference;
    
    % Save structure tsg
    % ------------------
    setappdata( hTsgGUI, 'tsg_data', tsg);

    % display waitbar
    % ---------------
    waitbar( 1/2, wb, 'loading structure tsg' );

    % Close waitbar
    % -------------
    close(wb)

    % cursor back to normal
    % ---------------------
    set(hTsgGUI,'Pointer','arrow');

end

% ----------------------------------------------------------------------
% nested function new_config
% generate a new preference structure with default values
% ----------------------------------------------------------------------
  function preference = new_config

    % add tsg.preference to default values
    % -------------------------------------
    preference.version               = VERSION;
    preference.char_version          = CHAR_VERSION;
    preference.autoload              = 'off';
    preference.levitus_version       = {'WOA01','WOA05'};
    preference.levitus_value         = 2;  % WOA05
    preference.levitus_depth_string  = {'0','10'};
    preference.levitus_depth_value   = 1;
    preference.ship_speed            = 1;
    preference.plot_connected_string = {'none', '-', '--', ':', '-.'};
    preference.plot_connected_value  = 1;  % 0, line not connected

    % save preference struct to 'prefdir.mat' file
    % ---------------------------------------------
    save(config_file, 'preference');

  end

end