function xbat

% xbat - start xbat
% -----------------
%
% xbat

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 6277 $
% $Date: 2006-08-22 02:30:02 -0400 (Tue, 22 Aug 2006) $
%--------------------------------

% TODO: allow for some configuration options as input, this is just a start

%------------------------------------------
% SETUP
%------------------------------------------

% NOTE: turn off function name warning, currently it triggers on private functions

warning('off', 'MATLAB:dispatcher:nameConflict');

%------------------------------------------
% ADD PATHS
%------------------------------------------

%--
% get root directory for installation
%--

% NOTE: there is currently a mismatch between this and the xbat root

root = which('xbat');

ix = findstr(root, filesep);

root = root(1:ix(end) - 1);

%--
% add paths recursively starting from local root
%--

add_path(root, 'rec');

%------------------------------------------
% SET RECYCLE
%------------------------------------------

% NOTE: we can set delete to use the recycling bin

recycle('off');

%------------------------------------------
% CHOOSE FONT PROPERTIES
%------------------------------------------

%--
% set default font
%--

% NOTE: most fonts do not work well across scales, the current choice does

FONTS = { ...
	'Lucida Sans MS', ...
	'Arial', ...
	'Century Gothic', ...
	'Comic Sans MS', ...
	'Lucida Console', ...
	'Tahoma', ...
	'Times New Roman', ...
	'Trebuchet MS', ...
	'Verdana' ...
};
	
FONT_NAME = FONTS{4};

%--
% qualitative text size variable
%--

FONT_SIZE = 8;

%------------------------------------------
% SET FIGURE AND TEXT PROPERTIES
%------------------------------------------

% NOTE: settings are kept separate to support clarity and flexibility

%--
% turn off tex interpreter
%--

set(0, 'DefaultTextInterpreter', 'none');

%--
% turn off figure menubar
%--

set(0, 'DefaultFigureMenubar', 'none');

%--
% set axes text properties
%--

set(0, ...
	'DefaultAxesFontName', FONT_NAME, ...
	'DefaultAxesFontSize', FONT_SIZE ...
);

%--
% set text properties
%--

set(0, ...
	'DefaultTextFontName', FONT_NAME, ...
	'DefaultTextFontSize', FONT_SIZE ...
);

%--
% set uicontrol text properties
%--

set(0, ...
	'DefaultUicontrolFontName', FONT_NAME, ...
	'DefaultUicontrolFontSize', FONT_SIZE ...
);

%------------------------------------------
% CONFIGURE PALETTES
%------------------------------------------

%--
% palette size
%--

% NOTE: allowed values are: 'smaller', 'small', 'medium', 'large', and 'larger'

height = get(0, 'screensize'); height = height(4);

if height <= 768
	set_env('palette_size', 'smallest');
	
elseif height <= 1050
	set_env('palette_size', 'smaller');
	
else
	set_env('palette_size', 'small'); 
	
end

%--
% other palette options
%--

% NOTE: allowed values are 'on' and 'off'

set_env('palette_gradient', 'on');

set_env('palette_tooltips', 'on');

set_env('palette_sounds', 'on');

%------------------------------------------
% SVN STUFF
%------------------------------------------

if ~isempty(svn_root)	
	get_svn_info('refresh');
end

%------------------------------------------
% SHOW SPLASH AND OPEN XBAT PALETTE
%------------------------------------------

clc;

% TODO: include version information in the splash status bar ...

splash_image('xbat_splash_3.png', 3);

xbat_palette;

set_env('palette_sounds', 'on');

%------------------------------------------
% MISC 
%------------------------------------------

% PREFERENCES AND MODES
%--

% NOTE: this sets an environment variable used by other functions

%--
% user options
%--

show_desktop 1;

show_other_sounds 0;

%--
% developer options
%--

xbat_developer 1;

if xbat_developer
	pcode_refresh('clear');
else
	pcode_refresh('generate');
end

%--
% start palette daemon
%--

start(palette_daemon); 


%------------------------------------------------------------
% ADD_PATH
%------------------------------------------------------------

function str = add_path(pi, mode)

% add_path - add directories to path
% ----------------------------------
%
% str = add_path(pi, 'flat')
%     = add_path(pi, 'rec')
%
% Input:
% ------
%  pi - initial directory
%
% Output:
% -------
%  str - string appended to path

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 6277 $
% $Date: 2006-08-22 02:30:02 -0400 (Tue, 22 Aug 2006) $
%--------------------------------

%--
% set mode
%--

if (nargin < 2)
	mode = 'flat'; 
end 

%--
% set directories to exclude
%--

EXCLUDE_DIRS = { ...
	'.', ...
	'..', ...
	'.zip', ...
	'private', ...
	'Users', ...
	'Presets', ...
	'CVS', ...
	'.svn' ...
};

%--
% create string to append to path according to mode
%--

switch (mode)
	
	%--
	% single directory mode
	%--
	
	case ('flat')

		tmp = what_ext(pi);
		
		str = tmp.path;
		
		for k = 1:length(tmp.dir)
			sub = [pi, filesep, tmp.dir{k}];
			str = [str, pathsep, sub];
		end
		
	%--
	% recursive directory search mode
	%--
	
	case ('rec')

		tmp = what_ext(pi);
		
		str = tmp.path;
		
		for k = 1:length(tmp.dir)
			
			%--
			% check whether directory appears in excluded list
			%--
						
			ix = find(strcmpi(tmp.dir{k},EXCLUDE_DIRS));
			
			%--
			% add directory if it not a method or excluded directory
			%--
			
			if (isempty(ix) & (tmp.dir{k} ~= '@'))
				
				% NOTE: this was used to debug a '..' recursive bug
				
% 				disp([pi, filesep, tmp.dir{k}]);
				
				tmp_str = add_path([pi, filesep, tmp.dir{k}],'rec');
				str = [str, pathsep, tmp_str];
				
			end
			
		end
		
end

%--
% DEBUG -- display string appended to path
%--

% sprintf(['\t', strrep(strrep(str,'\','\\'),';',';\n\t')])

%--
% append string to path when no output
%--

if ~nargout
	path(path, str);
end


%------------------------------------------------------------
% WHAT_EXT
%------------------------------------------------------------

function W = what_ext(d,varargin)

% what_ext - get directory content information using extensions
% -------------------------------------------------------------
%
% W = what_ext(d,x1,...,xN)
%
% Input:
% ------
%  d - directory (def: pwd)
%  x1,...,xN - file extensions (def: '')
%
% Output:
% -------
%  W - structure with path, file extensions, and dir

%-------------------------------------------------
% HANDLE INPUT
%-------------------------------------------------

%--
% set directory
%--

if ((nargin < 1) | isempty(d))
	d = pwd;
end

%--
% set extensions to search
%--

if (length(varargin) < 1)
	ext = [];
else
	ext = varargin;
end

%-------------------------------------------------
% GET DIRECTORY AND FILE INFORMATION
%-------------------------------------------------

%--
% add path field 
%--

W.path = d;

%--
% get directory information
%--

d = dir(d);

d = d(3:end); % remove self and parent directory references

%--
% remove and keep directories
%--

D = {};
j = 1;

for k = length(d):-1:1
	
	if (d(k).isdir)

		D{j} = d(k).name;
		j = j + 1;
		d(k) = [];
		
	end
	
end

D = flipud(D(:));

W.dir = D;

%--
% get files with specified extensions
%--

for i = 1:length(ext)
	
	%--
	% create list of selected filenames
	%--
	
	L = {};
	j = 1;
	
	for k = length(d):-1:1
		
		%--
		% get extension from name
		%--
		
		ix = findstr(d(k).name,'.');
		
		% file has no extension in name
		
		if (isempty(ix))
			continue;
		end
		
		r = d(k).name(ix(end) + 1:end);
		
		%--
		% select file based on extension
		%--
		
		if (strcmp(r,ext{i}))
			L{j} = d(k).name;
			j = j + 1;
			d(k) = [];
		end
		
	end
	
	L = flipud(L(:));
		
	%--
	% put cell array into field
	%--
	
	W.(ext{i}) = L;
	
% 	W = setfield(W,ext{i},L);
	
end
